Magento 2 – How to Add Confirmation Popup Before Removing Item from Cart

magento2magento2.2

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??

Best Answer

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();
                }
            }
        });
    })
});
Related Topic