Magento addAttributeToFilter with Multiple Select

Adam Moss picture Adam Moss · May 9, 2012 · Viewed 8.5k times · Source

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.

Answer

Alex Hadley picture Alex Hadley · May 9, 2012

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