How can I correctly update/change/modify order item quantity
of order items in existing WooCommerce orders?
(I am not talking about cart items before order creation like in How to update WooCommerce order item quantity )
I would please like to know both ways:
- via SQL (
UPDATE
) - via the WordPress way (
wp functions
)
I know that the product_qty
field is located in the wc_order_product_lookup
table.
I know that the meta_key
field containing the _qty
value ( where the meta_value
field is containing the order item quantity) is located in the woocommerce_order_itemmeta
table.
As a side note:
I am using the WooCommerce Advanced Quantity plugin ( https://codecanyon.net/item/woocommerce-advanced-quantity/11861326/support ) which enables decimal/float quantities (which I use as weight) instead of integer ones.
wc_order_product_lookup
table: product_qty
field - integer quantities (decimals are cut off)
woocommerce_order_itemmeta
table: meta_key
= _qty
, meta_value
- decimal quantities
Thus, both fields (integer & decimal/float) should be correctly updated.
Any clue would be highly appreciated.
I found 2 resources pointing in the right direction:
$order = wc_get_order($ordernum);
foreach( $order->get_items() as $item_id => $item )
{
$item->set_quantity($qnty);
$item->save();
}
$order->save();
wc_add_order_item_meta( $order_item_id, '_qty', 2, true ); // quantity