【问题标题】:Magento list sort by attribute sorting by attribute order not valuesMagento列表按属性排序按属性排序而不是值排序
【发布时间】:2013-02-18 08:52:23
【问题描述】:

我在 magento 中遇到了属性排序问题。我为用于对类别列表进行排序的产品创建了一个下拉属性。我遇到的问题是排序是按值而不是按我给它们的顺序进行的。

示例:我有带有选项的属性颜色: 1. 红色
2. 黑色
3.绿色

当我选择按颜色排序时,顺序是黑色、绿色、红色,但我需要它是红色、黑色、绿色。

我找到了一个理论上可以解决问题的补丁,但我无法让它工作。 https://gist.github.com/colinmollenhour/4082426

我的 magento 版本是 1.7.0.2。

谢谢。

【问题讨论】:

    标签: php magento sorting e-commerce magento-1.7


    【解决方案1】:

    补丁需要一些修改才能工作。但是,最好将代码放在不同类的模型重写中。

    对我有用的解决方案是覆盖 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));
    

    必须注意,对于每个目录集合和每个属性(其源模型类为 eav/entity_attribute_source_table),这将取代 option_id 排序的值排序

    【讨论】:

    • 谢谢,效果很好。在表前缀的情况下,您应该只对表使用 Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') 一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2013-01-06
    • 2012-12-18
    相关资源
    最近更新 更多