How To Cancel Pending Orders Automatically In Magento

Magento store owners are sometimes bombarded with pending orders which aren’t completed due to payment difficulties or the customers not completing the order for any reason. These pending orders remains in the database making your Magento store perform slower and slower with time. Deleting all these pending orders manually is a big pain, no store owners wants to waste his/her precious time going through pending order management unless required. One option is to cancel all pending orders first and finally delete them after a said interval which will save database space, so you will only have valid records in your database tables.

For deleting all pending records automatically in Magento you can devise your own policy and interval which will periodically check in your database and cancels all pending orders then you can further delete all cancelled order records from the table.

To cancel all pending orders older than 6 hours in Magento we will create an observer class called Magik_Order_Model_Observer with a public method autocancelPendingOrders. Then we will call this method autocancelPendingOrders in our CRON tab.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Magik_Order_Model_Observer
{
    public function autocancelPendingOrders()
    {
            $orderCollection = Mage::getResourceModel('sales/order_collection');
 
            $orderCollection
                    ->addFieldToFilter('state', 'pending_payment')
                    ->addFieldToFilter('created_at', array(
'lt' =>  new Zend_Db_Expr("DATE_ADD('".now()."', INTERVAL -'360:00' HOUR_MINUTE)")))
                    ->getSelect()
                    ->order('e.entity_id')
                    ->limit(20)
            ;
 
           $orders ="";
            foreach($orderCollection->getItems() as $order)
            {
              $orderModel = Mage::getModel('sales/order');
              $orderModel->load($order['entity_id']);
 
              if(!$orderModel->canCancel())
                continue;
 
              $orderModel->cancel();
              $orderModel->setStatus('canceled_pendings');
              $orderModel->save();
 
            }
 
    }
 
}

In the above example first we are calling our public method autocancelPendingOrders which is calling database tables filtered by status “pending_payment” and older than 6 hours (360 minutes). Finally, we will create a cron tab command to execute this in every 10 minutes

1
2
3
4
5
6
7
 <crontab>
        <jobs>
            <order_cancel_orders>
                <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
                <run><model>order/observer::autocancelPendingOrders</model></run>
            </order_cancel_orders>
    </crontab>

I hope this will help to automatically cancel all pending orders periodically. Magento keeps cancelled payment history for customers so they can refer to what they have ordered previously. If you are sure you do not want to keep cancelled orders then read my next article on how to delete all cancelled payment records from your database.

I would love to hear your experiences of automatically canceling all pending orders in said interval. Please leave me a comment and let me know. Subscribe our RSS to receive latest Magento updates.

  • Moo

    Could you explain where abouts this code would be placed? 

    Also, shouldn’t there be a closing tag?

  • Moo

    jobs* tag

  • Mirco

    Where the code should be placed? Congratulation for the post. Thanks