Confirmation popup before removing item from cart

The question:

When i remove item from cart i get a poup confirmation:

     require([
        'jquery'
    ], function($) {
            $('.action-delete').off('click');
            $('.action-delete').click(function(e) {
            if(confirm('confirm')){
                return true
            }
            else {
                return false
            }
        })
})

How can i do this with Confirmation widget Magento 2??

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You must add this code in your theme’s folder.

Magento_Checkout/templates/cart/form.phtml

require([
    'jquery',
    'mage/translate',
    'Magento_Ui/js/modal/confirm',
    "mage/template",
], function ($, $t, confirm, mageTemplate) {
    $('.action-delete').click(function (e) {
        e.stopPropagation();
        confirm({
            title: $t("Remove item cart"),
            content: $t("Do you want remove this item of the cart?"),
            modalClass: "classModal",
            actions: {
                confirm: function () {
                    var params = $(e.currentTarget).data('post');
                    var formTemplate = '<form action="<%- data.action %>" method="post">'
                        + '<% _.each(data.data, function(value, index) { %>'
                        + '<input name="<%- index %>" value="<%- value %>">'
                        + '<% }) %></form>';

                    var formKeyInputSelector = 'input[name="form_key"]';

                    var formKey = $(formKeyInputSelector).val();
                    if (formKey) {
                        params.data.form_key = formKey;
                    }
                    $(mageTemplate(formTemplate, {
                        data: params
                    })).appendTo('body').hide().submit();
                }
            }
        });
    })
});


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment