Let shoppers clear their whole cart with one click on the cart page, protected by a security check.
// Show the button next to "Update cart".
add_action( 'woocommerce_cart_actions', function (): void {
$url = wp_nonce_url( add_query_arg( 'nahnu-empty-cart', '1', wc_get_cart_url() ), 'nahnu_empty_cart' );
printf( '<a class="button" href="%s">Empty cart</a>', esc_url( $url ) );
} );
// Empty the cart when the button is clicked.
add_action( 'wp_loaded', function (): void {
if ( ! isset( $_GET['nahnu-empty-cart'] ) || ! function_exists( 'WC' ) || ! WC()->cart ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ?? '' ), 'nahnu_empty_cart' ) ) {
return;
}
WC()->cart->empty_cart();
wc_add_notice( 'Your cart is now empty.' );
wp_safe_redirect( wc_get_cart_url() );
exit;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Removing items one by one is tedious when a cart has many lines, especially on wholesale stores. This snippet adds an Empty cart button to the cart page.
The button is added on woocommerce_cart_actions, next to the coupon and Update cart controls. Its link carries a nonce, a one-time token, so a link on another site can’t empty someone’s cart.
On wp_loaded, after WooCommerce has loaded the cart, the snippet checks the nonce, empties the cart, shows a notice and redirects back to a clean cart URL.
woocommerce_cart_actions hook.Add a few items, open the cart and click Empty cart. The cart should be empty with a “Your cart is now empty” notice.