0

I have a requirement where I found the list of specific orders according to the filtered conditions. I need to create a searchCriteria which I can pass to $this->orderRepository->getList($searchCriteria). To pass searchcriteria, I need below condition:

(status = 'pending' AND custom_field = 1) OR (status = 'review' AND custom_field = 4)

I found multiple similar questions which answer about the combinations of conditions:

(attribute-A OR attribute-B) AND (attribute-C OR attribute-D)

This link will explain more what I want. If anyone can help me please let me know.

  • please explain briefly – Rk Rathod Feb 19 at 11:42
  • Please check I have added more explanation and added link through which it will be more easy to understand. – Gaurav Agrawal Feb 19 at 11:52
0

Yo can use Filter Group for this. Hope this will be useful.

Class YourClass
{
    protected $filterBuilder;
    protected $filterGroupBuilder;
    protected $searchCriteriaBuilder;
    protected $orderRepository;

    public function __construct(
        .....
        \Magento\Framework\Api\FilterBuilder $filterBuilder,
        \Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Sales\Model\OrderRepository $orderRepository,
        .....
    ) {

        $this->filterBuilder = $filterBuilder;
        $this->filterGroupBuilder = $filterGroupBuilder;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderRepository = $orderRepository;
    }

    public function yourFunction() {
        $filterA1 = $this->filterBuilder
            ->setField('status')
            ->setConditionType('ed')
            ->setValue('pending')
            ->create();

        $filterA2 = $this->filterBuilder
            ->setField('custom_field')
            ->setConditionType('eq')
            ->setValue('1')
            ->create();

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

        $filterB1 = $this->filterBuilder
            ->setField('status')
            ->setConditionType('ed')
            ->setValue('review')
            ->create();

        $filterB2 = $this->filterBuilder
            ->setField('custom_field')
            ->setConditionType('eq')
            ->setValue('4')
            ->create();

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


        // add filter group to setFilterGroups()
        $searchCriteria = $this->searchCriteriaBuilder
            ->setFilterGroups([$filterGroup1, $filterGroup2])
            ->create();

        $this->orderRepository->getList($searchCriteria);

    }
}

Ref: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/searching-with-repositories.html

|improve this answer|||||
  • This will create a condition: (status = 'pending' OR custom_field = 1) AND (status = 'review' OR custom_field = 4). I don't want this. Please check if you can provide the correct solution. – Gaurav Agrawal Feb 19 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.