I need to change the WooCommerce product quantity whenever before the order actually gets submitted (so either after adding to cart or before order submitting). I've managed to do it like this:
function change_cart_item_quantities ( $cart ) {
$new_qty = 9; // Piece of code for new Qty calculation, dummy number for now
// Checking cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['data']->get_id();
$cart->set_quantity( $cart_item_key, $new_qty, false );
}
}
The set_quantity()
method should do exactly what I need and the third parameter is a boolean which defines whether or not cart totals should be refreshed after quantity change. It works well, every product has the quantity of '9' after adding to cart, but the cart totals do get updated.
Is there a way to work this out? Am I missing something?