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

Copy link
Copy Markdown

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

Thanks a million for the gist!

@jknight2014

Copy link
Copy Markdown

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

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

Heasan2 commented Nov 11, 2016

Copy link
Copy Markdown

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

@spndavillain

Copy link
Copy Markdown

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

@cjrobe

cjrobe commented Sep 1, 2017

Copy link
Copy Markdown

So simple and works perfectly.

@sarun007

sarun007 commented Nov 1, 2017

Copy link
Copy Markdown

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

insafm commented Jan 2, 2018

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

Thanks. It works perfectly to clear cart :-)

@thepedroferrari

Copy link
Copy Markdown

Thank you, it works!

@alexx855

alexx855 commented May 15, 2018

Copy link
Copy Markdown

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

@bruce-atkinson

Copy link
Copy Markdown

Thanks @alexx855!

@ramonfincken

Copy link
Copy Markdown

@alexx855 you have a double comma in the add_action

@ArtistXhairs

Copy link
Copy Markdown

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

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

@bfintal

bfintal commented Nov 13, 2019

Copy link
Copy Markdown

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

ArtistXhairs commented Nov 13, 2019 via email

Copy link
Copy Markdown

@bfintal

bfintal commented Nov 14, 2019

Copy link
Copy Markdown

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

ArtistXhairs commented Nov 14, 2019 via email

Copy link
Copy Markdown

@paulocoghi

Copy link
Copy Markdown

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

@Kirill-A-Z

Copy link
Copy Markdown

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

Kirill-A-Z commented Nov 30, 2020

Copy link
Copy Markdown

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

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?

@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