Search code examples
javaintershopisml

How can I specify the opposite of a customer segment


In components you don't have an exclusion segment: enter image description here

My subject is to create the opposite of a customer segment.
If I have a customer segment DEVELOPER then I would like to create EXCEPT_DEVELOPER ("Everyone can see this component except DEVELOPER").

We want to override/enhance the core method of Intershop which retrieve the assignement.
The goal to check if the user is in the EXCEPT customer segment dynamically.

...
...
forEach(CustomerSegment csItem : customerSegmentList) { 
   if(csItem.contains("EXCEPT")){ //EXCEPT~DEVELOPER
      String customerSegmentToExcept = csItem.split("~")[1]; //DEVELOPER
      if(currentUser.isIn(customerSegmentToExcept)) //current user is in customer segment DEVELOPER
         return "Don't display component";
   }
}
...
...
return "Display component";

What do you think ? Do you have some advice or another method to achieve this please ? Thank you !


Solution

  • The way to override or enhance the pagelet assignment lookup is to provide a custom PageletVisibilityFilter. In 2016 I gave a speech on that topic at the Intershop Developer Conference.

    In general, they are wired in a position/priority based list of out-of-the-box filters and they are executed from highest to lowest priority when a content-entry-point (e.g slot, placeholder, page, include) is rendered.

    You need to implement that interface:

    /**
     * A pagelet visibility filter is a class that decides if a pagelet 
     * is visible to the end user or not. The <code>decide</code> method is invoked 
     * to get the decision from the implementation. 
     */
    public interface PageletVisibilityFilter
    {
        /**
         * Returns the decision if a pagelet should be filtered or not.
         * 
         * @param pagelet the pagelet which visibility is tested
         * 
         * @return the decision for the pagelet visibility
         * 
         * @see PageletVisibilityFilterReply
         */
        public PageletVisibilityFilterReply decide(Pagelet pagelet);
    }
    

    And register that implementation with the component framework. A quick look into app_sf_responsive revealed the place where to instantiate and wire your instance.

    <instance name="pageletABTestGroupVisibilityFilter" with="PageletABTestGroupVisibilityFilter" />
    <fulfill requirement="pageletVisibilityFilter" of="pageletVisibilityFilterCtnr" with="pageletABTestGroupVisibilityFilter" />
    

    Be prepared to override the original out-of-the-box filter for customer segments to express that complement logic your're trying to achieve.