Configuring Category Page Sorting
Control the fields used to sort category pages.
Complete the following prerequisites:
At the Content Editor
-
Open the catalog templates on the content tree.
Browse to
/sitecore/templates/Commerce/Catalog/
-
Open the
__Standard Values
content pane.Expand the
Commerce Category
item and select__Standard Values
.The
__Standard Values
content pane opens. -
Expand the
Commerce Search
section. -
Select and rank the sort fields you wish to use.
-
Save your changes.
Click Save on the Home ribbon.
The sort fields you have selected are available on all category pages.
NoteYou can use different sort fields for specific category page by modifying the
Sort Fields
field for that category. -
Note
If some of the sort fields you have selected have non-string based values, then you need to provide a strongly typed property that represents those fields. For example, to sort on a boolean product property called "onsale":
Create a new class called ExtendedProductSearchResultItem that extends CommerceProductSearchResultItem, and then add a new property called
OnSale
that is of typebool
and has anIndexField
attribute that contains the "onsale" property name in the index.RequestResponse/// <summary> /// Extends the default product search item to add custom properties /// </summary> public class ExtendedProductSearchResultItem : CommerceProductSearchResultItem { /// <summary> /// Gets or sets a value indicating whether or not a product is on sale /// </summary> [IndexField(“onsale")] public bool OnSale { get; set; } }
The type will need to be added wherever a product query is performed. For example:
-
var query = LinqHelper.CreateQuery<ExtendedProductSearchResultItem]](context, searchStringModel)
-
searchResults = searchManager.AddSearchOptionsToQuery<ExtendedProductSearchResultItem]](searchResults, searchOptions);
-
-
Extend the ProductSorter class to set the sort value. Create a new class called ExtendedProductSorter. For example:
RequestResponse/// <summary> /// Extends the default product sorter with some additional fields /// </summary> public class ExtendedProductSorter : ProductSorter { /// <summary> /// Applies sorting to a query based on details in the searchOptions /// </summary> /// <typeparam name="T">The search item type e.g. UISearchResult</typeparam> /// <param name="query">The query to apply the sort to</param> /// <param name="searchOptions">The options for sorting</param> /// <returns> /// The query with the new sort options applied /// </returns> public override IQueryable<T> ApplySorting<T>(IQueryable<T> query, CommerceSearchOptions searchOptions) { if (!string.IsNullOrEmpty(searchOptions.SortField)) { var localQuery = (IQueryable<ExtendedProductSearchResultItem>)query; var sortingFunc = this.GetSortingFunc(localQuery, searchOptions); switch (searchOptions.SortField) { case Constants.KnownSortFields.OnSale: localQuery = sortingFunc(f => f.OnSale); break default: return base.ApplySorting<T>(query, searchOptions); } return localQuery.Cast<T>(); } return query; }
-
Patch the ExtendedProductSorter class into the Commerce Server types selection by using the following snippet:
RequestResponse<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <commerceServer> <types> <type name="ISortProvider" type="Sitecore.Commerce.Connect.CommerceServer.Search.ProductSorter, Sitecore.Commerce.Connect.CommerceServer" lifetime="Singleton"> <patch:attribute name="type">MVCSite.ExtendedProductSorter, MVCSite</patch:attribute> </type> </types> </commerceServer> </sitecore> </configuration>
You will now be able to sort on your new property. To sort on a Category or Catalog property, you can use the CategorySorter or CatalogSorter class. We have also introduced CommerceCategorySearchResultItem and CommerceCatalogSearchResultItem classes to be used as base SearchResultItem classes, which contain built-in non-string properties that you may want to sort on.