Table of contents
Get the industry’s most secure Magento hosting◦ 99.99% uptime
◦ Comprehensive security
◦ 24/7 support

Magento GuideCart → Add To Cart

Magento 2: How to show a popup after an Add to Cart button click

Adding a popup after someone clicks the Add to Cart button in Magento 2 can improve your store’s user experience and boost conversions. Instead of redirecting or just showing a message, a popup lets you provide instant feedback, suggest other products, or invite users to continue shopping—all without leaving the current page.

Let’s walk through how to build this feature step by step using Magento 2’s ajax:addToCart event.

Host Magento at full throttle.

Get secure, reliable Magento hosting so you can scale faster.

How popup behavior works with Add to Cart in Magento 2

When a shopper clicks the Add to Cart button, Magento 2 usually sends the request using AJAX. That means the product gets added to the cart in the background without reloading the whole page. During this process, Magento fires a JavaScript event called ajax:addToCart.

That event is our key to showing a popup. We can listen for it in custom JavaScript, and when it happens, trigger a modal window or popup with whatever content we want—confirmation, upsells, checkout buttons, and more.

Step-by-step: how to show a popup after Add to Cart

Here’s a beginner-friendly walkthrough for creating a simple popup in Magento 2 using your own module.

1. Create a custom module

Magento modules are like plugins—they let you add features without modifying the core. To create one:

2. Create a layout XML file for the popup

Next, define where and how your popup HTML will appear.

Create this file: view/frontend/layout/default.xml

This adds your popup template to the page without affecting other layouts.

3. Create the popup template

Make this file:
view/frontend/templates/popup/confirmation.phtml

Here’s a basic version:

You can style this and add more features later.

4. Add the JavaScript file

Make a JS file that listens for the ajax:addToCart event.

Create: view/frontend/web/js/popup-trigger.js

This code opens the popup when the Add to Cart event fires and closes it when the close button is clicked.

5. Configure RequireJS

Register your script so Magento knows when to load it.

Create: view/frontend/requirejs-config.js

This loads your popup-trigger.js globally on the frontend.

6. Style your popup (basic CSS)

Create a simple CSS file: view/frontend/web/css/popup.css

Include this CSS using layout XML or add it to your theme.

7. Deploy and test

Clear cache and deploy your static files:

Then go to your storefront, click Add to Cart, and make sure your popup appears.

Optional enhancements for your popup

Want to make your popup more dynamic or useful? Try adding:

Benefits of using ajax:addToCart

Using the ajax:addToCart event has real advantages over modifying core JS files:

Best practices for Magento 2 popups

Designing a popup isn’t just about getting it to appear—it also needs to support your store’s goals without annoying your customers. Here are some best practices to make your Magento 2 popups more effective and user-friendly.

Common use cases for post–Add to Cart popups

Once you master the basics, you can use this feature for a variety of goals:

Magento 2 cart FAQs

You can create a custom module that uses JavaScript to listen for the ajax:addToCart event. When triggered, your JavaScript can open a popup with any content you want.

It’s a button that lets users select a product and store it in a shopping cart while they continue browsing. Clicking it doesn’t mean the user is checking out—it’s just the first step toward buying.

You can disable the Add to Cart button by changing a product’s visibility settings, setting its stock status to “Out of Stock,” or removing the button using layout XML or theme overrides.

Use a .phtml template to define the popup HTML, then show and hide it using JavaScript. Magento also supports UI components like Magento_Ui/js/modal/modal for more advanced popups.

Next steps for showing a popup after Add to Cart in Magento 2

Adding a popup after the Add to Cart click helps your Magento store feel more interactive and user-friendly. Whether you’re suggesting related products or simply confirming the cart update, a well-timed popup adds value.

Start by creating a basic custom module, hook into the ajax:addToCart event with JavaScript, and display your popup with helpful content.

Ready to upgrade your Magento experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy.

Liquid Web offers the raw infrastructure power you need with mission-critical features that keep your store running smoothly. Most importantly, our in-house Magento experts are standing by to help with both hosting and Magento application roadblocks.

Click through below to explore all of our Magento hosting options, or chat with an expert right now to get answers and advice.

Ready to get started?

Get the fastest, most secure Magento hosting on the market

Additional resources

What is Magento Ecommerce? →

A complete beginner’s guide to the Magento Ecommerce platform

Magento shopping cart: How to setup, optimize, and manage →

Let’s walk through everything you need to know about configuring, optimizing, and maintaining your Magento cart the right way.

Best Magento ERP extensions →

Our top 10 compared so you can decide which is best for your business