Support » Plugin: WooCommerce » How to empty cart before adding the new product to the cart in woocommerce

Viewing 8 replies - 1 through 8 (of 8 total)
  • tweakben

    (@tweakben)

    View the card, then click the “x” next to the thumbnail of the product.

    In case you’re seeking for a PHP-based solution, put this on your theme’s functions.php:

    add_action ('woocommerce_add_to_cart', 'woocommerce_empty_cart_before_add', 0);
    
    function woocommerce_empty_cart_before_add() {
        global $woocommerce;
    
        // Get 'product_id' and 'quantity' for the current woocommerce_add_to_cart operation
        $prodId = (int)$_POST["add-to-cart"];
        $prodQty = (int)$_POST["quantity"];
    
        // Get the total of items for each product in cart
        $cartQty = $woocommerce->cart->get_cart_item_quantities();
    
        // Empty cart before adding the new product to the cart
        if ($cartQty[$prodId] != $prodQty) {
            $woocommerce->cart->empty_cart();
            $woocommerce->cart->add_to_cart($prodId,$prodQty);
        }
    }

    We do not get an infinite loop, because ($cartQty[$prodId] != $prodQty) fails when $woocommerce->cart->add_to_cart() trigger the woocommerce_add_to_cart action for the second time.

    Jim Camomile

    (@castlemediadevs)

    Wow, that is EXACTLY what I was looking for. Thanks for saving me an hour.. and probably more!

    Here it is some improvements to the first code. I’m sure there are better ways to catch the GET and POST requests, as well better ways of using array functions from PHP, but this is the best I can do right now.

    /**
     * Hook: Empty cart before adding a new product to cart WITHOUT throwing woocommerce_cart_is_empty
     */
    add_action ('woocommerce_add_to_cart', 'woocommerce_empty_cart_before_add', 0);
    
    function woocommerce_empty_cart_before_add() {
        global $woocommerce;
    
        // Get 'product_id' and 'quantity' for the current woocommerce_add_to_cart operation
        if (isset($_GET["add-to-cart"])) {
            $prodId = (int)$_GET["add-to-cart"];
        } else if (isset($_POST["add-to-cart"])) {
            $prodId = (int)$_POST["add-to-cart"];
        } else {
            $prodId = null;
        }
        if (isset($_GET["quantity"])) {
            $prodQty = (int)$_GET["quantity"] ;
        } else if (isset($_POST["quantity"])) {
            $prodQty = (int)$_POST["quantity"];
        } else {
            $prodQty = 1;
        }
    
        // If cart is empty
        if ($woocommerce->cart->get_cart_contents_count() == 0) {
    
            // Simply add the product (nothing to do here)
    
        // If cart is NOT empty
        } else {
    
            $cartQty = $woocommerce->cart->get_cart_item_quantities();
            $cartItems = $woocommerce->cart->cart_contents;
    
            // Check if desired product is in cart already
            if (array_key_exists($prodId,$cartQty)) {
    
                // Then first adjust its quantity
                foreach ($cartItems as $k => $v) {
                    if ($cartItems[$k]['product_id'] == $prodId) {
                        $woocommerce->cart->set_quantity($k,$prodQty);
                    }
                }
    
                // And only after that, set other products to zero quantity
                foreach ($cartItems as $k => $v) {
                    if ($cartItems[$k]['product_id'] != $prodId) {
                        $woocommerce->cart->set_quantity($k,'0');
                    }
                }
            }
        }
    }

    It is important to mention that I had some issues using the code above with products configured to be selled individually.

    For this, we need a filter/action that is fired on Add-to-cart button click, but before the current product is added to cart. Otherwise it will go in loop and it will always empty the cart.

    I tried following code and it worked perfectly.

    add_filter( ‘woocommerce_add_cart_item_data’, ‘wdm_empty_cart’, 10, 3);
    function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
    {
    global $woocommerce;
    $woocommerce->cart->empty_cart();
    // Do nothing with the data and return
    return $cart_item_data;
    }

    The above filter is defined in class-wc-cart.php within function add_to_cart().
    http://docs.woothemes.com/wc-apidocs/source-class-WC_Cart.html#774-905

    I have a blog post that allow 1 item MAX added to cart only, refer here.

    @wisdmlabs and @terrytsang your code seems to be the finest solution. Thanks a lot. How do I exclude the old snippets I’ve posted here?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to empty cart before adding the new product to the cart in woocommerce’ is closed to new replies.