Is it possible to sort by A the position and then B these products alphabetically shown.
I have new products and sale products coming into a category and they show all the new ones first then all the sale ones.
But I need to sort this by name.
Re: Sort by Position? A product is sorted for a category with the same id by getting collection of products. For Sort by Price, Product collection of specific category first load, Set Order by Asc/Desc price for a collection and based on that for the same category Product collection will load as Price sorting.
To change the default sorting, go to Catalog > Categories, choose the needed category, and open the Display Setting tab. Then go to the Default Product Listing Sort By field and choose the needed parameter from the dropdown. Save the changes.
Every product in a category in Magento 2 store has a certain position. The position of the products defines the order in which the products are displayed on the storefront.
In admin
Go to Manage Categories, select the category, then on it's products tab give each one a position number. They will be sorted according to that order.
Programmatically
You can do this by calling a product collection's addAttributeToSort
method for each ordering.
For example, wherever you see in a template, $_category->getProductCollection()
or $_product->getCollection()
you can then add ->addAttributeToSort('position')->addAttributeToSort('name')
immediately after it.
addAttributeToSort()
also takes a direction as a second parameter so you can have either addAttributeToSort('name', 'asc')
or addAttributeToSort('name', 'desc')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With