# Empty Cart Button

> Adds an "Empty cart" button next to "Update cart" on the classic cart page, protected by a nonce.

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

## Code

```php
add_action( 'woocommerce_cart_actions', function () {
    $url = wp_nonce_url( add_query_arg( 'wpide_empty_cart', '1', wc_get_cart_url() ), 'wpide_empty_cart' );

    printf( '<a class="button wpide-empty-cart" href="%s">Empty cart</a>', esc_url( $url ) );
} );

add_action( 'wp_loaded', function () {
    if ( empty( $_GET['wpide_empty_cart'] ) || ! function_exists( 'WC' ) || ! WC()->cart ) {
        return;
    }

    $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ) : '';

    if ( ! wp_verify_nonce( $nonce, 'wpide_empty_cart' ) ) {
        return;
    }

    WC()->cart->empty_cart();
    wp_safe_redirect( wc_get_cart_url() );
    exit;
}, 20 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Empty Cart Button" 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/
