对于在结帐时注册的新用户,将自定义字段保存到 WooCommerce Checkout 上的用户元数据 [英] Save custom fields to user meta on WooCommerce Checkout for new user who is registering on checkout

查看:23
本文介绍了对于在结帐时注册的新用户,将自定义字段保存到 WooCommerce Checkout 上的用户元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在结帐中添加了几个自定义字段,这些字段仅在用户第一次以访客身份结帐时显示.我的结帐流程要求用户创建一个帐户才能完成结帐.

I have added several custom fields to my checkout that only appear when a user is checking out as a guest for the first time. My checkout process requires that the user create an account to complete checkout.

我使用 woocommerce_form_field 方法添加了四个字段,然后尝试修改 提供的代码在这里回答以实现我想要的.但是,我尝试了多次结帐,创建新帐户以检查自定义字段值是否保存到新用户的个人资料中,但似乎不起作用.

I have added four fields using the woocommerce_form_field method and then I have tried modifying the code provided in the answer here to achieve what I want. However, I have tried several checkouts, creating new accounts to check if the custom field values save into the new users' profile, but it doesn't seem to be working.

以下是我尝试过但不起作用的解决方案的一个示例:

Here is one example of a solution I tried which did not work:

function reigel_woocommerce_checkout_update_user_meta( $customer_id) {
    if ( ! empty( $_POST['practitioner_license_number'] ) ) {
        $pln = sanitize_text_field( $_POST['practitioner-license-number'] );
        update_user_meta($customer_id, 'practitioner_license_number', $pln);
    }
}
add_action('woocommerce_created_customer', 'reigel_woocommerce_checkout_update_user_meta', 10, 2);

practitioner-license-number 是我使用 woocommerce_form_field 方法添加的自定义字段.我修改了上面链接中的代码,以便该函数在 woocommerce_created_customer 上运行,然后在 update_user_meta 中我试图传递客户 ID,以便将元保存到创建的用户配置文件.

practitioner-license-number is my custom field added using the woocommerce_form_field method. I modified the code at the link above, so that the function runs on woocommerce_created_customer and then in update_user_meta I'm trying to pass the customer ID so that it saves the meta to the user profile that is created.

希望能帮助您解决这个问题.大多数关于此主题的问题/答案都假设用户已登录并已拥有帐户,但此处并非如此.

Would appreciate help trying to solve this. Most of the questions/answers on this subject assume that the user is logged in and already has an account, which is not the case here.

推荐答案

从你分享的代码来看,我认为问题在于错误地传递给了$_POST,可能是 practitioner_license_numberpractitioner-license-number.

From the code you share, I think the problem lies in incorrectly passing to the $_POST, perhaps practitioner_license_number vs practitioner-license-number.

您可以使用下面的代码来查看您的钩子是否有效,如果该值写入数据库中,您就知道问题确实出在其他地方.

You can use the code below to see if your hook works, if the value is written in the database you know that the problem is indeed elsewhere.

分享您用于自定义字段的代码也可能有帮助吗?

It might help to share the code you used for the custom fields too?

function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
    $pln = 'test';
    update_user_meta( $customer_id, 'practitioner_license_number', $pln);
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 ); 

这篇关于对于在结帐时注册的新用户,将自定义字段保存到 WooCommerce Checkout 上的用户元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆