user1669256
user1669256

Reputation: 151

Magento - Sorting product collection

I have a product collection which needs to be filtered. For this I have overridden the 'Mage_Catalog_Block_Product_List' and have the following code:

$collection = parent::_getProductCollection();
$collection->addAttributeToFilter('language', array('in' => 
array('B','C','E')));
$collection->addAttributeToSort('language', 'DESC');

return $this->_productCollection; 

On the front end this sorts in the form of E,C,B

If I set the attribute to sort to 'ASC' the sort is in the form of B,C,E

What I need however is to specify the order to E,B,C

Is there any way this can be done?

Thanks for your help.

UPDATE

I have tried fin_Set and looked at the link you sent. I tried the code:

 $collection = parent::_getProductCollection();
 $collection->addAttributeToFilter('language', 
 array('finset'=>'E','C,E,B'));

return $this->_productCollection;

However this only shows products set as 'E' I have also tried the code you provided but I receive an empty product list.

Upvotes: 0

Views: 692

Answers (1)

nevvermind
nevvermind

Reputation: 3392

Try this:

parent::_getProductCollection()
        ->addAttributeToFilter('language', array('finset'=>'B,C,E'));

return $this->_productCollection;

finset maps to MySQL's FIND_IN_SET.

Upvotes: 2

Related Questions