1

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;
}
  • use only $searchCriteria, no need to use $searchCriteria->create() in the getList function – MeenakshiSundaram R Nov 9 '17 at 10:38
  • @MeenakshiSundaramR the change caused fatal error Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Sales\Model\OrderRepository\Interceptor::getList() must be an instance of Magento\Framework\Api\SearchCriteria, – Korbin Nov 9 '17 at 10:43
  • have you tried like this $this->_orderRepository->getList($searchCriteria) ? – MeenakshiSundaram R Nov 9 '17 at 10:46
  • @MeenakshiSundaramR yes – Korbin Nov 9 '17 at 10:47
  • can you post full exception message – MeenakshiSundaram R Nov 9 '17 at 10:51
0

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;
}

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.