How to clear a Woocommerce cart

user1370288 picture user1370288 · Jan 6, 2014 · Viewed 53.4k times · Source

I am wondering how you can clear the contents of your cart on page load using woocommerce.

I came accross how to add a clear cart button using by adding this in to functions.php

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
  global $woocommerce;

    if ( isset( $_GET['empty-cart'] ) ) {
        $woocommerce->cart->empty_cart(); 
    }
}

But I was wondering how I'd go about triggering this on say, page load of the home page (if you could specifiy the exact page that would be great, but even the home page would be useful)

Any ideas? Thanks!

Answer

Kuliraj picture Kuliraj · Jul 5, 2018

The updated version of this would be :

WC()->cart->empty_cart();