Cert++
Close

Applying refinements, sorting rules and inventory filters to a ProductSearchModel

Key Takeaway

On a ProductSearchModel, setRefinementValues() replaces an attribute's existing refinement values, while addRefinementValues() adds to them. To refine by several values, pass them as one pipe-separated string, such as 'Cotton|Linen', and results match any of them.

ProductSearchModel (dw.catalog.ProductSearchModel) is the B2C Commerce Script API class that runs a product search and holds its result and refinement options. You set criteria on it, such as a category, refinements, a sorting rule and inventory lists. Then you call search() and read the hits and refinements. Merchants define the refinement definitions and sorting rules in Business Manager, and your code applies them to each search.

How to run a product search
  1. Create the model with new ProductSearchModel().
  2. Set criteria, such as setSearchPhrase() or setCategoryID().
  3. Call search().
  4. Read the results with getProductSearchHits() and the refinement options with getRefinements().

search() returns a SearchStatus. A status of SUCCESSFUL or LIMITED means the search worked. Calling search() on a model with no search term and no filter returns EMPTY_QUERY.

getProductSearchHits() can include hits for products that were removed or taken offline since the last index update. Check each hit before you call ProductSearchHit.getProduct(). getProducts() skips those products, but getProducts() is deprecated because loading a Product for each result is very expensive.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers. 796 members and growing.