如何将自定义字段添加到 WooCommerce 注册表单 [英] How to add custom fields to WooCommerce registration form

查看:45
本文介绍了如何将自定义字段添加到 WooCommerce 注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过类似的问题,但找不到适合我的解决方案.

I have seen similar questions but couldn't find a solution for me.

我正在尝试将自定义字段添加到 WooCommerce 注册表单,特别是名字和姓氏字段.我已经设法创建了这些字段,但是当用户登录时,输入的信息不会传递到帐户详细信息"页面.其他教程提到了验证这些字段,但我不确定它是否与我相关.我正在开发 Wordpress 子主题.

I am trying to add custom fields to WooCommerce registration form, specifically first and last name field. I have managed to create these fields but the information entered does not pass over to the Account Details page when the user has logged in. Other tutorials have mentioned validating the fields but I am not sure is it relevant to me or not. I am working on a Wordpress child theme.

请访问 codepad .org 查看代码.我尝试使用代码示例选项将代码粘贴到此处,但无法正常工作.

Please visit to codepad .org to view the code. I tried to paste the code here by using the code sample option but it doesn't work properly.

希望我已经清楚地解释了自己.如果不是,请告诉我,我会澄清.

Hope I have explained myself clearly. If not please let me know and I will clarify.

推荐答案

我认为你已经覆盖了 woocommerce/templates/myaccount/form-login.php 模板,并且通过这样做你已经设法显示 billing_first_namebilling_last_name 但您忘记使用 woocommerce_created_customer 将这些数据保存到数据库中所需的钩子.

I think you have overwrite woocommerce/templates/myaccount/form-login.php template, and by doing that you have managed to show the billing_first_name and billing_last_name but you forget to use woocommerce_created_customer hook which is needed to save those data into your database.

我的建议是保持模板不变,并通过 function.php

What I'll suggest is that you keep the template as it is and add those field through function.php

这是在 WooCommerce 注册表单中添加自定义字段的代码:

Here is the code to add custom fields in WooCommerce registration form:

/**
 * To add WooCommerce registration form custom fields.
 */

function text_domain_woo_reg_form_fields() {
    ?>
    <p class="form-row form-row-first">
        <label for="billing_first_name"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" />
    </p>
    <p class="form-row form-row-last">
        <label for="billing_last_name"><?php _e('Last name', 'text_domain'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
    </p>
    <div class="clear"></div>
    <?php
}

add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');

关于验证问题的第二部分,它是完全可选的,取决于您想要什么的业务逻辑,通常大多数站点都需要名字和姓氏,但同样完全取决于您,如果您不想验证这一点,请从上面的代码中删除 <span class="required">*</span> 并跳过此部分.

Coming to your second part of your question on validation it is totally optional and depends on your business logic that what you want, in general most of the site has First Name and the Last Name required but again it totally depends upon you, If you don't want to validate this then remove <span class="required">*</span> from above code and skip this section.

/**
 * To validate WooCommerce registration form custom fields.
 */
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) {
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
        $validation_errors->add('billing_first_name_error', __('<strong>Error</strong>: First name is required!', 'text_domain'));
    }

    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
        $validation_errors->add('billing_last_name_error', __('<strong>Error</strong>: Last name is required!.', 'text_domain'));
    }
    return $validation_errors;
}

add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);

现在这是一个主要部分,这是您错过的部分,需要以下代码来保存自定义数据:

Now this is a main part and this what you was had missed, below code is needed to save the custom data:

/**
 * To save WooCommerce registration form custom fields.
 */
function text_domain_woo_save_reg_form_fields($customer_id) {
    //First name field
    if (isset($_POST['billing_first_name'])) {
        update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
        update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
    }
    //Last name field
    if (isset($_POST['billing_last_name'])) {
        update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
        update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
    }
}

add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');

以上所有代码都在活动子主题(或主题)的 function.php 文件中.或者也可以在任何插件 php 文件中.
代码经过测试且功能齐全.
希望这会有所帮助!

All the above code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
The code is tested and fully functional.
Hope this helps!

这篇关于如何将自定义字段添加到 WooCommerce 注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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