Magento Sort Attribute by Decimal not Alphanumerically

后端 未结 6 1696
刺人心
刺人心 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:29

    So I found a thread on this on their documentation, and apparently it's a known pain point for the product. The best solution I found was to override the ORDER BY for the query by calling a primitive method of the Collection class, here's the example they give:

    $_productCollection = Mage::getModel('catalog/product')->getCollection();
    
    $_productCollection->setOrder(array('cm_brand', 'name', 'cm_length'), 'asc');
    $_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
    $_productCollection->getSelect()->order(array('cm_brand ASC', 'name ASC', 'CAST(`cm_length` AS SIGNED) ASC'));
    

    Based on your example with only one sorting column, I would think you could go with:

    $_productCollection = Mage::getModel('catalog/product')->getCollection();
    $_productCollection->setOrder('weight', 'asc');
    $_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
    $_productCollection->getSelect()->order('CAST(`weight` AS SIGNED) ASC'));
    

提交回复
热议问题