How To Clear Cart When Product Added Into The Cart?

Empty the cart before adding the product into the cart in WooCommerce

In this, we have checked how to clear a cart before adding the cart, When We have to click on add to cart button at a time first clear the cart and then add the product to the cart page.

We have used the woocommerce_add_to_cart_validation hook used to clear the cart.

Following is the code to add to your child theme functions.php file.

add_filter( 'woocommerce_add_to_cart_validation', 'tch_empty_cart', 10, 3 );
function tch_empty_cart( $passed, $product_id, $quantity ) {
    if( ( ! WC()->cart->is_empty() ) )
        WC()->cart->empty_cart();

    return $passed;
}

Following is the output of the code:

 

Submit a Comment

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

Subscribe

Select Categories