How to Restore Magento Quote After Payment Refusal
User experience, by no doubt, is important for a successful E-commerce store. Right from the visit to the site, through the conversion step and even after the purchase, the best user experience must be offered to the customers.
It may happen that when a customer is redirected to 3rd party payment site, the payment is failed due to some technical error or he decides to postpone the payment process for future, he clicks the back button and gets redirected back to the Magento store with an empty cart. If he wants to place the order again, he has to add products to cart again and proceed from scratch. It hinders the shopping experience.
Such a situation forces the user to again fill the cart which is tedious. He may decide to abandon the shopping. To avoid it, make sure to restore Magento Quote after payment refusal. Here, I’ll show how to improve the user experience by preventing Magento to empty the cart in the case of failed payments.
Implement the below code to prevent Magento to empty cart after failed payments!
Method to Restore Magento Quote After Payment Refusal:
in YourIcancelAction() Or failureAction();
Add below code to restore the previous cart:
1 2 3 4 5 6 |
if (Mage::getSingleton('checkout/session')->getLastRealOrderId()) { if ($lastQuoteId = Mage::getSingleton('checkout/session')->getLastQuoteId()) { $quote = Mage::getModel('sales/quote')->load($lastQuoteId); $quote->setIsActive(true)->save(); } } |
With this method, your store customers will no longer have to come back to an empty cart!
You may post your doubts in the Comments section below and I’d be happy to help.
Rate the post with 5 stars if you like it.
Thank you 🙂
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
10 Comments
Hi
i am get this error before going to payment gateway page :
Error in information registration
also my cart in empty
Hello,
Your issue is due to the payment gateway extension.
Thanks.
Hi
Please advise, how to restore Magento Quote After Payment Refusal in magento 2.3.x
Thanks
Hello Jay,
Please use this code: $this->checkoutSession->restoreQuote();
Thank You
Hi (Talking about Magento2)
But, if you restore the quote, you will use the exact quote (same quote id), and you have a canceled order associated to that quote, That is not a problem?
Thanks
Hii,
No, that won’t be a problem.
Thanks.
Do you have a solution for Magento 2?
Use the \Magento\Checkout\Model\Session::restoreQuote() method for your requirements.
What version of magento is this code for? Also,, what is the fie location? Thanks
We have tested this for Magento 1.9
Here’s the file location as per the custom extension:
app\code\core\Mage\Checkout\controllers\OnepageController.php