-
-
Save kloon/4541017 to your computer and use it in GitHub Desktop.
// 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(); | |
} | |
} |
Thanks a million for the gist!
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?
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 :)
Ohhh spent weeks on the web trying to find a solution to this clear cart problem. This works. Thank you!!! :D
Thank you very much.... you saved my day.
So simple and works perfectly.
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
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
Thanks. It works perfectly to clear cart :-)
Thank you, it works!
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();
}
}
Thanks @alexx855!
@alexx855 you have a double comma in the add_action
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
Now, though it works, when you go to the cart, it empties the cart.
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
Thank, @bfintal ! It's still working with Woocommerce 4.3.0 😃
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?
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
?
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?
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.
Works, but missing exit after wp_safe_redirect().
Works, but missing exit after wp_safe_redirect().
Adding exit did the trick
These all stopped working. I can't clear my cart with any of them.
This doesn't work - does anyone have a current version of this??
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 :)