How to Overriding the existing quantity of products already in cart?
<p>I am using the latest version of wordpress and Woocommerce. 
My Functionality is " Overriding the existing Quantity of Products already in cart". I already try this <a href="https://stackoverflow.com/questions/26850763/how-to-override-existing-quantity-already-in-cart">idea</a> but its failure. See the Code and its errors.</p>

<pre><code>add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);
function update_product_in_cart($p, $q) {
global $woocommerce;
$cartItem = $woocommerce-&gt;cart-&gt;cart_contents;
$currentProductId = $q-&gt;id;
$wCart = $woocommerce-&gt;cart-&gt;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-&gt;cart-&gt;empty_cart();
                // If you want to remove just the product from the cart...
                $woocommerce-&gt;cart-&gt;set_quantity($key, 0);
            }
        }
    }
}
// This adds the product to the cart...
return $q;
}
</code></pre>

<p>Its Error is:</p>

<pre><code>Catchable fatal error: Object of class WC_Product_Grouped could not be converted to string in plugins\woocommerce\includes\class-wc-form-handler.php on line 715
</code></pre>