我在magento中遇到属性排序问题。我已经为我用来对类别列表进行排序的产品创建了一个下拉属性。我遇到的问题是排序是由值而不是我给它们的顺序进行的。
示例:我有属性颜色和选项: 1.红色 2.黑色 3.绿色
当我选择按颜色排序时,顺序为黑色,绿色,红色,但我需要它是红色,黑色,绿色。
我找到了一个理论上可以解决问题的补丁,但我无法让它发挥作用。 https://gist.github.com/colinmollenhour/4082426
我的magento版本是1.7.0.2。
感谢。
答案 0 :(得分:2)
补丁需要一些修改才能工作。但是,代码最好放在不同类的模型重写中。
对我有用的解决方案是覆盖Mage_Eav_Model_Entity_Attribute_Source_Table::addValueSortToCollection
并将以下代码添加到函数的末尾:
$attribute = $this->getAttribute();
$order = $attribute->getAttributeCode();
$dir = strtoupper($dir);
$collection->getSelect()->reset(Zend_Db_Select::ORDER);
$collection->getSelect()
->joinLeft('eav_attribute_option AS eao', "eao.option_id=IF({$order}_t2.value_id > 0, {$order}_t2.value, {$order}_t1.value)", array("sort_order" => 'eao.option_id'))
->order(new Zend_Db_Expr('eao.sort_order '.$dir));
必须要知道,这将替换每个目录集和每个属性的option_id排序的值排序,其源模型类为eav/entity_attribute_source_table