如何解决在WooCommerce中更新自定义订单项目元
我缺少一些东西。我看过几篇有关如何更新项目元数据的文章,但我无法让它们中的任何一个起作用。我需要获取item_id,但我不知道该怎么做。
$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
我试图通过以下方式拉出Item_id
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$item_id = $item['item_id'];
break;
}
也尝试过
foreach ($items as $key => $product ) {
$item_id = $key;
}
解决方法
您将从现有的WC_Order对象$order
变量中使用以下内容:
foreach ( $order->get_items() as $item-id => $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
}
应该可以。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。