Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active January 26, 2024 18:14
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save kloon/4541017 to your computer and use it in GitHub Desktop.
Save kloon/4541017 to your computer and use it in GitHub Desktop.
WooCommerce Clear Cart via URL
// check for clear-cart get param to clear the cart, append ?clear-cart to any site url to trigger this
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
@paulocoghi
Copy link

Thank, @bfintal ! It's still working with Woocommerce 4.3.0 😃

@Kirill-A-Z
Copy link

2018 way:

add_action( 'woocommerce_before_cart', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
	if ( isset( $_GET['clear-cart'] ) ) {
		WC()->cart->empty_cart();
	}
}

Doesn't seem to work now.. For me just doesn't do anything - adding /?clear-cart to the domain just leads to the main page.
/?add-to-cart=... works though

Is the only thing that should be done is adding the code to functions.php?
Any other things should be done before it starts to work?

@Kirill-A-Z
Copy link

Kirill-A-Z commented Nov 30, 2020

For me, I combined @kloon & @alexx855's solution to trigger on the add-to-cart url:

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
	if ( isset( $_GET['add-to-cart'] ) ) {
		WC()->cart->empty_cart();
	}
}

This one works for me though..
I don't see much difference, but the previous didn't work with /?clear-cart in the url
And this one works perfectly.
Thank you, @bfintal

Can the script be changed so that it would be possible to use clear-cart separately or together with add-to-cart?
something like

https://mysite.com/checkout/?clear-cart&add-to-cart=1234,1235

?

@lyonsun
Copy link

lyonsun commented Mar 12, 2021

Can we do a redirection after clicking the clear cart url? Like if the url is inside the cart page, how can we reload it as cart empty page after clicking the url?

@lyonsun
Copy link

lyonsun commented Mar 12, 2021

Can we do a redirection after clicking the clear cart url? Like if the url is inside the cart page, how can we reload it as cart empty page after clicking the url?

Finally figured this part out. So the woocommerce_before_cart hook will work for the Woocommerce empty_cart function, but it won't do any page reload.
If page reloading is needed, the function has to be hooked with the init hook, or better with wp_loaded hook IMO.

Credits: How To Add an Empty Cart Button to WooCommerce

@GitHub-Mike
Copy link

Credits: How To Add an Empty Cart Button to WooCommerce

Works, but missing exit after wp_safe_redirect().

@its-leofisher
Copy link

Credits: How To Add an Empty Cart Button to WooCommerce

Works, but missing exit after wp_safe_redirect().

Adding exit did the trick

@ekazoom
Copy link

ekazoom commented Jan 26, 2024

These all stopped working. I can't clear my cart with any of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment