How to use multiple AND and OR conditions in searchCriteria filter in Magento 2?

How to use multiple AND and OR conditions in searchCriteria filter in Magento 2?

In this article, I will show you how you can use multiple OR and AND operation in searchCriteria filter.

When we are working with API and working with getList collection using searchCriteria, we sometimes need to build a custom query to get the collection.

There are two main part of searchCrieria

  1. Filter
  2. Filter Group

1. Filter

The Magento\Framework\Api\Filter class is the smallest part of the Search Criteria. It allows you to add a custom field, value, and condition type to the criteria.

For example, We need to get product with SKU = “MS012”

$filter
    ->setField("sku")
    ->setValue("MS012")
    ->setConditionType("eq");

This filter will get a product which has SKU “MS012”

2. Filter Group

The Magento\Framework\Api\Search\FilterGroup class acts like a collection of Filters that apply one or more criteria to a search.

The boolean OR statement joins Filters inside a single Filter Group.

The boolean AND statement join Filter Groups inside a Search Criteria.

How to use OR condition

For example, we need to search product with SKU = “MS012” OR name like “test”

$filter1 = $this->filterBuilder
    ->setField("sku")
    ->setValue("MS012")
    ->setConditionType("eq");

$filter2 = $this->filterBuilder
    ->setField("name")
    ->setValue("%test")
    ->setConditionType("like");

$filterGroup1 = $this->filterGroupBuilder
    ->addFilter($filter1)
    ->addFilter($filter2)
    ->create();

$searchCriteria = $this->searchCriteriaBuilder
    ->setFilterGroups([$filterGroup1])
    ->create();
How to use AND condition

For example, we need to search product with SKU = “MS012” AND name like “test”

$filter1 = $this->filterBuilder
    ->setField("sku")
    ->setValue("MS012")
    ->setConditionType("eq");

$filterGroup1 = $this->filterGroupBuilder
    ->addFilter($filter1)
    ->create();

$filter2 = $this->filterBuilder
    ->setField("name")
    ->setValue("%test")
    ->setConditionType("like");

$filterGroup2 = $this->filterGroupBuilder
    ->addFilter($filter2)
    ->create();

    
$searchCriteria = $this->searchCriteriaBuilder
    ->setFilterGroups([$filterGroup1, $filterGroup2])
    ->create();

Here the full code snippet to get the following query:

Query: To get product with (sku = “MS012” OR name like “%test”) AND (url_type=1)

namespace MagePrince\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Framework\Api\FilterBuilder $filterBuilder,
        \Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder
    ) {
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterBuilder = $filterBuilder;
        $this->filterGroupBuilder = $filterGroupBuilder;
    }

    /**
     * OR condition for searchCriteriaBuilder
     */
    public function getProducts()
    {
        //Filter to get product with sku=MS012
        $filter1 = $this->filterBuilder
            ->setField("sku")
            ->setConditionType("eq")
            ->setValue("MS012")
            ->create();

        //Filter to get product with name like "test"
        $filter2 = $this->filterBuilder
            ->setField("name")
            ->setConditionType("like")
            ->setValue("%test")
            ->create();

        //Filtergroup for product with sku=MS012 OR name like "test"
        $filterGroup1 = $this->filterGroupBuilder
            ->addFilter($filter1)
            ->addFilter($filter2)
            ->create();

        //Filter to get product with url_type=1
        $filter3 = $this->filterBuilder
            ->setField("url_type")
            ->setConditionType("eq")
            ->setValue(1)
            ->create();

        //Filtergroup for product with with url_type=1
        $filterGroup2 = $this->filterGroupBuilder
            ->addFilter($filter2)
            ->create();

        /**
         * Generate searchCriteria instance for products 
         * with (sku=MS012 OR name like "test") AND (url_type=1)
         */
        $searchCriteria = $this->searchCriteriaBuilder
            ->setFilterGroups([$filterGroup1, $filterGroup2])
            ->create();

        $products = $this->productRepository->getList($searchCriteria)->getItems();
        return $products;
    }
}

Use getItems() method to get a collection of products

$products = $this->productRepository->getList($searchCriteria)->getItems();

I hope this article will help you to use OR and AND condition in searchCriteria. If you have any questions you can comment below. Please share this article with the following sharing links.

Happy Coding…:)

Leave a Reply

Close Menu