addAttributeToFilter and OR condition in Magento's Collection
Asked Answered
P

5

28

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?

Pluperfect answered 14/3, 2011 at 16:8 Comment(0)
I
68

yes it is.

$collection->addAttributeToFilter(
    array(
        array('attribute' => 'someattribute', 'like' => 'value'),
        array('attribute' => 'otherattribute', 'like' => 'value'),
        array('attribute' => 'anotherattribute', 'like' => 'value'),
    )
);
Implore answered 14/3, 2011 at 16:28 Comment(3)
note that this syntax is not correct for addFieldToFilter, refer to https://mcmap.net/q/362867/-magento-addfieldtofilter-two-fields-match-as-or-not-and for that syntaxLefthand
can you not give the most generic link possible, where people have to wade and search through to find stuff?Gelderland
thanks for noticing @ahnbizcad, This post is super old. Magento has changed the location of the link.Implore
D
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'),
));
Drugge answered 5/1, 2015 at 13:17 Comment(0)
B
4

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

->addFieldToFilter(
     'type_id',
     ['in' => ['simple', 'configurable']]
)
Breger answered 3/5, 2016 at 1:38 Comment(0)
K
2

For addAttributeToFilter:

$collections = Mage::getModel('sales/order')->getCollection()
             ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds))
             ->addAttributeToFilter('status', ['in' => ['pending','pending_seller_confirmation']]);
Kinakinabalu answered 16/10, 2017 at 14:49 Comment(0)
R
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/

Rabe answered 31/12, 2022 at 6:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.