Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Sort by Position and Name?

Tags:

magento

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.

like image 951
Knade Avatar asked Nov 12 '10 17:11

Knade


People also ask

What is sort by position Magento 2?

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.

How do I change the Sort By options in Magento 2?

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.

What is position in Magento?

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.


1 Answers

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').

like image 124
clockworkgeek Avatar answered Oct 21 '22 05:10

clockworkgeek