Search code examples
sap-commerce-cloudrestriction

Custom Product restrictions are not working on child components


I have created a custom product restriction for a component due to project specific visibility rules. This product restriction can be applied on ExampleListComponent, ExampleComponent and also can be used in homepage, product page or content pages as and when required. ExampleListComponent can have multiple ExampleComponent instances. When apply the restriction on ExampleListComponent(which is added directly on homepage content slot) it is working fine but when apply the same on ExampleComponent(child component) it is not working. Following are some code snippets

items.xml

<collectiontype code="ExampleCardList" elementtype="ExampleComponent" autocreate="true" generate="true" type="list" />

    <itemtype code="ExampleComponent" extends="AbstractMediaContainerComponent" autocreate="true" generate="true" jaloclass="com.example.core.jalo.ExampleComponent">
        <attributes>
            <attribute qualifier="title" type="localized:java.lang.String">
                <persistence type="property" />
            </attribute>
            <attribute qualifier="secondaryMedia" type="Media">
                <modifiers read="true" write="true" search="true" unique="false" />
                <persistence type="property" />
            </attribute>
        </attributes>
    </itemtype>

    <itemtype code="ExampleListComponent" extends="SimpleCMSComponent" autocreate="true" generate="true" jaloclass="com.example.core.jalo.ExampleListComponent">
        <attributes>
            <attribute qualifier="cards" type="ExampleCardList">
                <persistence type="property" />
            </attribute>
        </attributes>
    </itemtype>

    <itemtype code="ExampleAvailabilityRestriction" jaloclass="com.example.core.jalo.restrictions.ExampleAvailabilityRestriction" extends="AbstractRestriction" autocreate="true"   generate="true">
        <attributes>
            <attribute qualifier="product" type="ProductList">
                <persistence type="property" />
            </attribute>
        </attributes>
    </itemtype>
    
    

ExampleAvailabilityRestrictionEvaluator.java

public class ExampleAvailabilityRestriction implements CMSRestrictionEvaluator<ExampleAvailabilityRestrictionModel> {

    public boolean evaluate(ExampleAvailabilityRestrictionModel restrictionModel, RestrictionData restrictionData) {
        List<ProductModel> products = restrictionModel.getProduct();
        if (CollectionUtils.isEmpty(products)) {
            return true;
        }

        for (ProductModel product : products) {
            if (!availabilityService.isProductAvailable(product.getCode())) {
                return false;
            }
        }

        return true;
    }
}

I verified the other OTB restriction and code looks ok. Is there any other configurations required to be able to apply the restriction on child components.


Solution

  • This article helped me to resolve the issue.

    https://answers.sap.com/questions/13121879/time-restriction-not-working-on-cms-link-component.html

    Added the following line in examplecardcomponent.jsp

    <c:if test="${ycommerce:evaluateRestrictions(component)}">
    
    </c:if>
    

    also we can add this on the parent component jsp(examplecardlistcomponent.jsp) while iterating the child components

    <c:forEach var="exampleCard" items="${exampleCards}" varStatus="loop">
         <cms:component component="${exampleCard}" evaluateRestriction="true" />
    </c:forEach>