Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

“Empty cart” button

Adds an “Empty cart” button to the cart page that removes everything in one click.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Needs WooCommerce
  • Category WooCommerce
  • Tested with WordPress 7.1, WooCommerce 11.1
PHP
<?php

add_action(
	'woocommerce_cart_actions',
	static function () {
		printf(
			'<a class="button snipfire-empty-cart" href="%s">%s</a>',
			esc_url( wp_nonce_url( add_query_arg( 'snipfire-empty-cart', '1', wc_get_cart_url() ), 'snipfire-empty-cart' ) ),
			esc_html__( 'Empty cart', 'woocommerce' )
		);
	}
);
add_action(
	'wp_loaded',
	static function () {
		if ( isset( $_GET['snipfire-empty-cart'], $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'snipfire-empty-cart' ) && WC()->cart ) {
			WC()->cart->empty_cart();
			wp_safe_redirect( wc_get_cart_url() );
			exit;
		}
	}
);

Good to know

For the classic cart and checkout (the [woocommerce_cart] and [woocommerce_checkout] shortcodes). The block cart and checkout don’t use these hooks.

In these packs

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.