27

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?

ÉricP
  • 843
  • 4
  • 12
  • 17

5 Answers5

67

yes it is.

$collection->addAttributeToFilter(
    array(
        array('attribute' => 'someattribute', 'like' => 'value'),
        array('attribute' => 'otherattribute', 'like' => 'value'),
        array('attribute' => 'anotherattribute', 'like' => 'value'),
    )
);
Rito
  • 5,117
  • 3
  • 40
  • 37
  • 17
    note that this syntax is not correct for addFieldToFilter, refer to http://stackoverflow.com/a/7851884/336905 for that syntax – Jonathan Day Sep 02 '13 at 23:32
  • can you not give the most generic link possible, where people have to wade and search through to find stuff? – ahnbizcad Mar 30 '17 at 18:42
  • 2
    thanks for noticing @ahnbizcad, This post is super old. Magento has changed the location of the link. – Rito Jun 13 '17 at 12:36
13

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'),
));
George Donev
  • 563
  • 7
  • 13
4

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

->addFieldToFilter(
     'type_id',
     ['in' => ['simple', 'configurable']]
)
johnsnails
  • 1,971
  • 20
  • 29
2

For addAttributeToFilter:

$collections = Mage::getModel('sales/order')->getCollection()
             ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds))
             ->addAttributeToFilter('status', ['in' => ['pending','pending_seller_confirmation']]);
Hassan Ali Shahzad
  • 2,439
  • 29
  • 32
0

addAttributeToFilter Conditionals There are many operators in SQL and addAttributeToFilter will accept all of them, as long as you use the correct syntax. I've listed them all below and provided examples.

Equals: eq This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you're using.

$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator

Not Equals - neq

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

Like - like

$_products->addAttributeToFilter('sku', array('like' => 'UX%'));

One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.

Not Like - nlike

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
In - in
$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
When using in, the value parameter accepts an array of values.

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
NULL - null
$_products->addAttributeToFilter('description', array('null' => true));

Not NULL - notnull

$_products->addAttributeToFilter('description', array('notnull' => true));
Greater Than - gt
$_products->addAttributeToFilter('id', array('gt' => 5));

Less Than - lt

$_products->addAttributeToFilter('id', array('lt' => 5));
Greater Than or Equals To- gteq
$_products->addAttributeToFilter('id', array('gteq' => 5));

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));

Debugging The SQL Query There are two ways to debug the query being executed when loading a collection in Magento.

/*
 * This is the better way to debug the collection
 */
$collection->load(true);

/*
 * This works but any extra SQL the collection object adds before loading
 * may not be included
 */
echo $collection->getSelect();

Reference Link https://fishpig.co.uk/magento/tutorials/addattributetofilter/