如何将自定义字段添加到WooCommerce注册表单

问题描述 投票:2回答:1

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

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

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

希望我已经清楚地解释了自己。如果没有,请告诉我,我会澄清。

php wordpress forms woocommerce hook-woocommerce
1个回答
8
投票

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

我建议你保持模板不变,并通过function.php添加这些字段

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

/**
 * 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>并跳过本节。

/**
 * 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);

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

/**
 * 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文件中。 代码经过测试并且功能齐全。 希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.