1

Below code is working but when trying to add time doesn't work see other section of code.

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

$orders = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory')->create()
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('status', ['eq'=> 'pending_payment'])
                        ->load();
                        
foreach($orders as $order) {
    $orderState = 'canceled';
    $order->setState($orderState)->setStatus($orderState);
    $order->save();
}

when added time in code and doesn't work

<?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'])
                        ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
                        ->load();
                        
foreach($orders as $order) {
    $orderState = 'canceled';
    $order->setState($orderState)->setStatus($orderState);
    $order->save();
}

1 Answer 1

1

Create a file status.php on your magento root

Add this code and hit the file in browser like: localhost/magento/status.php to change pending_payment orders to cancel

<?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'])
                        ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
                        ->load();
                        
foreach($orders as $order) {
    $orderState = 'canceled';
    $order->setState($orderState)->setStatus($orderState);
    $order->save();
}
4
  • only set this program true if orders are older than 24 hours?
    – Bayzel
    Oct 28, 2022 at 8:06
  • @Bayzel updated the answer Oct 28, 2022 at 9:05
  • it was working earlier before the new edit but now it not working at all.
    – Bayzel
    Oct 28, 2022 at 9:46
  • i have edited your code and it working now
    – Bayzel
    Oct 28, 2022 at 10:09

Your Answer

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

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