Question

I trying to filter a product collection based on an attribute which is a multi-select attribute. here is my code,

$collection2 = Mage::getModel('catalog/product')->getCollection();

$collection2->addAttributeToSelect('name'); 
$collection2->addAttributeToSelect('manufacturer');
$collection2->addAttributeToSelect('printer_family');
$collection2->addAttributeToSelect($attributeCode);

$collection2->addFieldToFilter(array(
array('attribute'=>'manufacturer','eq'=>$ctname), 
)); 


$collection2->addFieldToFilter(array(
array('attribute'=>'printer_family','eq'=>$value), //this filter is not working
)); 

the variable $value is retrieved as follows

$product->getAttributeText($attributeCode);

can anyone tell me what i am doing wrong?

Was it helpful?

Solution

Just change

->addFieldToFilter(array(
array('attribute'=>'printer_family','eq'=>$value), //this filter is not working
)); 

to

addFieldToFilter('printer_famil',
    array(
        array('finset'=> array($val1)),
        array('finset'=> array($val2)),
        array('finset'=> array($val3)),
    )
);

Check the links:

https://jmpmaniacs.wordpress.com/2012/03/03/filter-product-collection-for-multi-select-attribute-in-magento/

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top