为了账号安全,请及时绑定邮箱和手机立即绑定

在 WooCommerce 中更新自定义订单项目元

在 WooCommerce 中更新自定义订单项目元

PHP
慕斯王 2023-10-21 19:58:41
我缺少一些东西。我看过几篇有关如何更新项目元数据的文章,但我无法让其中任何一篇起作用。我需要获取 item_id 但我不知道该怎么做。$your_phone = $item->get_meta('dinner_phone'); // 1115559999$update_phone = wdc_format_phone($your_phone); // comes back (111) 555-9999wc_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_idforeach ( $items as $item ) {    $product_id = $item->get_product_id();    $item_id = $item['item_id'];    break;}也尝试过这个    foreach ($items as $key => $product ) {      $item_id = $key;   }
查看完整描述

1 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

您将使用现有 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

}

它应该有效。


查看完整回答
反对 回复 2023-10-21
  • 1 回答
  • 0 关注
  • 57 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信