0

I would like to put an Attribute called "Volume" to my Magento Online Shop. Products should be sorted by this attribute and it should be filterable through the layered navigation. since text attributes are not filterable in layered navigation and multi select fields not sortable i actually dont know how to acomplish that. any idea maybe ? i was thinking about a price attribute but then it would be there in EUROs not in Liters for example...

  • Dropdown is the only type allowing filtering as well as sorting iirc. Any reason for not using it? – pong Nov 20 '16 at 16:50
  • i tried it but i cant sort it realy nummerical. its alphanummerical. for example when i got values like 1 2 4 100 1000 it sorts it like 1 100 1000 2 4 instead of 1 2 4 100 1000. Is there a fix for that bug for dropdown attributes ? i allready fixed that for a text attribute but is there a fix for dropdown too ? – Magneto23 Nov 20 '16 at 17:12
1

You can override the order in an observer and provide SQL that takes numeric values into account. This is some kind of a workaround since Magento doesn't allow for elegant sort order modification.

config.xml

Observer definition listening to event catalog_product_collection_load_before – i.e. last one before query gets rendered:

<config>
    <frontend>
        <events>
            <catalog_product_collection_load_before>
                <observers>
                    <namespace_catalog_product_collection_load_before>
                        <type>singleton</type>
                        <class>namespace_module/observer</class>
                        <method>setSortOrder</method>
                    </namespace_catalog_product_collection_load_before>
                </observers>
            </catalog_product_collection_load_before>

Observer.php

public function setSortOrder (Varien_Event_Observer $observer)
{
    $numericallySortedAttributeCode = 'test_attribute';

    $request = Mage::app()->getRequest();
    $order = $request->getParam ( 'order' );

    if ( $order == $numericallySortedAttributeCode )
    {
        $dir = strtoupper ( $request->getParam ( 'dir' ) );
        $dir = in_array ( $dir, array ( Varien_Db_Select::SQL_ASC, Varien_Db_Select::SQL_DESC ) ) ? $dir : 'DESC' ;

        $observer
            ->getCollection()
            ->getSelect()
            ->reset ( Zend_Db_Select::ORDER )
            ->order ( new Zend_Db_Expr ( 'CAST(`' . $numericallySortedAttributeCode . '` AS SIGNED) ' . $dir ) )
        ;
    }
}

This is tested with attribute values of type integer. If you use alphanumeric values like '30 fl.oz.', you might have to modify the SQL CAST. You probably also wanna edit the attribute code and the condition of when to apply the sort modification – above code obviously only works if the sort order in question is sent explicitly as a request parameter aka not used implicitly as default sort order.

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.