I am missing something. I have seen several articles about how to update an items meta data but I can't get any one of them to work. I need to get the item_id but I can't figure out how to do that.
$your_phone = $item->get_meta('dinner_phone'); // 1115559999
$update_phone = wdc_format_phone($your_phone); // comes back (111) 555-9999
wc_update_order_item_meta($item_id,'dinner_phone', $update_phone); //I want to update with new format
$new_phone = $item->get_meta('dinner_phone'); // doesn't work I still get 1115559999
I have tried to pull the Item_id by the following
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$item_id = $item['item_id'];
break;
}
Also tried this
foreach ($items as $key => $product ) {
$item_id = $key;
}
You will use the following from an existing WC_Order Object $order
variable:
foreach ( $order->get_items() as $item ) {
$dinner_phone = $item->get_meta('dinner_phone'); // 1115559999
if ( ! empty( $dinner_phone ) ) {
$formatted_diner_phone = wdc_format_phone( $dinner_phone ); // comes back (111) 555-9999
$item->update_meta_data('dinner_phone', $formatted_diner_phone);
$item->save(); // Save item
$new_phone = $item->get_meta('dinner_phone');
echo $new_phone; // Check that items is updated
}
$order->calculate_totals(); // Recalculate Order totals and save
}
It should work.