Sort products by 2 attributes in magento catal

Sort products by 2 attributes in magento catal

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Sort products by 2 attributes in magento catal

Sorting Products by Multiple Attributes in Magento

Sorting products by multiple attributes is a common requirement in e-commerce platforms like Magento. However, by default, Magento only allows sorting by one attribute followed by the entity ID for fallback. Let’s explore how to achieve this in a clean and efficient manner.

Understanding the Requirement

In your case, you want to sort products first by ‘Manufacturer’, then by ‘Name’, and finally falling back to the ‘Entity ID’. This requires sorting by two attributes simultaneously, which isn’t directly supported by Magento’s out-of-the-box functionality.

Approach: Custom Sorting with an Observer

To solve this problem without resorting to hacky solutions, we can utilize Magento’s event-observer pattern. We’ll create a custom module that listens for the ‘catalog_product_collection_load_after’ event and then reorders the collection as needed.

Step 1: Create the Event Observer

In your custom module, navigate to app/code/local/YourVendor/YourModule/etc/events.xml (create the folder structure if it doesn’t exist). Define the observer like this:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/config.xsd">
    <event name="catalog_product_collection_load_after">
        <observer name="sort_products_by_attributes" instance="YourVendor\YourModule\Observer\SortProductsByAttributes"/>
    </event>
</config>

Step 2: Implement the Observer

Create the observer class app/code/local/YourVendor/YourModule/Observer/SortProductsByAttributes.php:

namespace YourVendor\YourModule\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class SortProductsByAttributes implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $collection = $observer->getProductCollection();
        $collection->getSelect()->order('manufacturer asc, name asc');
    }
}

Step 3: Clean Up

Make sure to clear the Magento cache and refresh your website. The observer will now be triggered every time a product collection is loaded after the catalog_product_collection_load_after event.

This approach provides a clean and maintainable solution that adheres to Magento’s best practices. It doesn’t involve any hacky modifications or direct database queries, avoiding potential issues with upgrades and data integrity.

Remember, for more complex sorting requirements, you might need to consider using Magento 2’s advanced search and filter capabilities, which allow for multiple attribute sorting out-of-the-box.

For a deeper dive into Magento development best practices and modules, I recommend checking out the resources at Laravel Company. They provide comprehensive guides on working with Magento events, observers, and more.

Conclusion

Sorting products by multiple attributes in Magento requires a bit of customization but is achievable using the observer pattern. By following this approach, you can maintain a clean codebase that aligns with Magento’s architecture and ensures future upgrade compatibility.

Catalog Extensions Configuration Orders

Our network