2013-02-18 163 views
2

我正面臨着magento屬性排序問題。我爲我用來排序分類列表的產品創建了一個下拉屬性。我遇到的問題是,排序是由值而不是由我給他們的順序。Magento列表按屬性排序按屬性排序不值

例子:我有一個選項屬性顏色: 1.紅
2.黑色
3.綠色

當我選擇按顏色排序的順序是黑,綠,紅,但我需要它是紅色,黑色,綠色。

我發現了一個補丁,理論上可以解決這個問題,但我不能讓它工作。 https://gist.github.com/colinmollenhour/4082426

我的magento版本是1.7.0.2。

謝謝。

回答

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

+0

謝謝,這工作完美。在表前綴的情況下,只有一個問題需要使用Mage :: getSingleton('core/resource') - > getTableName('eav_attribute_option')。 – 2013-02-18 13:14:56

+0

你是對的,謝謝你的留言 – 2013-02-18 13:27:28