Magento 2 – Filtering Order List with Multiple Search Criteria in AND Logic

filtermagento-2.1magento2orderssearch-criteria

I am trying to filter the order list based on two filters.

So far, all of my attempts failed and didn't work. The filter is not working properly and it returns very first order in list of orders in admin panel. It doesn't care about the status or post value of the filter.
I tried to do the filter based on magento 2 documentation
Any idea?

    public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Sales\Model\OrderRepository $orderRepository,
    \Magento\Sales\Model\Order $order,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Framework\Api\FilterBuilder $filterBuilder,
    \Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder        
) {
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_orderRepository = $orderRepository;
    $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->_order = $order;
    $this->_filterBuilder = $filterBuilder;
    $this->_filterGroupBuilder = $filterGroupBuilder;    
}

/**
* Save order - set as handled
*
* @param {Collection} list of orders
*/
public function setOrdersAsRead($orders){

    foreach($orders as $order){

        $order->setPost('1');
        $order->setStatus('complete');
        $this->_orderRepository->save($order);
    }
}



/**
* Find orders where the `post` field is 0
*
* @return {Array} $orders
*/
public function getOrders() {

    $filter1 = $this->_filterBuilder
          ->setField('post')
          ->setValue('0')
          ->setConditionType('eq')
          ->create();

     $filterGroup1 = $this->_filterGroupBuilder;
     $filterGroup1->setFilters([$filter1]);

     $filter2 = $this->_filterBuilder
         ->setField('status')
         ->setValue('canceled')
         ->setConditionType('neq')
         ->create();

     $filterGroup2 = $this->_filterGroupBuilder;
     $filterGroup2->setFilters([$filter2]);

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

    $orderList = $this->_orderRepository->getList($searchCriteria->create());

    return $orderList;
}

Best Answer

After doing some catch and try and also tnx to @MeenakshiSundaramR, this was the fix for my problem,I changed the getOrders() in this way

    public function getOrders() {

    $filter1 = $this->_filterBuilder
    ->setField('post')
    ->setValue('0')
    ->setConditionType('eq')
    ->create();

    $filterGroup1 = $this->_filterGroupBuilder->setFilters([$filter1])->create();

    $filter2 = $this->_filterBuilder
        ->setField('status')
        ->setValue('canceled')
        ->setConditionType('neq')
        ->create();

    $filterGroup2 = $this->_filterGroupBuilder->setFilters([$filter2])->create();

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

    $orderList = $this->_orderRepository->getList($searchCriteriaBuilder->create());

    return $orderList;
}
Related Topic