Magento 2 – How to Get Confirmation Before Removing Item from Cart

cartmagento2

I want to remove the items from cart from
checkout\cart
page, but before that i wanna to get a confirm from user.
it is possible to get confirmation from user after that i can remove the item from cart if he/she click OK, else he/she click cancel it should not delete the item from cart.

I have tried some examples but still when i click on cancel the item gets removed from cart.

here is the code we need to change:-

<a href="#"
   title="<?php echo $block->escapeHtml(__('Remove item')); ?>"
   class="action action-delete"
   data-post='<?php /* @escapeNotVerified */ echo $block->getDeletePostJson(); ?>'>
    <span>
        <?php /* @escapeNotVerified */ echo __('Remove item')?>
    </span>
</a>

If anyone get the solution?

Best Answer

You have to override form.phtml file

app/design/frontend/vendorname/themename/Magento_Checkout/templates/cart/form.phtml

Add below code at last

<script>
require(['jquery', 'jquery/ui'], function($){ 
        jQuery('.action-delete').click(function(e)
    {
        if(confirm('Do you want to remove this product?'))
        {
            return true;
        }
        else
        {
            return false; 
        }

    });
 });
</script>
Related Topic