I know we can add meta for woocommerce cart item using woocommerce_add_cart_item_data
hook.
Is there any way to update existing cart item meta.?
To get the $cart meta data, use the WC->cart->get_cart() and loop it and then you can use the wc_get_formatted_cart_item_data() function by passing the $cart_item .
In order to remove a specific item from your WooCommerce cart, you must loop through the cart, get the item key and wrap the remove_cart_item() function with a product ID dependant if statement, as per the below example. Note, you can replace WC() with $woocommerce.
Yes, but it seems, only via accessing the cart directly:
global $woocommerce;
$woocommerce->cart->cart_contents[$cart_item_key]['whatever_meta'] = 'testing';
$woocommerce->cart->set_session(); // when in ajax calls, saves it.
I would recommend to remove and re-add the product, as other meta data could be lost (as in Phong Tran's answer).
Based on @DanielSalcedos answer, only keeping the absolute minimum required to answer the question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With