1

I'm trying to get all orders that were completed a certain number of days ago using the below code:

protected $searchCriteria;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria,
    \Magento\Framework\Api\SortOrderBuilder $sortBuilder
)
{
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteria;
    $this->sortBuilder = $sortBuilder;
    return parent::__construct($context);
}

public function execute()
{   
    $today = date("Y-m-d 0:0:0");
    $from = strtotime('-3 day', strtotime($today));
    $to = strtotime('-2 day', strtotime($today));
    $from = date('Y-m-d h:i:s', $from);
    $to = date('Y-m-d h:i:s', $to);

    $searchCriteria = $this->searchCriteriaBuilder
      ->addFilter('status','complete','eq')
      ->addSortOrder($this->sortBuilder->setField('entity_id')
      ->setDescendingDirection()->create())
      ->setPageSize(500)->setCurrentPage(1)->create();

    $ordersList = $this->orderRepository->getList($searchCriteria);
    $ordersList->addFieldToFilter('updated_at', array('from'=>$from, 'to'=>$to));

    foreach ($ordersList as $order) {
        echo $order->getCustomerEmail();
        exit;
    }

}

This returns a collection of orders however restricting the date just does not seem to be working at all. What is going wrong here?

1

You can get extract time to changes to Complete status,you should check sales_order_status_history.

At the table, sales_order_status_history Magento save order status changes over time.

SELECT * FROM sales_flat_order_status_history where status="complete" and entity_id=4 order by created_at asc limit 1;

  • Yeah this is the better way that i wanted to do but figured it might be easier to use the updated variable as doubt orders will change after completion. I will look into this and thanks for the tip. – harri Jun 1 '18 at 11:19
  • An order may have multiple records complete order status.you should select first one – Amit Bera Jun 1 '18 at 11:26
  • Ok, when would this occur? I am trying to use the status history now but not a single row has complete in it although i have complete orders witihin the admin grid – harri Jun 1 '18 at 11:38
  • Would you be up for checking that table in one of your databases to see if this is normal? Mine has 419 records complete in admin but none marked within this table so bit worrying – harri Jun 1 '18 at 11:58
  • I think potentially this bug github.com/magento/magento2/issues/9730 is stopping me from what i want to achieve here – harri Jun 1 '18 at 13:05
0

This seems to work:

    $searchCriteria = $this->searchCriteriaBuilder
      ->addFilter('status','complete','eq')
      ->addFilter('updated_at',$from,'gteq')
      ->addFilter('updated_at',$to,'lteq')
      ->addSortOrder($this->sortBuilder->setField('entity_id')
      ->setDescendingDirection()->create())
      ->setPageSize(500)->setCurrentPage(1)->create();

So I just updated the initial serach criteria adding the conditions within there. Also then removed the filter on the order repository as was not working that way for me.

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.