Categories: How To

How to Prevent Magento from Emptying Cart After Payment Cancellation

Emptying cart before payment confirmation is red signal for Magento store sales. It’s really very harmful Magento default functionality but Magento still does it unfortunately. Let me explain it with an example.

Suppose you have been redirected to a payment gateway to proceed payment, but because of some reason, you click back without paying or payment gets failed. In such a case, payment gateway redirects back to Magento store shopping cart page but sadly with empty cart. Now if customer wants to continue again, it will get him annoyed of searching for products and adding them again and rather he may prefer leaving your site! Painful and costly, isn’t it?

To overcome this situation, you need to trick a code to redirect customers back to Magento stores with already filled cart with earlier selected products and options.

Below is the code to prevent Magento from Emptying Cart after Payment Cancellation:

Go to app/code/[local/community]/[namespace]/[module_name]/controller/exampleController.php and rewrite failureAction() or canceledAction() method as shown below.


While working on a client’s site, I came to know this problem and find the solution for it. It worked pretty well for me without changing any other single part. Using this code marks current order as cancelled and restores the cart using the previous order details. Hope it will work great for you as well.

Let me know have you used this code to stop customers filling the purchase data again? Tell me how this blog affected and helped you through commenting. Queries and questions are always welcomed.

Click to rate this post!
[Total: 10 Average: 4.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago