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?