RE: RE: Commerce Categories Navigation Template and Key-Value Properties

thumbnail
Gennaro Lippiello, modified 3 Years ago. Junior Member Posts: 39 Join Date: 3/9/22 Recent Posts

Hello. I have a custom Commerce Categories Navigation Template and I want to get the category key-value properties. 

How can I do that?


 

thumbnail
Jeffrey Handa, modified 3 Years ago. Liferay Master Posts: 541 Join Date: 12/1/08 Recent Posts

Hi Gennaro, 

You'll need to use the servicelocator to get the AssetCategoryPropertyLocalService (com.liferay.asset.category.property.service.AssetCategoryPropertyLocalService).  That service has a getCategoryProperty that should get you what you need.  You can find a quick example here:  https://gist.github.com/jhanda/aa997c302f9f1ad99ee2e62fa07cd8a8.  

thumbnail
Gennaro Lippiello, modified 3 Years ago. Junior Member Posts: 39 Join Date: 3/9/22 Recent Posts

Hello Jeffrey, following this example, if I dont have specified properties for a category I get this error:

NoSuchCategoryPropertyException: No AssetCategoryProperty exists with the key {categoryId=40816, key=esenzione}

thumbnail
Jeffrey Handa, modified 3 Years ago. Liferay Master Posts: 541 Join Date: 12/1/08 Recent Posts

HI Gennaro, 

Sorry, I'm not much of a FreeMarker expert so please don't consider my example as a best practice.  You should be able to wrap your code in an Attempt/Recover block.  Something like:  

<#attempt>
    <#if assetCategoryPropertyLocalService.getCategoryProperty(categoryId,propertyName)??>
        ${(assetCategoryPropertyLocalService.getCategoryProperty(categoryId,propertyName).getValue())!}
    </#if>
<#recover>
    No property defined
</#attempt>