Skip to main content

Magetno 2 get the products collection filter by attributes with or condition

how to get product collection with filters? I'm use magento 2 . So i'm trying to get in product collection with filter multiple attributes with or condition for custom search

use below product collection object if your wring any blocks if not use object manager and create product collection object

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;


$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');


$collection = $this->_productCollection
                                    ->create()
                                    ->addFieldToSelect('*')
                                    ->addAttributeToFilter(
                                    array(
                                            array('attribute' => 'name', array('like' => '%'.$queryString.'%')),
                                            array('attribute' => 'oem_no',array('like' => '%' .$queryString.'%')),
                                            array('attribute' => 'oem_brand',array('like' => '%' .$queryString.'%'))
                                        )
                            );

this above code gives or condition with result.

$collection = $this->_productCollection
                 ->create()
                 ->addFieldToSelect('*')
                 ->addFieldToFilter('name', ['like' => '%'.$queryString.'%'])->addFieldToFilter('product_title', ['like' => '%'.$queryString.'%']);

this code gives filter and operation


Comments