How to add or update Sort by Options in the Hybris?

Case1: Update/remove existing Sort by option

If you are looking for update/remove existing Sort by option then you can do it by simply through hmc/backoffice/impex.
  • Go to back-office SolrIndexedType and open your Indexed type
  • Go to Solr Sorts tab: Here you can see the list of Sort By option which you can update/delete

Case2: Add a custom Sort by option without custom sort logic
In case if you want to add new custom Sort by option without any custom sorting logic then you can do it without changing any code. Let's assume I want a new sort by option based on 
isPromotedProduct (boolean) of the Product type. Just follow the below steps...

1) Define SolrIndexedProperty

You need to have SolrIndexedProperty for your attribute and you need to do Solr indexing to reflect its value in the Solr server. Here, I am assuming you know this part very well or you may already have SolrIndexedProperty defined.

INSERT_UPDATE SolrIndexedProperty ; solrIndexedType(identifier)[unique=true] ; name[unique=true] ; type(code) ; sortableType(code) ; currency[default=false] ; localized[default=false] ; multiValue[default=false] ; useForSpellchecking[default=false] ; useForAutocomplete[default=false] ; fieldValueProvider               ; valueProviderParameter
                                  ; $solrIndexedType                         ; isPromotedProduct ; boolean    ;                    ;                         ;                          ;                           ;                                    ;                                   ; productIsPromotedProductProvider ;                                       

2) Define SolrSort

Now you need to define an instance of SolrSort so that the same value you can see on the storefront dropdown. Refer below example how to define it.

INSERT_UPDATE SolrSort ; indexedType(identifier)[unique=true] ; code[unique=true] ; name[lang=$lang]  ; useBoost  
                       ; $solrIndexedType                     ; isPromotedProduct ; Promoted Products ; false   

3) Define SolrSortField

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

Hybris internally bind newly created SolrSort to the related SolrIndexedType. So here you are almost done. Now check on the storefront whether you got the new Sort By option or not.

If you are not able to see newly created sort by the option on the storefront I would recommend to go and check the sorts filed of the SolrIndexedType in the back office. If created SolrSort option is not there you can simply add it. This also can be done through Imex like below. Please make sure to add all sort options there.

INSERT_UPDATE SolrIndexedType ; identifier[unique=true] ; type(code) ; variant ; sorts(code)                                    
                              ; $solrIndexedType        ; Product    ; false   ; solrSortCode1,solrSortCode2,isPromotedProduct

Case3: Sort by option with custom sort logic

Refer how to add custom sort logic for a Sort By Option in the Hybris?

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


Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

How to Install temporary Hybris license?