因此,我疯狂地在 Google 上搜索,试图找到解决这个问题的方法,该方法实际上可以正常工作,但空手而归。
在类别页面上使用“排序方式”功能按属性(容量、重量等)对产品进行排序时。 Magento 这样排序是因为它认为数字是文本字符串:
Product A: 10kg
Product B: 11kg
Product C: 15kg
Product D: 9kg
而它应该排序为:
Product D: 9kg
Product A: 10kg
Product B: 11kg
Product C: 15kg
环顾四周,人们似乎建议将 backend_type 更改为 decimal 并将 frontend_input 更改为 eav_attribute 表中的价格喜欢按数字排序。然而,这不仅似乎没有实际工作,而且它改变了数字的格式,在它前面有一个美元 ($) 符号,因为我们在产品页面上显示实际属性值,在使用它之上作为排序依据,我们无法解决问题。
我正试图弄清楚 getSortOrder() 方法的确切工作原理,但看起来此功能嵌入得非常深,因此我正在努力寻找解决此错误的方法。
感谢任何帮助!
编辑:
对于希望在未来解决此问题的任何人,这是我想出的修复方法:
您需要覆盖存储在 app/Code/Mage/core/catalog/block/product/list.php 中的 List.php 中的函数 _getProductCollection()。
将文件复制到 app/code/Mage/local/catalog/block/product/list.php 这样您就不会编辑核心文件。
然后在下面写着:
$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::ORDER);
$this->_productCollection->getSelect()->order('CAST(`' . $filterAttribute . '` AS SIGNED) ' . $filterAttributeDir . "'");
}
// End of code to force Magento to numerically sort....
现在,如果您在管理面板中将商店所有者的输入验证设置为小数或整数的属性: 然后此代码将重置产品集合的排序顺序,然后将其转换为带符号的整数,以便按数字而不是字母数字排序。
希望对某人有所帮助!
最佳答案
所以我找到了一个thread on this在他们的文档中,显然这是该产品的一个已知痛点。我找到的最佳解决方案是通过调用 Collection
类的原始方法来覆盖查询的 ORDER BY
,这是他们给出的示例:
$_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'));
根据您只有一个排序列的示例,我认为您可以使用:
$_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'));
关于php - Magento 按十进制而不是字母数字排序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260419/