How update cart item meta

Last Updated:December 5, 2023

How to update the cart item meta of the the WooComerce cart


add_action('woocommerce_after_cart_item_quantity_update', 'after_cart_item_quantity_update', 10, 3);
function after_cart_item_quantity_update($cart_item_key, $quantity, $old_quantity) {
    // Get the cart item data
    $cart_item = WC()->cart->get_cart_item($cart_item_key);

    // Access and modify custom data of the cart item
    $existing_custom_data = isset($cart_item['custom_data']) ? $cart_item['custom_data'] : '';

    $com_yards = $existing_custom_data ['com_yards'];
    if($com_yards){
        $com_yards = ($com_yards / $old_quantity)*  $quantity;
    }

    // Update the custom data of the cart item
                  
    WC()->cart->cart_contents[$cart_item_key]['custom_data']['com_yards'] =  $com_yards;
}