[英]Magento sort collection with multiple attributes
我想在目錄產品列表頁面上對具有多個屬性的Magento產品集合進行排序。 我正在使用此代碼
$this->_collection->setOrder('price', 'desc');
$this->_collection->setOrder('price_plus_shipping', 'desc');
我也嘗試了這段代碼
$this->_collection->setOrder(array('price', 'price_plus_shipping'),Varien_Data_Collection::SORT_ORDER_DESC);
當我將它們都排序時,它們並沒有給我准確的結果,但是如果我分別使用它們就像我僅使用時一樣
price_plus_shipping
它的工作也很好
價錢
它們的工作文件也向我顯示了准確的結果。 但是我想結合使用它們。 在價格中,我的產品價格在price_plus_shipping中,我有一些字母,例如
a,b,c等
我已經研究了您的方法並獲得了結果。 我創建了一個文本類型屬性“ price_plus_shipping
”。
Visible on Product View Page on Front-end => Yes
Used in Product Listing => Yes
Used for Sorting in Product Listing => Yes
現在在app\\design\\frontend\\[Package]\\[Theme]\\template\\catalog\\product\\list.phtml
上進行以下更改
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection=$_productCollection->addAttributeToSort('price', 'DESC');
$_productCollection=$_productCollection->addAttributeToSort('price_plus_shipping', 'ASC');
$_helper = $this->helper('catalog/output');
希望對您有幫助。
Magento 1.9中的setOrder和多列似乎存在一個錯誤。 我能夠像下面這樣工作:
$this->_collection->getSelect()->order(array('price desc', 'price_plus_shipping desc'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.