How to Apply OR Conditions To Collection In Magento 2

How to Apply OR Conditions To Collection In Magento 2

Magento 2 allows applying OR & AND conditions to collections in Magento 2. However, the default condition applied is AND to collections in Magento 2.

Example for AND Condition:

The collection of only those products will be fetched whose (write according to the example)

SKU starts with mt and product type is simple

Example for OR Condition:

The method to apply OR conditions to collection in Magento 2 is as below:

OR CONDITION for addAttributeToFilter filter:

SKU content character ch or status is 1

OR CONDITION for addFieldToFilter filter:

That’s it.

In this way, you can add OR conditions to the collection in Magento 2 using the addFieldToFilter() method.

Any doubts in the method can be mentioned in the Comments section below and I’d be glad to help you out.

Do share the post with Magento peeps via social media.

Thank you.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

(based on 4 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a reply:

Your email address will not be published.