[英]Magento sort collection with multiple attributes
I want to sort Magento product collection with multiple attributes at catalog product list page. 我想在目录产品列表页面上对具有多个属性的Magento产品集合进行排序。 I am using this code 我正在使用此代码
$this->_collection->setOrder('price', 'desc');
$this->_collection->setOrder('price_plus_shipping', 'desc');
I also tried this code as well 我也尝试了这段代码
$this->_collection->setOrder(array('price', 'price_plus_shipping'),Varien_Data_Collection::SORT_ORDER_DESC);
When i sort these both combine its not giving me accurate results but if i use both separately like when i am using only 当我将它们都排序时,它们并没有给我准确的结果,但是如果我分别使用它们就像我仅使用时一样
price_plus_shipping price_plus_shipping
its working fine also as 它的工作也很好
price 价钱
its also working file they are showing me accurate results. 它们的工作文件也向我显示了准确的结果。 But i want to use them combine. 但是我想结合使用它们。 in Price i have product prices in price_plus_shipping i have alphabets like 在价格中,我的产品价格在price_plus_shipping中,我有一些字母,例如
a,b,c etc a,b,c等
I have worked on your approach and get the results. 我已经研究了您的方法并获得了结果。 I have created a text type attribute " price_plus_shipping
". 我创建了一个文本类型属性“ price_plus_shipping
”。
Visible on Product View Page on Front-end => Yes
Used in Product Listing => Yes
Used for Sorting in Product Listing => Yes
Now made below changes on app\\design\\frontend\\[Package]\\[Theme]\\template\\catalog\\product\\list.phtml
现在在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');
I hope it will help you. 希望对您有帮助。
There seems to be a bug in Magento 1.9 with setOrder and multiple columns. Magento 1.9中的setOrder和多列似乎存在一个错误。 I was able to get this working like this: 我能够像下面这样工作:
$this->_collection->getSelect()->order(array('price desc', 'price_plus_shipping desc'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.