How to add custom fields to WooCommerce registration form

后端 未结 3 781
暗喜
暗喜 2020-12-10 09:32

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

I am trying to add custom fields to WooCommerce registration form, specifically first and last na

3条回答
  •  误落风尘
    2020-12-10 09:58

    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.

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

    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() {
        ?>
        

    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 * 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', __('Error: First name is required!', 'text_domain'));
        }
    
        if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
            $validation_errors->add('billing_last_name_error', __('Error: 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');
    

    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!

提交回复
热议问题