# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, cart, javascript, ux
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-cart-remove-confirm/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Ask Before Removing a Product From the Cart" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
