I want to change the order status to 'canceled' when payment failed. I've tried the code below but the order status gets saved as processing:

        $orderId = $number8; //order id
        $order = Mage::getModel('sales/order')->load($orderId); //load order
        $order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
        $state = 'canceled';
        $status = $state;
        $comment = "Changing state to $state and status to $status Status";
        $isCustomerNotified = false; //whether customer to be notified
        $order->setState($state, $status, $comment, $isCustomerNotified);
        $order->addStatusHistoryComment("Payment Failed",


Please assist. I have a condition in my model if(payment fails) execute code above but the order gets saved as processing instead of canceled.

1 Answer 1


Use only following lines of code no need to save order again & aganin:

$orderId = $number8; //order id
$order = Mage::getModel('sales/order')->load($orderId);
$order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, 'canceled', 'Payment Failed', false)->save();

Second last argument is commnet & Last boolean parameter is $isCustomerNotified false in your case.

What ever payment method you are using it should have cancelAction in it you need to write following code in it:

public function cancelAction()
        $orderId = $this->getRequest()->getParam('orderId');
        if ($orderId) {
            $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
            if ($order->getId()) {
                // Flag the order as 'cancelled' and save it
                $order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, 'canceled', 'Gateway has declined the payment.')->save();
                Mage_Core_Controller_Varien_Action::_redirect('onestepcheckout/index/index/cc/back', array('_secure' => true));

In my case I have this redirect path


Put yours

  • @radhiyah williams please let me know if you still have an issue? Oct 22, 2018 at 8:27
  • i tried that code and it is still saves the order as processing @Hassan Ali Shahzad Oct 22, 2018 at 8:46
  • in my payment setup new order status is set to processing but thats for when its successful. i have a if statement that checks if it failed if it did i want to change the status to canceled but nothing working Oct 22, 2018 at 8:49
  • ok debug by the following ways: 1) First you confirm you have 'canceled' status present in in admin, if not add plz. 2) Second If present then Second add exit(); after the above save line and see the status. If upto here its working fine, then in this case need to debug deep their must be some code which is changing order status back to processing. Oct 22, 2018 at 8:51
  • @RadhiyahWilliams Update ? Oct 22, 2018 at 9:50

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.