Magento按小数排序属性,而不是字母数字 [英] Magento Sort Attribute by Decimal not Alphanumerically
问题描述
因此,我疯狂地搜寻了Google,以尝试找到此问题的解决方案,该解决方案实际上可以正常工作,但空手而归.
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.
在类别页面上使用排序依据"功能按属性(容量,重量等)对产品进行排序时. Magento之所以这样排序,是因为它认为数字是文本字符串:
When using the Sort By function on a category page to sort products by an attribute (capacity ,weight etc). Magento sorts like this because it thinks the number is a text string:
产品A:10kg
Product A: 10kg
产品B:11kg
产品C:15kg
产品D:9kg
它应该排序为:
产品D:9kg
Product D: 9kg
产品A:10kg
产品B:11kg
产品C:15kg
环顾四周,似乎有人建议在 eav_attribute 表中将您要设置的属性的 backend_type 更改为小数,将 frontend_input 更改为价格喜欢按数字排序.但是,这似乎并不实际,但它更改了数字的格式,使其前面带有美元($)符号,并且因为在使用它之前,我们在产品页面上显示了实际的属性值进行排序时,我们无法解决问题.
Looking around it seems like people suggest to change backend_type to decimal and frontend_input to price in the eav_attribute table for attributes that you'd like to sort numerically. However, not only does this not seem to actually work, but it changes the format of the number to have a dollar ($) symbol in front of it and because we display the actual attribute value on the product page, on top of using it to sort by, we doesn't work as a fix.
我正试图弄清楚getSortOrder()方法的工作原理,但此功能似乎已深深嵌入,因此我正在努力寻找解决此错误的方法.
I'm trying to figure out exactly how the getSortOrder() method works but it looks like this functionality is pretty deeply embedded so I'm struggling to figure a workaround for this bug.
感谢您的帮助!
对于希望将来解决此问题的任何人,这是我想出的解决方法:
For anyone looking to solve this issue in the future, here's the fix I came up with:
您需要覆盖存储在app/Code/Mage/core/catalog/block/product/list.php中的List.php中的_getProductCollection()函数.
将文件复制到app/code/Mage/local/catalog/block/product/list.php,以便您不编辑核心文件.
You'll need to override the function _getProductCollection() in List.php that is stored in app/Code/Mage/core/catalog/block/product/list.php.
Copy the file to app/code/Mage/local/catalog/block/product/list.php so that you aren't editing core files.
然后在下面说:
$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....
现在,如果您在管理面板中将属性设置为十进制数"或整数",则具有商店所有者的输入验证": 然后,此代码将重置产品集合上的排序顺序,然后将其转换为有符号整数,以便按数字而不是字母数字进行排序.
Now if you have Input Validation for Store Owner in the admin panel for the attribute set to Decimal Number or Integer Number: Then this code will reset the sort order on a product collection and then CAST it as a signed integer so that it sorts numerically rather than alphanumerically.
希望对某人有帮助!
推荐答案
所以我找到了 thread 在他们的文档中,显然这是该产品的一个痛点.我发现的最佳解决方案是通过调用Collection
类的原始方法来覆盖查询的ORDER BY
,这是它们给出的示例:
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'));
这篇关于Magento按小数排序属性,而不是字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!