3

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

7

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));
}
| improve this answer | |
  • 3
    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 – Blackpanther0001 Nov 30 '17 at 18:05
  • Glad to know that you get 2 solutions. Happy Coding :) – Abhishek Panchal Nov 30 '17 at 18:08
  • @AbhishekPanchal, I used below code to get customer email, foreach ($orderData as $orderItem) { $orderItem->customerEmail(); }. which is not returning – jafar pinjar Apr 7 at 12:19
  • @jafarpinjar can you please open new question and post your entire code over there? – Abhishek Panchal Apr 7 at 13:11
  • @AbhishekPanchal, yes, I have created a new ticket for that, pls update me – jafar pinjar Apr 7 at 13:21

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.