Site icon webarchers

[Solution] Popup Minicart when product add to the cart Magento 2

In Magento 2, minicart shows in the header and Popup only when you click on the cart icon and show the summary of your cart. What if you want to open the minicart when product added to cart? So, here we come with a very simple solution which will popup/open minicart whenever a product added to the cart of add to cart action is performed.

In order to open shopping cart when add to cart button is clicked, you need to add some custom code.

There are multiple ways to achieve the same goal, but we promised you to provide a very simple solution with very less code.

So, let’s go ahead and write some code to popup the minicart in Magento 2.

You can do this in two ways:-

  1. Just create a custom module and paste the code inside your js folder
  2. Create a file copyright.phtml file at this location \app\design\frontend\yourVendor\youTheme\Magento_Theme\templates\html

And paste the following code:

<script>
     require(['jquery'], function ($) {
            $(document).on('click','.tocart',function(event){
            // console.log("incopyright")
            var minicart = $('[data-block="minicart"]');
            setTimeout(function() {
                minicart.find('[data-role="dropdownDialog"]').dropdownDialog('open');
            }, 3000);
            setTimeout(function() {
                minicart.find('[data-role="dropdownDialog"]').dropdownDialog('close');
            }, 10000);
            
            event.stopPropagation();
        });
});
</script>

Then after don’t forget to clear the cache

Php bin/magento c:f

That’s all

Exit mobile version