OBAID
OBAID

Reputation: 1539

Magento sort collection with multiple attributes

I want to sort Magento product collection with multiple attributes at catalog product list page. 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

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

a,b,c etc

Upvotes: 1

Views: 3154

Answers (2)

OffTheRadar
OffTheRadar

Reputation: 255

There seems to be a bug in Magento 1.9 with setOrder and multiple columns. I was able to get this working like this:

$this->_collection->getSelect()->order(array('price desc', 'price_plus_shipping desc'));

Upvotes: -1

Suman Singh
Suman Singh

Reputation: 1377

I have worked on your approach and get the results. I have created a text type attribute "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

$_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.

Upvotes: 3

Related Questions