Category sorting in Episerver Commerce
Recently Episerver released Commerce 11. It contains a new feature - a product sorting. But unfortunately, there is no way to sort categories.
It is quite a standard requirement to sort product categories in a certain order. Customers might want to promote some categories more than others and display those on the top in the navigation or category listing. While Episerver doesn't have support for this yet, there is a solution.
At first, add a content area on your category type (or category base type if you have it). Limit content area items to catalog nodes, that only categories can be added to this content area.
[Display("Sub-category order")]
[UIHint(UIHintCommerce.CatalogNode)]
public virtual ContentArea SubCategoryOrder { get; set; }
Then create a method which loads categories in proper order.
public virtual IEnumerable<CategoryContent> GetSubCategories(CategoryContent parentCategory)
{
var children = _contentLoader
.GetChildren<CategoryContent>(parentCategory.ContentLink);
var orderedCategories = parentCategory
.SubCategoryOrder
?.Items
.Select(x => x.ContentLink)
.Select(
link => children.FirstOrDefault(c => c.ContentLink.Equals(link, ignoreVersion: false)))
.Where(x => x != null)
.ToList() ?? new List<CategoryContent>();
var restCategories = children.Except(orderedCategories);
return orderedCategories.Union(restCategories);
}
Here I am loading all child categories of the parent category. Then extract content links from the content area items and retrieve corresponding child categories by these links. Also, ignore the links which do not have a matching child category by filtering items which are null. The loaded child categories should be in the same order as added to the content area.
Next, retrieve the unordered categories from the all child category list. And finally, create a common list by placing ordered categories in the front and unordered one at the end.
With this solution, editors now can add categories which they want on the top in the new content area. They can add these categories in a certain order. They also can add all categories and order those as needed.
While you can use this solution, it would be better to have a native Episerver support for this. I hope that Episerver will add it soon :).