使用结账时'Billing First Name'更新WooCommerce 'User Firstname Field'



我目前正在使用WooCommerce。

为了让我的客户生活更轻松,我去掉了账户注册上的"名字"one_answers"姓氏"字段。我刚拿到电子邮件地址。

但是,当客户结账时,我想在他们的用户元中用"billing_first_name"值填充"user_firstname"。

我正在尝试以下代码,但似乎不起作用——有什么想法吗?

//自动更新用户详细信息

add_filter( 'process_checkout' , 'custom_update_checkout_fields', 10, 2 );
function custom_update_checkout_fields($user_id, $old_user_data ) {
  $current_user = wp_get_current_user();
  // Updating Billing info
  if($current_user->billing_first_name != $current_user->user_firstname)
    update_user_meta($user_id, 'user_firstname', $current_user->billing_first_name);
}

干杯,

Linz

好的,所以我找到了一个好的解决方案:

add_action('woocommerce_checkout_order_processed', 'custom_process_order1', 10, 1);
function custom_process_order1($order_id) {
    $current_user = wp_get_current_user();
    $current_user_id = get_current_user_id();
    update_user_meta($current_user_id, "first_name", $current_user->billing_first_name);
    update_user_meta($current_user_id, "last_name", $current_user->billing_last_name);
}

我是个新手,所以帮助别人吧。

  • "add_action"位将此代码挂钩到签出过程中
  • $current_user定义术语
  • 您会注意到wp_get_current_user()和get_current_user_id()有点不同,但有点相同。我想这是因为一个是Wordpress,另一个是WooCommerce
  • 然后我简单地说,将"firstname"字段替换为"billing_first_name"字段

希望对读者有所帮助。

最新更新