Magento - Changing attribute sorting

Magento - Changing attribute sorting

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Changing attribute sorting

Changing Attribute Sorting in Magento

Hello developers! Today we’re going to tackle a common issue that many of us encounter when working with Magento - changing the sorting order for product attributes.

The Challenge

Our user wants to sort products by a specific attribute (in this case, color) but with a twist. They want to use the position value given in the “Manage Attributes” section as the primary sort key, but only if there are any non-zero positions specified. If all positions are zero, they want it to fall back to sorting by the attribute name.

Where is the Sorting Applied?

In Magento, the product collection sorting is primarily applied in the Mage_Catalog_Model_Resource_Product_Collection class. You can find this file at app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php.

Here’s a snipped of the relevant code:

public function addFieldToSelect($field)
{
    $this->addFilterToMap(null, 'attribute_set_id');
    return parent::addFieldToSelect($field);
}

As you can see, Magento adds an alias for attribute_set_id to the select statement, which is used later for sorting.

Changing the Sorting Behavior

To achieve our user’s desired behavior, we need to modify how Magento sorts the product collection when using a specific attribute as the sort key. We’ll do this by creating a custom extension that extends the Mage_Catalog_Model_Resource_Product_Collection class and overriding the addFieldToFilter() method.

Here’s an example of what the code might look like:

class Custom_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
    public function addFieldToFilter($field, $condition = null)
    {
        if ($field == 'attribute_set_id' && is_array($condition) && isset($condition['eq'])) {
            // Custom sorting logic here
            if (!empty($condition['eq'])) {
                foreach ($condition['eq'] as $setId => $values) {
                    // Implement your custom sorting logic based on the attribute's position value
                    // Example: sort by the 'position' field in ascending order when non-zero, and then by name
                }
            } else {
                // Fall back to default sorting (by set_id)
            }
        }

        return parent::addFieldToFilter($field, $condition);
    }
}

In this example, we’re checking if the field being filtered is attribute_set_id and if it’s being used in an ‘eq’ condition. If so, we can implement our custom sorting logic.

Conclusion

Changing the attribute sorting order in Magento requires a deep understanding of how the product collection works, as well as some creative coding to override the default behavior.

Remember that overriding core Magento files is generally not recommended due to upgrade issues. A better approach would be to create an extension as shown above, or use Magento’s built-in catalog rule system to achieve complex product sorting requirements.

Don’t forget to check out our guide on Magento Extension Development for a comprehensive walkthrough of the process.

Catalog Extensions Orders Errors

Our network