{"id":796,"date":"2020-02-10T04:29:04","date_gmt":"2020-02-10T04:29:04","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/02\/10\/apply-or-conditions-to-collection-in-magento-2\/"},"modified":"2025-04-25T09:37:23","modified_gmt":"2025-04-25T04:07:23","slug":"apply-or-conditions-to-collection-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/apply-or-conditions-to-collection-in-magento-2\/","title":{"rendered":"How to Apply OR Conditions To Collection In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 allows applying OR &amp; AND conditions to collections in Magento 2. However, the default condition applied is AND to collections in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example for AND Condition:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory;\nprotected $productCollectionFactory;\npublic function __construct(CollectionFactory $productCollectionFactory)\n{\n    $this->productCollectionFactory = $productCollectionFactory;\n}\n$collection = $this->productCollectionFactory->create();\n$collection->addAttributeToSelect('*');\n$collection->addAttributeToFilter([['attribute'=>'sku','like'=>'%ch%'],['attribute'=>'status', 'eq'=>'1']]);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The collection of only those products will be fetched whose (write according to the example)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SKU starts with mt and product type is simple<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to apply OR conditions to the collection in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. <strong>OR CONDITION for addAttributeToFilter filter:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory;\nprotected $productCollectionFactory;\npublic function __construct(CollectionFactory $productCollectionFactory)\n{\n    $this->productCollectionFactory = $productCollectionFactory;\n}\n$collection = $this->productCollectionFactory->create();\n$collection->addAttributeToSelect('*');\n$collection->addAttributeToFilter([['attribute'=>'sku','like'=> '%ch%'], \n['attribute'=>'status','eq' => '1']]);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">SKU content character ch or status is 1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. <strong>OR CONDITION for addFieldToFilter filter:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory;\nprotected $productCollectionFactory;\npublic function __construct(CollectionFactory $productCollectionFactory)\n{\n    $this->productCollectionFactory = $productCollectionFactory;\n}\n$collection = $this->productCollectionFactory->create();\n$collection->addAttributeToSelect('*');\n$collection->addFieldToFilter(['attribute_set_id','type_id'],[['eq'=>4],['eq'=>'simple']]\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this way, you can add OR conditions to the collection in Magento 2 using the addFieldToFilter() method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with Magento peeps via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 allows applying OR &amp; AND conditions to collections in Magento 2. However, the default condition applied is AND to collections in Magento 2&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":12994,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions\/12994"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}