4

I need to get the orders collection from the last 2 Days until now (including today).

Example: If today is 11/30/2017 -> then I need orders from 11/28/2017, 11/29/2017 and 11/30/2017.

This is how I started:

    public function __construct(

    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Framework\Api\SortOrderBuilder $sortBuilder,
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = [])
    {

      $this->orderRepository = $orderRepository;
      $this->searchCriteriaBuilder = $searchCriteriaBuilder;
      $this->sortBuilder = $sortBuilder;

      parent::__construct($context, $data);
    }

    protected function _prepareLayout()
    {
          $searchCriteria = $this->searchCriteriaBuilder
          ->addFilter('status','pending','eq')
          ->addSortOrder($this->sortBuilder->setField('entity_id')
          ->setDescendingDirection()->create())
          ->setPageSize(100)->setCurrentPage(1)->create();

          $ordersList = $this->orderRepository->getList($searchCriteria);
    }

When I print out an order from the $ordersList, this is what I get:

{"entity_id":"21","state":"new","status":"pending","coupon_code":null,"protect_code":"619c73", .... ,"created_at":"2017-11-23 10:15:18","updated_at":"2017-11-24 16:03:37", .... }

So I think filtering by 'created_at' field is probably the solution.

Maybe something like: ->addFilter('created_at','<today minus 2 days>','gteq')

Any tips on how to do that correctly ?

Thanks

2 Answers 2

11

Try below code

protected function _prepareLayout()
{
    $searchCriteria = $this->searchCriteriaBuilder
      ->addFilter('status','pending','eq')
      ->addSortOrder($this->sortBuilder->setField('entity_id')
      ->setDescendingDirection()->create())
      ->setPageSize(100)->setCurrentPage(1)->create();

    $to = date("Y-m-d h:i:s"); // current date
    $from = strtotime('-2 day', strtotime($to));
    $from = date('Y-m-d h:i:s', $from); // 2 days before

    $ordersList = $this->orderRepository->getList($searchCriteria);
    $ordersList->addFieldToFilter('created_at', array('from'=>$from, 'to'=>$to));
}
5
  • 7
    thanks ! looks like \Magento\Framework\Stdlib\DateTime\TimezoneInterface also works: Using $date as TimezoneInterface Object. $customDate = $date->date()->modify("-2 days")->format('Y-m-d H:i:s') then using it in the filter ->addFilter('created_at', $customDate ,'gteq') also works Nov 30, 2017 at 18:05
  • Glad to know that you get 2 solutions. Happy Coding :) Nov 30, 2017 at 18:08
  • @AbhishekPanchal, I used below code to get customer email, foreach ($orderData as $orderItem) { $orderItem->customerEmail(); }. which is not returning Apr 7, 2020 at 12:19
  • @jafarpinjar can you please open new question and post your entire code over there? Apr 7, 2020 at 13:11
  • @AbhishekPanchal, yes, I have created a new ticket for that, pls update me Apr 7, 2020 at 13:21
0

You can filter date at the searchCriteria:

protected function _prepareLayout()
{
    $to = date("Y-m-d h:i:s");
    $from = strtotime('-2 day', strtotime($to));
    
    $searchCriteria = $this->searchCriteriaBuilder
        ->addFilter('status', 'pending')
        ->addFilter('created_at', ['from' => date('Y-m-d h:i:s', $from)], 'gteq')
        ->addSortOrder(
            $this->sortBuilder->setField('entity_id')->setDescendingDirection()->create()
        )
        ->setPageSize(100)
        ->setCurrentPage(1)
        ->create();
    
    $ordersList = $this->orderRepository->getList($searchCriteria);
    // ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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