Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update cart item meta - woocommerce

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.?

like image 722
Sark Avatar asked Oct 24 '15 20:10

Sark


People also ask

How do you get meta items from cart?

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 .

How do I remove items from my cart in WooCommerce?

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.


1 Answers

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.

like image 157
benzkji Avatar answered Sep 25 '22 00:09

benzkji