2
votes

I'm facing a problem with attribute sorting in magento. I have created a drop down attribute for a product that I'm using to sort the category list. The problem I encounter is that the sorting is made by the values not by the order that I have given them.

Example: I have the attribute color with the options: 1. Red
2. Black
3. Green

When I choose to sort by color the order is Black, Green, Red, but i need it to be Red, Black, Green.

I found a patch that theoretically will solve the problem but i cant make it work. https://gist.github.com/colinmollenhour/4082426

My magento version is 1.7.0.2.

Thanks.

1

1 Answers

2
votes

The patch requires a few modifications to work. The code, however, is better be placed in a model rewrite of a different class.

The solution that worked for me was to override Mage_Eav_Model_Entity_Attribute_Source_Table::addValueSortToCollection and add the following code to the end of the function:

        $attribute = $this->getAttribute();
        $order = $attribute->getAttributeCode();
        $dir = strtoupper($dir);
        $collection->getSelect()->reset(Zend_Db_Select::ORDER);
        $collection->getSelect()
            ->joinLeft('eav_attribute_option AS eao', "eao.option_id=IF({$order}_t2.value_id > 0, {$order}_t2.value, {$order}_t1.value)", array("sort_order" => 'eao.option_id'))
                ->order(new Zend_Db_Expr('eao.sort_order '.$dir));

One has to be aware that this will replace value sorting for option_id sorting for every catalog collection and for every attribute, whose source model class is eav/entity_attribute_source_table