Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento addAttributeToFilter with Multiple Select

I'm having troubles using addAttributeToFilter on Magento Multiple select attributes.

I have a multiple select attribute for car makes, so the content is 'Audi', 'BMW' and so on. In my example I have a tyre product which I have selected 5 makes for from the multiple select.

I want to display products which have the make 'BMW' as one of their selected fields.

I have tried the following all of which are not working:

$products->addAttributeToFilter('make', array('like' => '%BMW%'));  
$products->addFieldToFilter('make', array('like' => '%BMW%'));  
$products->addAttributeToFilter('make', array('like' => '%38%')); // id of the attribute option

Nothing seems to work.

like image 859
Adam Moss Avatar asked Dec 16 '22 00:12

Adam Moss


1 Answers

The solution is to use a Find In Set query such as:

$this->_productCollection->addAttributeToFilter("make", array("finset"=>"38"));

A discussion can be seen here: http://www.magentocommerce.com/boards/viewthread/201312/

And a list of different options: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

like image 110
Alex Hadley Avatar answered Dec 28 '22 00:12

Alex Hadley