How to add custom Sort logic for Sort By in Hybris?


Sometimes you may be required to have custom sorting logic for Sort By options in your category or products listing page. In this post, I will explain to you all the required steps. All you need is to define custom SortProvider. I'm taking the example of sort by size.
Note: This post is based on Hybris version V6.0facetSort(code) is deprecated in SolrIndexedProperty type so it might be removed from the latest release.

Define your SortProvider(sizeAttributeSortProvider) bean

First, you need to define comparator class (SizeAttributeComparator) and bean definition for the same. Now declare sortProvider(sizeAttributeSortProvider) bean as below which point to OOTB class (DefaultFacetSortProvider).
<bean id="sizeAttributeSortProvider"
      class="de.hybris.platform.solrfacetsearch.config.impl.DefaultFacetSortProvider">
    <property name="comparator" ref="sizeFacetAttributeComparator"/>
</bean>

<bean id="sizeFacetAttributeComparator"
      class="de.hybris.platform.acceleratorservices.search.comparators.FacetValueNameComparator">
    <property name="comparator" ref="sizeAttributeComparator"/>
</bean>

<bean id="sizeAttributeComparator"
      class="de.hybris.platform.acceleratorservices.search.comparators.SizeAttributeComparator">
    <property name="pattern" value="[0-9]+(\.[0-9])*"/>
    <property name="sizeSystems">
        <list>
            <value>S</value>
            <value>M</value>
            <value>L</value>
        </list>
    </property>
</bean>


public class SizeAttributeComparator implements Comparator<String>
{
    private String pattern;
    private Pattern regexPattern;
    private List<List<String>> sizeSystems;

    @Override
    public int compare(final String value1, final String value2)
    {
      //TODO: your compare logic
    }
}

Bind bean with Indexed Property

You need to give above bean reference (sizeAttributeSortProvider) to customFacetSortProviderfor respective Indexed property(size).
INSERT_UPDATE SolrIndexedProperty ; solrIndexedType(identifier)[unique=true] ; name[unique=true] ; type(code) ; sortableType(code) ; currency[default=false] ; localized[default=false] ; multiValue[default=false] ; facet[default=true] ; facetType(code) ; facetSort(code) ; priority ; visible ; useForSpellchecking[default=false] ; useForAutocomplete[default=false] ; fieldValueProvider                      ; facetDisplayNameProvider                      ; customFacetSortProvider       ; topValuesProvider        ; rangeSets(name)     
                                  ; $solrIndexedType                         ; size              ; string     ;                    ;                         ; true                     ;                           ;                     ; MultiSelectOr   ; Custom          ; 2000     ; true    ;                                    ;                                   ; optionalModelPropertyFieldValueProvider ;                                               ; sizeAttributeSortProvider     ;   
Let me put more light on facetSort(code) which is the type of SolrIndexedPropertyFacetSort. There are three possible values (Count,Alpha and Custom)
This enumeration defines sorting order of the facet values within a facet.
  • Count: The facet values can be sorted by facet value count, with the highest count first.
  • Alpha: Sorted lexically by facet display name (A to Z)
  • Custom: A custom sort must be provided via SolrIndexedProperty.customFacetSortProvider.
So we have used Custom above as facetSort.

Define the custom SolrSort

INSERT_UPDATE SolrSort ; &sortRefID ; indexedType(identifier)[unique=true] ; code[unique=true] ; useBoost
                       ; sortRef1   ; $solrIndexedType                     ; size              ; false 

Define the custom sort fields

INSERT_UPDATE SolrSortField ; sort(indexedType(identifier),code)[unique=true] ; fieldName[unique=true] ; ascending[unique=true]
                            ; $solrIndexedType:size                              ; size                   ; true 

Add custom sort to indexed type Product

INSERT_UPDATE SolrIndexedType ; identifier[unique=true] ; type(code) ; variant ; sorts(&sortRefID)                                    
                              ; $solrIndexedType        ; Product    ; false   ; sortRef1
Here you need to bind all SolrSort using its references (sortRef1,sortRef2) otherwise this override sorts filed and you might have only one SortBy option. Or you can use merge option.
Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 15+ years experience in SAP Commerce Cloud (Hybris), SAP Spartacus. Extensive experience in SAP Hybris development, third-party integrations, project architecture and design... Read more

Comments

Post a Comment

Popular posts from this blog

Hybris flexible search query examples

How to Install temporary Hybris license?

How to remove or update all data records in Hybris?