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();
}
}
@EmanuelFaria
Copy link

Thanks for sharing this solution!

I have a total noob question... I'm using WP with a DIVI theme and Woocommerce. Could you please tell me where to put this code? Do I download the php file and put it in a folder on my site, or do I copy and paste this code somewhere into the product page?

Thanks in advance :)

@cheey2003
Copy link

Thanks a million for the gist!

@jknight2014
Copy link

Question, I am trying to use this with the add-to-cart function and it looks like it works. Anything that was in the cart is gone and the add-to-cart item is there. But when I click checkout I go back to the cart with nothing there.

Any thoughts?

@Pixelbits-nl
Copy link

Maybe a bit late but: @mannyrules

Just add the code (not just the file) it to the functions.php of your DIVI-childtheme. Google these words if they don't mean anything to you :)

@Heasan2
Copy link

Heasan2 commented Nov 11, 2016

Ohhh spent weeks on the web trying to find a solution to this clear cart problem. This works. Thank you!!! :D

@spndavillain
Copy link

Thank you very much.... you saved my day.

@cjrobe
Copy link

cjrobe commented Sep 1, 2017

So simple and works perfectly.

@sarun007
Copy link

sarun007 commented Nov 1, 2017

This code is not working when user logged in.When user logged in apply the empty cart button then cart is not empty.This code only working at the time of user logged out.I am using latest version of woocommerce 3.2.2

@insafm
Copy link

insafm commented Jan 2, 2018

global $woocommerce;
$woocommerce->cart->empty_cart();

I used this, but I got an error and I don't know why. Also I can't user add_to_cart() returns same error. What is the solution?

Error Details:
Fatal error: Uncaught Error: Call to a member function empty_cart() on null in /var/www/html/live/wp-content/plugins/zzz/inc/test-codes.php:27 Stack trace: #0/var/www/html/live/wp-content/plugins/zzz/inc/test-codes.php(5): afl_test_codes_callback() #1 /var/www/html/live/wp-includes/class-wp-hook.php(286): afl_admin_test_codes('') #2 /var/www/html/live/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #3 /var/www/html/live/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #4 /var/www/html/live/wp-admin/admin.php(224): do_action('test_page_eps-t...') #5 {main} thrown in /var/www/html/live/var/www/html/live/wp-content/plugins/zzz/inc/test-codes.php on line 27

@zzaheer1
Copy link

Thanks. It works perfectly to clear cart :-)

@thepedroferrari
Copy link

Thank you, it works!

@alexx855
Copy link

alexx855 commented May 15, 2018

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

@appinlet
Copy link

Thanks @alexx855!

@ramonfincken
Copy link

@alexx855 you have a double comma in the add_action

@ArtistXhairs
Copy link

This doesn't seem to work as of today. Did the recent WooCommerce/Wordpress update change it? I'd love some help with the change. Thanks

@ArtistXhairs
Copy link

Now, though it works, when you go to the cart, it empties the cart.

@bfintal
Copy link

bfintal commented Nov 13, 2019

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

@ArtistXhairs
Copy link

ArtistXhairs commented Nov 13, 2019 via email

@bfintal
Copy link

bfintal commented Nov 14, 2019

Does this currently work? How does one of your links look? Are you able to add something to cart AND use the empty cart URL?

The one I mentioned works for me. The cart gets emptied and my add-to-cart product gets added. My links look like: https://mysite.com/checkout/?add-to-cart=1234

@ArtistXhairs
Copy link

ArtistXhairs commented Nov 14, 2019 via email

@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