0

Trying to filter a collection to show results where attribute_1 = something OR attribute_2 = something. I can get it to filter by one or the other, but not if one is true OR the other is true.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('attribute_1');
$collection->addAttributeToSelect('attribute_2');

$collection->addFieldToFilter(array(
array("attribute"=>"attribute_1","eq"=>"something"),
array("attribute"=>"attribute_2","eq"=>"something")
));
1

Try this instead:

$collection->addAttributeToFilter(array(
    array("attribute"=>"attribute_1","eq"=>"something"),
    array("attribute"=>"attribute_2","eq"=>"something")
    ));
  • Worked perfect. Thank you. It won't let me vote you up until I have 15 reputation points. – lv2fly Mar 23 '17 at 2:20
  • Correction. Doesn't work perfect. The exact same code that worked in sales_order_place_after observer isn't working in catalog_product_save_after or catalog_product_prepare_save observer. If I limit it to a single attribute, it works how it should. Don't understand why it would work in one place but not another. – lv2fly Mar 23 '17 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.