【问题标题】:Woocommerce : How to add a custom meta_key to checkout billing_phone?Woocommerce:如何将自定义 meta_key 添加到结帐 billing_phone?
【发布时间】:2021-08-09 00:31:45
【问题描述】:

我想为每个 woocommerce 订单输入 digits_phone 元键的值以输入为 billing_phone

我想出了类似的方法,但它不起作用:

//Automatically add the digits phone number of the user, to woocommerce orders for every order

add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
    $data['billing_phone'] =['digits_phone'];

    return $data;
}

谁能帮我解决这个问题?

【问题讨论】:

  • 嗨,:) 你从哪里得到digits_phone?假设您使用插件来管理数字,我想您应该能够通过调用 get_user_meta( $current_user->ID, 'digits_phone' , true ); 来获取数据 ...
  • 嗨@giulp。是的digits_phone 由digits 插件管理。那么我应该如何编辑代码以从用户元中实际获取 digits_phone 并将其放入 woocommerce 订单的 billing_phone 中?它必须是这样的吗? add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data'); function dg_manipulate_checkout_posted_data ($data) { $data['billing_phone'] = get_user_meta( $current_user->ID, 'digits_phone' , true );; return $data; }
  • 您需要通过$current_user = wp_get_current_user();获取当前用户数据
  • 感谢您的帮助。实际上我是一个新手,并不真正知道如何完全使用这些代码。您能否以某种方式将您发送的代码与我在此问题中作为答案提出的初始代码结合起来或更正它?

标签: wordpress woocommerce hook-woocommerce meta-key


【解决方案1】:

this post 的最后一条评论中,我看到应该有 2 个与某些 digits_phone 相关的用户元数据:'digits_phone' 和 'digits_phone_no'

假设我们想要的是digits_phone,这段代码应该是一个正确方向的提示:

add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data'); 

function dg_manipulate_checkout_posted_data ($data) {
  // save current user data in variable $current_user
  $current_user = wp_get_current_user();

  //get digits phone from db and save in variable $digits_phone
  $digits_phone = get_user_meta( $current_user->ID, 'digits_phone' , true );     

  // assign to POSTed array and return it
  $data['billing_phone'] = $digits_phone;
  return $data; 
}

还可以查看How can I convert woocommerce checkout fields in capital letters 以更好地了解如何操作 POST 数据

【讨论】:

  • 它为此目的而工作。谢谢你的帮助。真的很感激
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 2017-09-17
  • 2015-10-08
  • 2012-09-19
  • 2020-06-09
  • 2020-07-15
  • 2021-04-30
相关资源
最近更新 更多