Steps to create a new CMS Restriction

If your project requires custom rules for the visibility of CMS components, you must create your own type of CMS restrictions. 

Introduction

There is a requirement to show a banner to a customer who is browsing the eShop with one or more products in the cart and he doesn't update the cart for some minutes. He may need assistance and it would be better if he calls the hotline.

Prerequisites

  • Experience developing with Hybris
  • Have seen the CMS cockpit and understanding of its WCMS - Key concepts and features
  • B2C Accelerator or other frontend installed on your local machine

Data Model

We add the extension cms2 as requirement in the extensioninfo.xml of our new custom extension. In this example, we use the extension cmstraining. It already has bean auto scanning.

Then we define a new type in cmstraining-items.xml:

   <itemtypes>
                <itemtype generate="true"
                   code="UndecidedShoppingCustomerRestriction"
                   jaloclass="org.areco.ecommerce.cmstraining.jalo.UndecidedShoppingCustomerRestriction"
                   extends="AbstractRestriction"
                   autocreate="true">
                        <attributes>
                                <attribute qualifier="timeToWaitInMinutes" type="java.lang.Integer">
                                        <description>The time to wait before triggering this restriction</description>
                                        <modifiers optional="false"/>
                                        <persistence type="property"/>
                                </attribute>
                <attribute qualifier="description" type="java.lang.String" redeclare="true">
                    <persistence type="dynamic" attributeHandler="undecidedShoppingCustomerRestrictionDynamicDescription" />
                    <modifiers write="false" />
                </attribute>
                        </attributes>
                </itemtype>
        </itemtypes>

The jalo property description comes from AbstractRestriction and we declare it as dynamic. Because of this, we have to provide two implementations: A jalo method and a Dynamic Attribute handler.

package org.areco.ecommerce.cmstraining.jalo;
 
import de.hybris.platform.jalo.SessionContext;
import de.hybris.platform.util.localization.Localization;
 
import org.areco.ecommerce.cmstraining.attributehandlers.UndecidedShoppingCustomerRestrictionDynamicDescription;
 
public class UndecidedShoppingCustomerRestriction extends GeneratedUndecidedShoppingCustomerRestriction
{
 
  /**
   * @param pSessionContext
   * @deprecated
   */
  @Override public String getDescription(final SessionContext pSessionContext)
  {
    return Localization.getLocalizedString(UndecidedShoppingCustomerRestrictionDynamicDescription.KEY_DESCRIPTION_TEXT,
        new Object[] { this.getTimeToWaitInMinutes(pSessionContext) });
  }
}
 
package org.areco.ecommerce.cmstraining.attributehandlers;
 
import de.hybris.platform.servicelayer.model.attribute.DynamicAttributeHandler;
import de.hybris.platform.util.localization.Localization;
import org.areco.ecommerce.cmstraining.model.UndecidedShoppingCustomerRestrictionModel;
import org.springframework.stereotype.Component;
 
@Component
public class UndecidedShoppingCustomerRestrictionDynamicDescription implements DynamicAttributeHandler<String, UndecidedShoppingCustomerRestrictionModel>
{
 
  public static final String KEY_DESCRIPTION_TEXT = "type.CategoryPageCustomerWaitingTooLong.description.text";
 
  @Override public String get(final UndecidedShoppingCustomerRestrictionModel pModel)
  {
    return Localization.getLocalizedString(KEY_DESCRIPTION_TEXT,
        new Object[] { pModel.getTimeToWaitInMinutes()});
  }
 
  @Override public void set(final UndecidedShoppingCustomerRestrictionModel pUndecidedShoppingCustomerRestrictionModel,
      final String s)
  {
    throw new UnsupportedOperationException();
  }
}

Localisation

We add the following lines to the resources/localization/cmstraining-locales_en.properties:

type.CategoryPageCustomerWaitingTooLong.name=Restriction for customers waiting too long
type.CategoryPageCustomerWaitingTooLong.timeToWaitInMinutes.name=Time to wait (min.)type.CategoryPageCustomerWaitingTooLong.description.text=Shows a component or page if a shopping customer has been waiting for more than {0} minutes

Cockpit/SmartEdit Configuration

For our new restriction type we use the default cockpit or SmartEdit configuration.

Evaluator

The model contains the configuration of the new restriction. But this isn't enough, we need a bean which takes this model and calculates if the CMS component must be visible or not:

package org.areco.ecommerce.cmstraining.restrictions;
 
import de.hybris.platform.cms2.servicelayer.data.RestrictionData;
import de.hybris.platform.cms2.servicelayer.services.evaluator.CMSRestrictionEvaluator;
import de.hybris.platform.core.model.order.AbstractOrderEntryModel;
import de.hybris.platform.core.model.order.CartModel;
import de.hybris.platform.order.CartService;
import java.util.Calendar;
import java.util.Date;
import org.apache.log4j.Logger;
import org.areco.ecommerce.cmstraining.model.UndecidedShoppingCustomerRestrictionModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class UndecidedShoppingCustomerRestrictionEvaluator implements CMSRestrictionEvaluator<UndecidedShoppingCustomerRestrictionModel>
{
  /*
    Logger of this class.
   */
  private static final Logger LOG = Logger.getLogger(UndecidedShoppingCustomerRestrictionEvaluator.class);
 
  @Autowired
  private CartService cartService;
 
  @Override public boolean evaluate(final UndecidedShoppingCustomerRestrictionModel pUndecidedShoppingCustomerRestrictionModel,
      final RestrictionData pRestrictionData)
  {
    CartModel cart = cartService.getSessionCart();
    if (cart.getEntries().isEmpty()) {
      return false;
    } else {
      //It is safe to use dates, because all the entries have the same time zone.
      Date newestModifiedTime = null;
      for (AbstractOrderEntryModel anEntry : cart.getEntries()) {
        if (newestModifiedTime == null || newestModifiedTime.compareTo(anEntry.getModifiedtime()) > 0)
        {
          newestModifiedTime = anEntry.getModifiedtime();
        }
      }
      Calendar now = Calendar.getInstance();
 
      Calendar timeLimit = Calendar.getInstance();
      timeLimit.setTime(newestModifiedTime);
      timeLimit.set(Calendar.MINUTE, pUndecidedShoppingCustomerRestrictionModel.getTimeToWaitInMinutes());
      final boolean resutlEvaluation = now.after(timeLimit);
      if (LOG.isDebugEnabled())
      {
        LOG.debug("Result of evaluation of " + pUndecidedShoppingCustomerRestrictionModel.getUid() + ": " + resutlEvaluation);
      }
      return resutlEvaluation;
    }
  }
}

We create a mapping between the restriction type and the evaluator. This mapping is loaded automatically by the default implementation of the CMSRestrictionService:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
 
    <!-- Component-scan for this extension -->
    <context:component-scan base-package="org.areco.ecommerce.cmstraining" />
 
    <bean id="cmsUserGroupRestrictionEvaluatorMapping" class="de.hybris.platform.cms2.servicelayer.services.evaluator.CMSRestrictionEvaluatorMapping">
        <property name="restrictionTypeCode" value="UndecidedShoppingCustomerRestriction" />
        <property name="restrictionEvaluator" ref="undecidedShoppingCustomerRestrictionEvaluator" />
    </bean>
</beans>

Now we can update the system and start the server.

Page Configuration

Each CMS Page has a list of applicable restriction types. We update this list of the type of page which is going to have our new restrictions:

INSERT_UPDATE CmsPageType;code[unique=true];restrictionTypes(code)[mode=append]
;ProductPage;UndecidedShoppingCustomerRestriction

Testing

The following section refers to the WCMS Cockpit. This extension will be replaced by SmartEdit and is deprecated since SAP Hybris 6.7. SmartEdit will support the old features and new ones.

In the next example the sample data of accelerator B2C is used.

We log in into cmscockpit and we got to a page with the productPageTemplate. We are going to use the “Product List” page.

In the slot “Header Bottom”, we create a new paragraph component with the text “ If you need advise on any of our products, you could call our hotline. We are here to help you!”, we synchronize it and we check if it is visible on the page http://electronics.local:9001/yacceleratorstorefront/electronics/en/Open-Catalogue/Components/Power-Supplies/c/816

New CMS restriction test parragraph

Now we create a new CategoryPageCustomerWaitingTooLong restriction with two minutes for the parragraph component.

Finally we can test our restriction:

The message is hidden
After two minutes:

The message is shown

You can download the sourcecode of the extension here.

–Based on Hybris 6.4

Discussion

sunil p, 2020/09/13 07:00

In *core-spring.xml file below tag need to be specified (for the RestrictionEvaluator by injecting cartService) <bean id=“undecidedShoppingCustomerRestrictionEvaluator” class=“com.kb.core.restrictions.UndecidedShoppingCustomerRestrictionEvaluator”> <property name=“cartService” ref=“cartService” /> </bean>

Diana Vera, 2020/10/01 17:53

Hi, how can I add a custom restriction attribute to the editor area in smartedit? You created a type with 2 attributes (string and integer), what if one of those attributes were:

<attribute qualifier=“myAttribute” type=“myCustomType”>

Thanks!

Enter your comment. Wiki syntax is allowed: