How to retrieve CPContentHelper or detailed product information in FreeMarker template of Product Publisher (Liferay 7.4 Commerce)?

Hi everyone,

I’m using Liferay Commerce version 7.4 u140 and working with the Product Publisher widget to display catalog entries. In my FreeMarker template I have code like:

<#if entries?has_content>
  <#list entries as curCPCatalogEntry>
    ${curCPCatalogEntry.CPDefinitionId}[$CURSOR$]
  </#list>
</#if>

I want to access more detailed information in the template, such as SKU, price, image, and custom (expando) fields of the product. In the widget’s JSP code (out-of-the-box) they retrieve:

CPContentHelper cpContentHelper =
    (CPContentHelper)request.getAttribute(CPContentWebKeys.CP_CONTENT_HELPER);

CPCatalogEntry cpCatalogEntry =
    cpContentHelper.getCPCatalogEntry(request);

String imageURL = cpContentHelper.getCPDefinitionCDNURL(
    cpCatalogEntry.getCPDefinitionId(), request);

In my FreeMarker template I attempted:

<#assign cpContentHelper =
    serviceLocator.findService(
      "com.liferay.commerce.product.content.helper.CPContentHelper") />

But this failed with a message indicating that non-ServiceBuilder generated services aren’t retrievable via serviceLocator. Also, the variable cpContentHelper is not available in my template context — I only have entries and curCPCatalogEntry.

Here are my specific questions:

  1. Is there a supported way to obtain CPContentHelper in a FreeMarker template (without using serviceLocator)?

  2. If not using CPContentHelper, how can I retrieve a product’s SKU, price, image, and custom Expando fields directly from curCPCatalogEntry (or any other context variable) in the FreeMarker template?

  3. Is there a reference FreeMarker (.ftl) template example for Liferay Commerce that shows this use case (Product Publisher, product details) that I can adapt?

  4. If the right approach is to create a custom OSGi TemplateContextContributor to expose the helper/service, has anyone created such a module and can share a sample snippet?

Any assistance, sample code, or pointers to relevant docs would be greatly appreciated. Thank you!

2 Answers

2

Hi @nhathm,

Product publisher widget has access to ‘cpContentHelper’ variable, and it has been added since 7.4 U112, therefore, you don’t have to use service locator in order to utilize ‘cpContentHelper’. Kindly check: https://liferay.atlassian.net/browse/LPD-16623

Following documentation would also help you further: https://learn.liferay.com/w/dxp/commerce/developer-guide/content/implementing-a-custom-product-content-renderer

Hi @Pooja_Bhambani

I am facing some problems as well in widget template for Commerce Search Result.

I have added one simple or virtual product with multiple SKU. I want to display each sku information on product listing page using commerce search result.

I have used this code to get this information:

<#list cpCatalogEntry.getCPSkus() as sku>
<@liferay_commerce_ui[“price”] CPCatalogEntry=cpCatalogEntry
CPDefinitionId=cpCatalogEntry.getCPDefinitionId() CPInstanceId=sku.getCPInstanceId()
showDiscount=false />
</#list>

But it is returning same price for all skus. to overcome this problem, I have to call headless api but still there are some problem. headless api returning sku price after deducting tax.

So can you suggest why it is returning same price for all skus.