addAttributeToFilter and OR condition in Magento's Collection

I’d like to select products depending on several criteria from different attribute.

I know how to user $collection->addAttributeToFilter('someattribute', array('like' => '%'));

But I’d like to use several attribute for OR condition.

Like:

$collection->addAttributeToFilter('someattribute', array('like' => 'value'));`

OR

$collection->addAttributeToFilter('otherattribute', array('like' => 'value'));`

To get products which either ‘someattribute’ OR ‘otherattribute’ set to ‘value’

Is it possible?

4 thoughts on “addAttributeToFilter and OR condition in Magento's Collection”

  1. yes it is.

    $collection->addAttributeToFilter(
        array(
            array('attribute' => 'someattribute', 'like' => 'value'),
            array('attribute' => 'otherattribute', 'like' => 'value'),
            array('attribute' => 'anotherattribute', 'like' => 'value'),
        )
    );
    

  2. In case you wish to use the same thing for addFieldToFilter function for collections that are not using the EAV, then you can using the following format:

    $collection->addFieldToFilter(
    array(
       'someattribute',
       'otherattribute',
       'anotherattribute',
    ),
    array(
        array('like' => 'value'),
        array('like' => 'value'),
        array('like' => 'value'),
    ));
    

  3. For addAttributeToFilter:

    $collections = Mage::getModel('sales/order')->getCollection()
                 ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds))
                 ->addAttributeToFilter('status', ['in' => ['pending','pending_seller_confirmation']]);
    

Leave a Reply

Your email address will not be published. Required fields are marked *