1

We have integrated the CCAvenue payment module. Some time customer not complete the payment. So the product stock decreased in the product table at submit the order. We need to cancel the order with stock added the relevant products for the order.

How to implement this?

  • have you use Cancal button at Admin – Amit Bera Feb 11 '15 at 11:33
  • 1
    Simply cancel your order from backend. – Siddharth Vaghasia Feb 11 '15 at 11:33
  • We need to automatically cancel the order after order 30 min – Rat Feb 11 '15 at 11:35
  • 1
    write a cron job which will run every 30 minutes and get the orders which is not done payment and set the status to cancel. – Pradeep Sanku Feb 11 '15 at 11:39
  • Is there any cron url or need to create a new file? If you have any coding for please post the code – Rat Feb 11 '15 at 11:41
2

I'll assume you know how to create a new extension.

Add a new cronjob in the file config.xml of your extension:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <!--  ... rest your extension configuration ... -->    
    <crontab>
        <jobs>
            <cancel_pending_orders>
                <schedule>
                    <cron_expr>0,30 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>your_module/cron::cancelPendingOrders</model>
                </run>
            </cancel_pending_orders>
        </jobs>
    </crontab>           
</config>

Then create Your_Module_Model_Cron with this code:

<?php

class Your_Module_Model_Cron
{

    /**
     * @param Mage_Cron_Model_Schedule $schedule
     * @return Your_Module_Model_Cron
     */
    public function cancelPendingOrders($schedule)
    {
        foreach ($this->getCollection() as $order) {
            if (!$order->canCancel()) {
                continue;
            }
            $order->cancel();
            $order->save(); 
        }

        return $this;
    }

    /**
     * @return Mage_Sales_Model_Resource_Order_Collection
     */
    public function getCollection()
    {
        // You could make this configurable in the backend
        $minutes = 30;

        // If necessary, you can filter the collection by other criteria like order state/status, payment method etc.
        $collection = Mage::getModel('sales/order')->getCollection()
            ->addFieldToFilter('state', Mage_Sales_Model_Order::STATE_PENDING_PAYMENT)
            ->addFieldToFilter('created_at', array('lt' => new Zend_Db_Expr("DATE_SUB('" . Mage::getModel('core/date')->gmtDate() . "', INTERVAL " . $minutes . " MINUTE)")));

        return $collection;
    }
}

What I didn't do but what you may want to do is wrap the order save in a transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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