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

thumbnail
Gennaro Lippiello, modificat fa 4 anys. Junior Member Apunts: 39 Data d'incorporació: 09/03/22 Publicacions recents

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, modificat fa 3 anys. Liferay Master Apunts: 541 Data d'incorporació: 01/12/08 Publicacions recents

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, modificat fa 3 anys. Junior Member Apunts: 39 Data d'incorporació: 09/03/22 Publicacions recents

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, modificat fa 3 anys. Liferay Master Apunts: 541 Data d'incorporació: 01/12/08 Publicacions recents

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>