I'm getting so mad that it literally hurts with a problem related to attribute option collection on Magento 1.8.1
I have a dropdown type attribute populated with dozens of values and frequently updated with new values, wich is used for layered navigation purposes.
What im trying to do is actually change de default behaviour and sort NUMERICALLY all theese options if "position" is not set.

Magento sorts my values like this (alphabetically)

100 Watt
1000 Watt
1200 Watt
200 Watt
300 Watt

I need to sort them this way

100 Watt
200 Watt
300 Watt
1000 Watt
1200 Watt

I tried to edit class Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection in any way. Maybe i'm pretty close adding in public function setStoreFilter

    $this->getSelect()->order(new Zend_Db_Expr("CAST('value' AS SIGNED) ASC"));

Hope someone can help me understand how to achieve this.

  • I think you're not too far from the truth with the CAST. Although you would probably need to strip the "Watt" text with REPLACE dev.mysql.com/doc/refman/5.0/en/… before you try to cast it. Aug 27 '15 at 9:19
  • Maybe try the sorting with LPAD: dba.stackexchange.com/questions/21156/… Aug 27 '15 at 9:25
  • nope, my best guess is it's overwritten * somewhere * to sort in another way so try to reset and add your sort as low in the code as possible (meaning near where it's used to output the list)
    – Sander Mangel
    Aug 27 '15 at 9:43
  • Hi and thanks for the answers. I think REPLACE and LPAD are both good for my case, but the code i posted doesn't affect the output of the attribute option collection.
    – Emir M.
    Aug 27 '15 at 9:45
  • Building on @SanderMangel and OP's last comment, have a look at this answer which copes with the issue of multiple sort order overrides: magento.stackexchange.com/a/146643/22860
    – pong
    Nov 20 '16 at 23:45

Did you use the position / order column in the edit attribute form in backend? Why not set the sort order preference there

  • I want to implement this as default behaviour, as i have many attributes where new values are added quite frequently.
    – Emir M.
    May 17 '17 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.