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

标签 php wordpress woocommerce metadata orders

我错过了一些东西。我看过几篇关于如何更新项目元数据的文章,但我无法让其中任何一篇文章工作。我需要获取 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
}
它应该工作。

关于php - 在 WooCommerce 中更新自定义订单项元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63524809/

相关文章:

php - 调整大小和拇指功能有效,上传多个文件时除外

php - WordPress 网站在 url 中添加 https 时损坏

javascript - 每当我提交表单后点击后退按钮时,保留选中的复选框

WordPress - 仅在一页上应用remove_filter

html - CSS :target - focus

php - 如果 "sale_price"大于 "price",则更新所有 WooCommerce 产品

javascript - PHP 中的搜索不起作用

php - 错误解析错误 : syntax error, 意外 T_STRING,期望 T_CONSTANT_ENCAPSED_STRING

javascript - 根据 Woocommerce 3 中的送货方式显示或隐藏结帐字段

css - 更改颜色按钮 WooCommerce 产品页面