0

I am having cron which will basically fetchs pending orders and check some conditions and cancel the orders.my code is

$orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'pending');

foreach($orders as $order){
    // my code which check some conditions
     code ........

    // and variable is set to error
    if($validated == "error"){
        try {
            if(!$order->canCancel()) {
                Mage::log("Cannot Cancel",null,'neo_api.log');
            }else{
                Mage::log("Can Cancel",null,'neo_api.log');
                Mage::log($order->getStatus(),null,'neo_api.log');

                $order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Cancel Transaction.');
                $order->addStatusToHistory($order->getStatus(),'Transaction ID', false);
                $order->save();
            }
        }catch (Exception $e) {
            Mage::log("ERROR: ".$e,null,'neo_api.log');
        }
    }
}

My orders are not canceled.

I had logged it is going into if then it is logging the messages as follows

1) Can Cancel

By same code is working when i write in the controller.

and code is not working when through cron.My code is in the Model/Observer.php

  • have you setup cron job in confing.xml file and also your server? – Abdul Jan 18 '16 at 4:37
0

I tried to reproduce your issue in my local magento and everything is deleting properly by cron.

I updated your code to this:

class Company_Module_Model_Order extends Mage_Core_Model_Abstract
{
    public function cancelOrder()
    {
        $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'pending');
        foreach($orders as $order) {
            if($order->getId() == 11)
            {
                continue;
            }
            try {
                if(!$order->canCancel()) {
                    Mage::log("Cannot Cancel",false ,'neo_api1.log', true);
                }else{
                    Mage::log("Can Cancel",false ,'neo_api1.log', true);
                    Mage::log('pos1' . $order->getId() . '-' . $order->getStatus(), false,'neo_api1.log', true);
                    $order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Cancel Transaction.');
                    Mage::log('pos2' . $order->getId() . '-' . $order->getStatus(), false,'neo_api1.log', true);
                    $order->addStatusToHistory($order->getStatus(),'Transaction ID', false);
                    Mage::log('pos3' . $order->getId() . '-' . $order->getStatus(), false,'neo_api1.log', true);
                    $order->save();
                    Mage::log('pos4' . $order->getId() . '-' . $order->getStatus(), false,'neo_api1.log', true);
                    return;
                }
            }catch (Exception $e) {
                Mage::log("ERROR: ".$e,null,'neo_api.log');
            }
        }
    }
} 

Here is the log:

2015-04-09T13:42:06+00:00 EMERG (): Can Cancel
2015-04-09T13:42:06+00:00 EMERG (): pos111-pending
2015-04-09T13:42:07+00:00 EMERG (): pos211-canceled
2015-04-09T13:42:07+00:00 EMERG (): pos311-canceled
2015-04-09T13:42:07+00:00 EMERG (): pos411-canceled

Which advice can I give to you?

  • Try make something like my script and try to skip one/two/more orders. Maybe your order has something specific which disallow magento to cancel it (even if canCancel() returns true).
  • Do no call cron.php from browser if you do that.
  • Try to put Mage::log dipper. Inside the method $order->cancel()

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.