Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Created May 26, 2016 15:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BFTrick/4e94862ec64d56164b960aa16701e090 to your computer and use it in GitHub Desktop.
Save BFTrick/4e94862ec64d56164b960aa16701e090 to your computer and use it in GitHub Desktop.
WooCommerce add an Empty Cart button
<?php
// check for empty-cart get param to clear the cart
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'woocommerce_cart_actions', 'patricks_add_clear_cart_button', 20 );
function patricks_add_clear_cart_button() {
echo "<a class='button' href='?empty-cart=true'>" . __( 'Empty Cart', 'woocommerce' ) . "</a>";
}
@patrickrios
Copy link

Great, thanks for sharing this solution
Very useful

@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

@nicobsas
Copy link

Hi ! I have the same situation.
Is this code working on the new WP version?
Best,

@andregiaever
Copy link

Hi!

Having the same issue. Works nicely when logged out but fails when logged in...

@MediaPublishing
Copy link

MediaPublishing commented Mar 15, 2018

hmm. works flawless with user logged in here. how come? Wocommerce 3.3.3

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