Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active September 13, 2024 22:41
Show Gist options
  • Select an option

  • Save kloon/4541017 to your computer and use it in GitHub Desktop.

Select an option

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();
}
}
@lyonsun

lyonsun commented Mar 12, 2021

Copy link
Copy Markdown

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
Copy Markdown

Credits: How To Add an Empty Cart Button to WooCommerce

Works, but missing exit after wp_safe_redirect().

@its-leofisher

Copy link
Copy Markdown

Credits: How To Add an Empty Cart Button to WooCommerce

Works, but missing exit after wp_safe_redirect().

Adding exit did the trick

@ekazoom

ekazoom commented Jan 26, 2024

Copy link
Copy Markdown

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

@bethgee

bethgee commented Sep 13, 2024

Copy link
Copy Markdown

This doesn't work - does anyone have a current version of this??

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