简体   繁体   English

更新 WooCommerce 中的自定义订单项元

[英]Update custom order item meta in WooCommerce

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.我需要获取 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

I have tried to pull the Item_id by the following我试图通过以下方式提取 Item_id

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:您将使用现有 WC_Order Object $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
}

It should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Woocommerce 3 中的购物车项目自定义数据更新订单元数据 - Update order meta with cart item custom data in Woocommerce 3 在 WooCommerce 中添加自定义字段作为购物车项目元和订单项目元 - Add custom fields as cart item meta and order item meta in WooCommerce 添加到 Woocommerce 的自定义元数据未显示在订单项元数据中 - Custom meta data added to Woocommerce not displayed in order item meta WooCommerce:添加自定义元作为隐藏订单项元供内部使用 - WooCommerce: Add custom meta as hidden order item meta for internal use 在 WooCommerce 中添加订单号作为自定义订单商品元数据 - Add order number as custom order item meta data in WooCommerce 将自定义购物车商品值添加到WooCommerce订单商品元数据 - Add a custom cart item value to WooCommerce order item meta data 在 WooCommerce 中将产品自定义字段值保存为自定义订单项元数据 - Save product custom field value as custom order item meta in WooCommerce 在Woocommerce 3中检索自定义订单项目元数据值 - Retrieve custom order item meta data values in Woocommerce 3 Woocommerce 预订添加自定义订单商品元数据 - Woocommerce Booking Add custom order item meta data WooCommerce PDF 水印:使用自定义订单商品元数据 - WooCommerce PDF Watermark: Using custom order item meta data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM