1

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();
}
?>

1 Answer 1

1

You can try below code:

Can refer vendor/magento/module-sales/Controller/Adminhtml/Order/MassCancel.php file which is used to cancel order from Admin!

<?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 = date('Y-m-d h:i:s', strtotime("-1 day"));

$orders = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory')->create()
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('status', ['eq'=> 'pending_payment'])
                        ->addFieldToFilter('created_at', ['lt'=> $time])
                        ->load();

$orderManagement = $objectManager->get('\Magento\Sales\Api\OrderManagementInterface');
                        
foreach ($orders as $order) {
    try {
        $orderManagement->cancel($order->getEntityId());
        echo "order with Id =".$order->getId()." is canceled" .PHP_EOL;
    } catch (\Exception $e) {
        return __('cannot cancel the order');
    }
}

Hope above will help!

7
  • time() method is not working it only cancel orders that are placed today which i don't want. i want to cancel all orders that are 24 hours old with a pending payment status
    – Bayzel
    Nov 2, 2022 at 8:32
  • I have updated code, $time will get last order greater than last 24 hours
    – Pawan
    Nov 2, 2022 at 9:00
  • i have edited the code i put 'lt'=> $time it working for me rather than 'gt'=> $time and i added "PHP_EOL" in the code please check
    – Bayzel
    Nov 2, 2022 at 10:46
  • Approved your updates..I was checking and modified code as per my orders data..forgot to update!now it is fine...
    – Pawan
    Nov 2, 2022 at 10:58
  • you can accept answer as well!
    – Pawan
    Nov 2, 2022 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.