Ask Before Removing a Product From the Cart
Shows a quick "Remove this item?" confirmation when a shopper clicks the remove (×) link in the cart, preventing accidental removals on phones.
Opens WPIDE on
Code
add_action( 'wp_footer', function () {
if ( ! function_exists( 'is_woocommerce' ) || is_admin() ) {
return;
}
?>
<script>
document.addEventListener( 'click', function ( event ) {
var link = event.target.closest( 'a.remove[href*="remove_item"], a.remove_from_cart_button' );
if ( link && ! window.confirm( <?php echo wp_json_encode( 'Remove this item from your cart?' ); ?> ) ) {
event.preventDefault();
event.stopImmediatePropagation();
}
}, true );
</script>
<?php
} );
Notes
Works on the classic cart page and the mini cart widget.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.