2012-07-15 68 views
3

我已瀏覽了幾篇與排序相關的文章,並未提出所需的解決方案。Magento - 二級排序方式

我正在使用下面的代碼在類別視圖字段中對產品進行排序以覆蓋默認選項並按toolbar.phtml中的屬性進行排序。

<option value="<?php echo $this->getOrderUrl('name', 'asc') ?>"<?php if($this->isOrderCurrent('name')): ?> selected="selected"<?php endif; ?>> 
       NAME 
      </option> 
      <option value="<?php echo $this->getOrderUrl('short_description', 'asc') ?>"<?php if($this->isOrderCurrent('short_description')): ?> selected="selected"<?php endif; ?>> 
       FRAGRANCE 
      </option> 
      <option value="<?php echo $this->getOrderUrl('price', 'asc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>> 
       PRICE (Low to High) 
      </option> 
      <option value="<?php echo $this->getOrderUrl('price', 'desc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>> 
       PRICE (High to Low) 
      </option> 

(I錯誤地使用,而不是添加自定義香味屬性SHORT_DESCRIPTION屬性)

現在,我所需要的功能是,當這些每一個被使用,例如排序價格,我會就像它按照它已經做的價格進行排序,然後再由另一個自定義屬性「範圍」進行二次排序。所以如果它顯示所有3英鎊的產品,它顯示他們從每個範圍聚集在一起,而不是,現在似乎是隨機的。

這是我上次上臺的主要絆腳石,所以任何幫助將不勝感激。

我已經設法通過使用下面的行toolbar.php道路做出一點

if ($this->getCurrentOrder()) { 
     $this->_collection->setOrder(array($this->getCurrentOrder(), 'range'), $this->getCurrentDirection()); 
} 

與唯一的問題是,它似乎通過一系列第一排序,而不是說價格,即所有範圍1按價格排序,然後範圍2按價格排序。當我需要它按價格訂購時,價格的子訂購

+0

您是否嘗試過的第一個參數反轉兩個屬性? – blmage 2012-07-16 07:08:07

+0

我確實,不幸的是它具有相同的效果。 @blmage – Xand94 2012-07-16 09:05:41

回答

4

當您在產品集合上調用setOrder()時,它會檢查給定屬性是否爲price,如果是,則使用addAttributeToSort代替。這是由於價格排序需要一些比通用的方法更具體的代碼。
但是當你傳遞一個數組,測試失敗並使用共同的代碼,所以你可以嘗試兩個setOrder()調用,而不是分開:

if ($this->getCurrentOrder()) { 
    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); 
    $this->_collection->setOrder('range', $this->getCurrentDirection()); 
} 
+0

血腥的Magento,我從來沒有考慮過它會以不同的方式處理數組。非常感謝這個人,正準備放棄希望!就像附加說明一樣,您可以添加更多種類來真正優化需求! – Xand94 2012-07-16 10:31:58