Dec 31, 2011
kalpesh

Magento: Getting back shopping cart items after order fails

It’s very awkward if users place an order in your website and by chance the order fails, from end user side or some technical glitch, shopping cart becomes empty. User have to again add items in cart and go through the process which becomes tedious and may be changing their mind to shop.

So here’s a code that does the trick and preserves shopping cart content even if order gets failed.
Open your file where you’re handling your order failure action in frontend. (generally Mage/Checkout/controllers/OnepageController.php)

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
public function failureAction()
{
    $lastQuoteId = $this->getOnepage()->getCheckout()->getLastQuoteId();
    $lastOrderId = $this->getOnepage()->getCheckout()->getLastOrderId();


    if ($lastQuoteId && $lastOrderId) {
        $orderModel = Mage::getModel('sales/order')->load($lastOrderId);
        if($orderModel->canCancel()) {
            
            $quote = Mage::getModel('sales/quote')->load($lastQuoteId);
            $quote->setIsActive(true)->save();
            
            $orderModel->cancel();
            $orderModel->setStatus('canceled');
            $orderModel->save();

            Mage::getSingleton('core/session')->setFailureMsg('order_failed');
            Mage::getSingleton('checkout/session')->setFirstTimeChk('0');
            $this->_redirect('kcheckout/index/payment', array("_forced_secure" => true));
            return;
        }
    }
    if (!$lastQuoteId || !$lastOrderId) {
        $this->_redirect('checkout/cart', array("_forced_secure" => true));
        return;
    }

    $this->loadLayout();
    $this->renderLayout();
}

Be noted here that we don’t have here onepage checkout so we’re redirecting user to payment page after restoring the cart. You can redirect user to any page as per your need after the cart is restored and previous order is cancelled.

8 Comments

  • Hi,

    This thing did not work for me could you please help me with this.. As sometimes there is an error and the customer has to come back to the main site and the cart is empty and in “My Orders” the order has been placed and also there is no way for the customer to make a payment

    • Hi Kaushik, if the error is from Payment gateway, then this is normal behaviour. Magento first places the order and then reaches out to Payment gateway to change Payment state from Pending to Processing if it gets successful. So if the customer did not make through the payment process, then also order is placed and hence shopping cart becomes empty.

      • So when this happens is there a way for the customer to make the payment again for the order??

        • Kaushik, In most of the cases, If you are using an external payment gateway, gateway itself will set the order status to canelled and then code snippet kalpesh has provide above should help you to fill the empty cart.

          Where i really faced problen was with call to method ‘canCancel()’ where method was returning ‘null’ and because of which code inside the If condition was not getting executed. So, I added an OR condition in same if block to check if it method returns ‘null’.

          Here, i assume that if the ‘canCancel’ flag is not set by anyone then we can cancel the order. Probably kalpesh is right person to confirm that.

      • I am working on Depoto ( https://www.depoto.com).

        In this, Cart becomes empty when cancel the payment transaction in one page checkout. I am using IWD one page checkout. I developed this on Magento-1.9.1.1

  • typo error line 20
    should be checkout and not kcheckout

  • this thing wont work.
    $orderModel->canCancel() returns false.
    even if you remove it , the items wont be back to cart.

  • Hi Kalpesh

    I am using lotus breath one step checkout plugin. I am using citrus payment gateway. I am using Magento 1.9.1.0. I have implemented this code but it’s not working properly. Item is not getting persist in cart after cancelling transaction on payment gateway. What can be the solution ?

    Thanks

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications