WooCommerce: have to refresh page before I can update item quantity?

When I add an item to the cart, the following code will check if the item already exists in the cart, if it does, delete the product from the cart and add the item again but with the new quantity.

add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );

function woo_custom_add_to_cart_before( $cart_item_data ) {
$cart = WC()->instance()->cart;
$id = $_POST['product_id'];
$cart_id = $cart->generate_cart_id($id);
$cart_item_id = $cart->find_product_in_cart($cart_id);

if($cart_item_id){
$cart->set_quantity($cart_item_id,0);
}
return true;
}

This works fine, but doesn’t allow me to add the item for a second time. For example an item is added using ajax, I then adjust the quantity and click the add to cart button for a second time but nothing happens - the cart is not updated, I have to refresh the page before I can edit the same item again. How can I customise my code to edit the quantity multiple times without the need to refresh?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.