cancel
Showing results for 
Search instead for 
Did you mean: 

Numerical sorting for attributes on product list/grid

SOLVED

Numerical sorting for attributes on product list/grid

Allright, I am going slightly insane.

 

Everywhere I see remarks concerning bugs in Magento as listing of products is not numerical.

 

So far, I have this sorted out for the layered navigation so I have it thus:

 

3.2, ,3.7, 4.2 etcetc

 

But in the product list or grid it just doesn't happen, whatever solution I find online. Per product group I have various options to select on which to sort, almost all based on numerical options.

 

Even though I change the sort order in the back-end, no change.

 

I am using Magento 1.9.2.4 and Amasty's extension for Improved Layer Navigation.

 

Anyone?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Numerical sorting for attributes on product list/grid

Nobody else?

 

It is getting stranger. A different product group with a different attribute set hád the same issue but this is now resolved by editing list.php - added the bold printed code after this line:

 

$this->_productCollection = $layer->getProductCollection();

        // Start of Code to force Magento to numerically sort decimal attributes rather than alphabetically

        $filterAttribute = $this->getRequest()->getParam('order');
        $filterAttributeDir = $this->getRequest()->getParam('dir');
        $attributeType = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $filterAttribute)->getFrontendClass();

        // If a Sort By option is selected on category page and attribute has frontend_class = validate-number or validate-digits
        // then CAST the attribute values as signed integers

        if (isset($filterAttribute) && ($attributeType == 'validate-digits' || $attributeType == 'validate-number')) {

            $this->_productCollection->getSelect()->reset(Zend_Db_Select:Smiley SurprisedRDER);
            $this->_productCollection->getSelect()->order('CAST(`' . $filterAttribute . '` AS SIGNED) ' . $filterAttributeDir . "'");

        }

        // End of code to force Magento to numerically sort....

 

 

However... the particular attribute of the other productgroup was NOT validated at all!

 

Now I still had the original issue as shown. In addition I tried in desperation to just add a 0 - ie 01, 02, 03 etc. And now it works...

 

I stil flabbergasted by this obvious issue and cannot fathom why Magento has not dealt with this. Too much online complaining about this to not have this addressed with something better than workarounds!

 

View solution in original post

10 REPLIES 10

Re: Numerical sorting for attributes on product list/grid

Hi @brakoops

First of all, thank you for using our extension!

 

Can you please attach a screenshot of what you need to sort? It will clarify your question as not it is not understood whether you mean a list of attributes names on the list or products.

 

 

Was my answer helpful? You can accept it as a solution.
230+ professional extensions for M1 & M2 with free lifetime updates!

Re: Numerical sorting for attributes on product list/grid

screenshot.jpg

 

 

In the shop per you can see the numerical order of the attributes (1->). As I set the default sorting (2 ->)on the product list and grid at the exact same attribute, I expected to see the 3.2 version - you see now the 10.0 version of the product

Re: Numerical sorting for attributes on product list/grid

@brakoops

Unfortunately, the screenshot seems to be broken. But from your description I can conclude that you face this problem because  Magento compare attributes as strings, not as numbers.  Try to create and use a new attribute, type "Decimal" to avoid it

Was my answer helpful? You can accept it as a solution.
230+ professional extensions for M1 & M2 with free lifetime updates!

Re: Numerical sorting for attributes on product list/grid

unfortunately, I don't understand what you mean?

 

Where do I type decimal to avoid this problem?

Re: Numerical sorting for attributes on product list/grid

@brakoops

please see the screenshot:

 

1e152-clip-65kb.png

Was my answer helpful? You can accept it as a solution.
230+ professional extensions for M1 & M2 with free lifetime updates!

Re: Numerical sorting for attributes on product list/grid

as with my screenshot, I can't view yours Smiley Wink

Re: Numerical sorting for attributes on product list/grid

You can navigate to New product Attribute -> Attribute Properties -> Catalog Input Type for Store Owner

Was my answer helpful? You can accept it as a solution.
230+ professional extensions for M1 & M2 with free lifetime updates!

Re: Numerical sorting for attributes on product list/grid

unfortunately, this does not work.

Re: Numerical sorting for attributes on product list/grid

Nobody else?

 

It is getting stranger. A different product group with a different attribute set hád the same issue but this is now resolved by editing list.php - added the bold printed code after this line:

 

$this->_productCollection = $layer->getProductCollection();

        // Start of Code to force Magento to numerically sort decimal attributes rather than alphabetically

        $filterAttribute = $this->getRequest()->getParam('order');
        $filterAttributeDir = $this->getRequest()->getParam('dir');
        $attributeType = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $filterAttribute)->getFrontendClass();

        // If a Sort By option is selected on category page and attribute has frontend_class = validate-number or validate-digits
        // then CAST the attribute values as signed integers

        if (isset($filterAttribute) && ($attributeType == 'validate-digits' || $attributeType == 'validate-number')) {

            $this->_productCollection->getSelect()->reset(Zend_Db_Select:Smiley SurprisedRDER);
            $this->_productCollection->getSelect()->order('CAST(`' . $filterAttribute . '` AS SIGNED) ' . $filterAttributeDir . "'");

        }

        // End of code to force Magento to numerically sort....

 

 

However... the particular attribute of the other productgroup was NOT validated at all!

 

Now I still had the original issue as shown. In addition I tried in desperation to just add a 0 - ie 01, 02, 03 etc. And now it works...

 

I stil flabbergasted by this obvious issue and cannot fathom why Magento has not dealt with this. Too much online complaining about this to not have this addressed with something better than workarounds!