Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CleanExpiredOrders.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
16  protected $storesConfig;
17 
22 
27  public function __construct(
29  \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
30  ) {
31  $this->storesConfig = $storesConfig;
32  $this->orderCollectionFactory = $collectionFactory;
33  }
34 
40  public function execute()
41  {
42  $lifetimes = $this->storesConfig->getStoresConfigByPath('sales/orders/delete_pending_after');
43  foreach ($lifetimes as $storeId => $lifetime) {
45  $orders = $this->orderCollectionFactory->create();
46  $orders->addFieldToFilter('store_id', $storeId);
47  $orders->addFieldToFilter('status', Order::STATE_PENDING_PAYMENT);
48  $orders->getSelect()->where(
49  new \Zend_Db_Expr('TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, `updated_at`)) >= ' . $lifetime * 60)
50  );
51  $orders->walk('cancel');
52  $orders->walk('save');
53  }
54  }
55 }
__construct(StoresConfig $storesConfig, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory)