Sorting Magento Products by Attribute Position
In this blog post, we’ll walk through how to sort your products in Magento based on the position of their attributes, rather than just alphabetically.
The Current Issue: Alphabetical Order
Let’s say you have an attribute called “Color” with the following values:
- Green (Position 1)
- Blue (Position 2)
- Red (Position 3)
Currently, when you filter your products by color in the frontend, they are displayed in alphabetical order: Blue, Green, Red. However, you want them to appear in the order defined by their positions: Green, Blue, Red.
Where to Start: Layered Navigation
The Layered Navigation module is responsible for displaying product filters like this one. To modify the sorting behavior, we need to make changes in a couple of places.
Step 1: Modify the Catalog Search Controller
Open the app/code/core/Mage/Catalog/controllers/AdvancedController.php file and locate the _getProductList() method around line 650. This function is responsible for fetching products based on filter criteria.
Find this line:
$products = $collection->load();
Replace it with:
$products = $this->_prepareProductCollection($collection, $requestedField);
Then, add the following method at the end of the class:
protected function _prepareProductCollection($collection, $requestedField)
{
if ($requestedField == 'position') {
$collection->getSelect()->order('main_table.' . $requestedField . ' ASC');
} else {
$collection->setOrder($requestedField, 'asc');
}
return $collection;
}
This changes the sorting behavior. When a user filters by “position”, it will now sort the products based on their position attribute instead of alphabetically.
Step 2: Modify the Catalog Search Result Block
Next, open app/code/core/Mage/Catalog/Block/Layer/View.php and find the _getProductList() method around line 490. Replace this line:
$products = $this->getLayer()->getProductCollection();
With:
$products = $this->getLayer()->getProductCollection()
->addAttributeToFilter('entity_id', array('in' => $this->getCurrentCategory()->getAllChildrenIds()))
->addAttributeToFilter($this->_getFilterableAttributes())
->setCurPage(1)->setOrder('position', 'asc')->load();
This ensures that the products are sorted by their position attribute when a user filters by position.
Step 3: Clear Cache and Reindex
After making these changes, you’ll need to clear the Magento cache and reindex your data. Go to System > Cache Management in the admin panel and select all checkboxes under “Type” to flush everything. Then, go to Index Management under System and click Reindex All.
Conclusion
By modifying these two files, we’ve successfully changed the default sorting behavior of the layered navigation filter for our custom attribute.
Remember, since you’re working with core Magento files, it’s always a good practice to make changes in a local development environment or use a version control system. If you’re using Magento 2, the process involves similar steps but uses different file paths and namespaces.
For more advanced customizations like this, consider checking out our guide on Magento Customization Best Practices for tips on keeping your store maintainable and secure.