Magento Sort Attribute by Decimal not Alphanumerically

后端 未结 6 1689
刺人心
刺人心 2021-02-05 22:08

So I\'ve Googled like crazy to try and find a solution to this problem that actually works properly but have come up empty handed.

When using the Sort By function on a

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 22:16

    In response to Adam B's post.

    My situation: I needed to sort products by a custom Magento (1.8.1.0) attribute with a numerical value. However, Magento was sorting like:

    1 , 11 , 123 ,2 ,234, 3 (alphanumerical) instead of: 1, 2, 3, 11, 123, 234 (numerical)

    I honestly don't get why this is not working out of the box but I got this working with the following adaption:

    $this->_productCollection->getSelect()->order(new Zend_Db_Expr("CAST( ".$filterAttribute." AS SIGNED ) ".$filterAttributeDir));
    

    Hope it's to any use for someone.

提交回复
热议问题