4

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

2

1 Answer 1

11

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();
                }
            }
        });
    })
});
1
  • How can we add a success message that will show once the cart reloads? This works well to add a confirmation, but I can't seem to figure out how to include a success message. Commented Dec 6, 2022 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.