How To Clear The Cart in WooCommerce Programatically

Sometimes you may want to clear the cart automatically. Let’s say, if a customer visits some url or after executing some code. Luckily, there’s a method for that. $woocommerce->cart->empty_cart() will also clear the cart session.

The code snippet below shows how to clear the cart when someone vists an url with /?clear-cart parameter.


function lionplugins_woocommerce_clear_cart() {
    global $woocommerce;

    if ( isset( $_GET['clear-cart'] ) ) {
    	$woocommerce->cart->empty_cart();
    }
}
add_action( 'init', 'lionplugins_woocommerce_clear_cart' );

Leave a Reply

Your email address will not be published. Required fields are marked *