Magento按十进制排序属性而非字母数字

Ada*_*m B 11 php sql sorting magento

所以我用谷歌搜索疯狂试图找到一个解决这个问题的方法,这个问题实际上是正常运行的,但却空手而归.

在类别页面上使用"排序依据"功能时,可以按属性(容量,重量等)对产品进行排序.Magento这样排序是因为它认为数字是一个文本字符串:

产品A:10kg

产品B:11kg

产品C:15kg

产品D:9kg

而它应该类似于:

产品D:9kg

产品A:10kg

产品B:11kg

产品C:15kg

环顾四周,好像人建议改变backend_type为十进制和frontend_input价格在eav_attribute表,你想用数字排序的属性.但是,这不仅实际上不起作用,而且它改变了数字的格式,在它前面有一个美元($)符号,因为我们在产品页面上显示实际属性值,在使用它之上排序依据,我们不能解决问题.

我正在试图弄清楚getSortOrder()方法是如何工作的,但看起来这个功能非常深入嵌入,所以我很难找到这个bug的解决方法.

任何帮助表示赞赏!

编辑:

对于那些希望将来解决这个问题的人来说,这是我提出的解决方案:

您需要覆盖List.php中存储在app/Code/Mage/core/catalog/block/product/list.php中的函数_getProductCollection().
将文件复制到app/code/Mage/local/catalog/block/product/list.php,这样就不会编辑核心文件.

然后在下面说:

$this->_productCollection = $layer->getProductCollection();
Run Code Online (Sandbox Code Playgroud)

输入以下代码:

        // 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::ORDER);
            $this->_productCollection->getSelect()->order('CAST(`' . $filterAttribute . '` AS SIGNED) ' . $filterAttributeDir . "'");

        }

        // End of code to force Magento to numerically sort....
Run Code Online (Sandbox Code Playgroud)

现在,如果您在管理面板中对存储所有者进行输入验证,则将属性设置为十进制数或整数: 属性类型 然后,此代码将重置产品集合上的排序顺序,然后将其作为有符号整数进行CAST,以便按数字排序,而不是按字母数字排序.

希望有人帮助!

Ant*_*ony 12

所以我在他们的文档上找到了一个关于此的线程,显然这是该产品的一个已知的痛点.我找到的最佳解决方案是ORDER BY通过调用类的基本方法来覆盖查询Collection,这里是他们提供的示例:

$_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'));
Run Code Online (Sandbox Code Playgroud)

根据您的示例只有一个排序列,我认为您可以使用:

$_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'));
Run Code Online (Sandbox Code Playgroud)