addAttributeToFilter and OR condition in Magento's Collection

52,131

Solution 1

yes it is.

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

Solution 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'),
));

Solution 3

Another thing to look at to achieve 'OR" is:

->addFieldToFilter(
     'type_id',
     ['in' => ['simple', 'configurable']]
)

Solution 4

For addAttributeToFilter:

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

Related videos on Youtube

ÉricP
Author by

ÉricP

Updated on July 09, 2022

Comments

  • ÉricP
    ÉricP almost 2 years

    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?

  • Jonathan Day
    Jonathan Day over 10 years
    note that this syntax is not correct for addFieldToFilter, refer to stackoverflow.com/a/7851884/336905 for that syntax
  • ahnbizcad
    ahnbizcad about 7 years
    can you not give the most generic link possible, where people have to wade and search through to find stuff?
  • Rito
    Rito almost 7 years
    thanks for noticing @ahnbizcad, This post is super old. Magento has changed the location of the link.