Support » Plugin: WooCommerce » How to override existing quantity already in cart

  • hi,

    I want to override existing quantity already in cart for grouped products. I got this code from stackoverflow

    Please help me to find the solution…..

    add_action(‘woocommerce_add_to_cart_handler’, ‘update_product_in_cart’, 11, 2);

    function update_product_in_cart($p, $q) {
    global $woocommerce;
    $cartItem = $woocommerce->cart->cart_contents;
    $currentProductId = $q->id;
    $wCart = $woocommerce->cart->get_cart();

    // If cart already exists, and product exists, than remove product, and add the new product to it.
    if ($wCart)
    {
    $cart_item_keys = array_keys($wCart);

    foreach($cart_item_keys as $key)
    {
    foreach($cartItem as $item)
    {
    $productItemId = $item[‘product_id’];
    if ($productItemId == $currentProductId)
    {
    // If you want to empty the entire cart…
    // $woocommerce->cart->empty_cart();
    // If you want to remove just the product from the cart…
    $woocommerce->cart->set_quantity($key, 0);
    }
    }
    }
    }
    // This adds the product to the cart…
    return $q;
    }

  • The topic ‘How to override existing quantity already in cart’ is closed to new replies.