I want to execute cronjob if the orders are 24 hours old and and turning pending payment status to cancel. how to do that?
I have this code script but doesn't want to work the way it suppose to, what did i miss?
<?php
require './app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$registry = $objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);
$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 86400;
$from = date('Y-m-d H:i:s', $lastTime);
$orders = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory')->create()
->addAttributeToSelect('*')
->addAttributeToFilter('status', ['eq'=> 'pending_payment'])
->addFieldToFilter('created_at', [
'from' => $from,
'to' => $to
])
->load();
foreach($orders as $order) {
$orderState = 'canceled';
$order->setState($orderState)->setStatus($orderState);
$order->save();
}
?>