How to display popup to confirm before add to cart in magento 2?

How to display popup to confirm before add to cart in magento 2?

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to display popup to confirm before add to cart in magento 2?

Displaying a Confirmation Popup Before Adding Items to Cart in Magento 2

As an e-commerce developer, you might want to provide users with additional choices or confirmations before they add products to their cart in your Magento 2 store. For instance, if your inventory is managed through multiple sources, you may want to prompt the user to select a specific source for each item they add.

In this guide, we’ll walk through how to create a popup screen that appears when a customer clicks “Add to Cart”. This popup will ask them to choose an inventory source and confirm their selection before proceeding. We’ll also ensure that the product is actually available in the chosen inventory before adding it to the cart.

Step 1: Create an Observer for Checking Product Availability

First, let’s create an observer that checks if a product is saleable in a specific inventory source when the “Add to Cart” button is clicked. To do this, follow these steps:

  1. Navigate to app/code/Vendor/Module/etc/events.xml and add the following event observer configuration:
<event name="catalog_product_get_final_price">
    <observer name="check_product_availability_in_source"
              class="Vendor\Module\Observer\CheckProductAvailability"/>
</event>
  1. Create the CheckProductAvailability observer in app/code/Vendor/Module/Observer:
<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;

class CheckProductAvailability implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Implement your logic to check if the product is available in the chosen inventory source.
        // Return or throw an exception if the product cannot be sold.
    }
}

Step 2: Preventing Direct “Add to Cart” Without Confirmation

To prevent users from clicking the “Add to Cart” button without first selecting their preferred inventory source, we need to disable this button by default and enable it only after they confirm their choice in the popup.

For enabling/disabling the button via JavaScript, you can usemage’s require function. However, remember that Magento 2 uses a mix of AMD (RequireJS) for client-side scripts and OOP (Object-Oriented Programming) for server-side handling.

Step 3: Creating the Confirmation Popup

Creating the popup involves both HTML/CSS for front-end design and JavaScript for handling user input. You can create a new template file under app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/addto/options.phtml to display your custom options.

To handle user interaction and validate their choices, you’ll need to write JavaScript code that updates the cookie with their selected inventory source when they click “Confirm”.

Step 4: Connecting the Popup Confirmation to the Observer

After creating the popup and handling its interactivity in JavaScript, you need to link this action with your observer. You can achieve this by calling a custom AJAX endpoint after the user clicks “Confirm” on the popup.

In your JavaScript file (app/design/frontend/Vendor/theme/Magento_Catalog/js/product/view/addto/options.js), make an AJAX call to your custom module’s controller when the user confirms their selection:

$.ajax({
    url: '/custom-module/your-route',
    data: {
        source_id: selected_source,
        product_id: product_id
    },
    type: 'POST'
}).done(function (response) {
    // Handle response from your custom module's controller.
});

In your custom module, create a controller that processes the AJAX request and calls the observer accordingly.

Conclusion

By following these steps, you can create a seamless user experience in your Magento 2 store by displaying a confirmation popup before adding products to the cart. This approach allows for better inventory management while providing users with relevant choices.

Remember to test thoroughly on different browsers and devices to ensure compatibility across all platforms. Also, consider optimizing your code for performance if your catalog has a large number of products or complex inventory rules.

For more advanced customizations in Magento 2, we recommend exploring the Laravel Company blog where we cover various topics on e-commerce development and best practices.

Magento 2 Checkout Catalog Extensions

Our network