Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

How to Add Custom Field in WooCommerce Registration Form

First of all, enable the registration form from backend WooCommerce settings. Go to Dashboard->WooCommerce->Settings->Account and find Enable customer registration on the “My account ” page and make it checked.

After do that, You can see registration form at the frontend. You can see it display Email address and password field. If you want to add more field that what to to. So here is code. Add below line of code at the end of your theme’s functions.php file of your active child theme (or theme) and Save the file.

I was add “first name”, “last name” and “Phone number” field in this example. To relate these registration form fields with the billing address, you have to include the prefix “billing_” before the field name.

function wdo_extra_register_fields() {?>
      
       

If you refresh this page you can see the extra fields being added to WooCommerce registration form.

Now you want to validate these extra added field. Let's do this. Add this following line of code at the end of your theme’s functions.php file of your active child theme (or theme) and Save the file.

function wdo_validate_extra_register_fields( $username, $email, $validation_errors ) {
 
      if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
 
             $validation_errors->add( 'billing_first_name_error', __( 'First name is required!', 'woocommerce' ) );
 
      }
 
      if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
 
             $validation_errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'woocommerce' ) );
 
      }
         return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wdo_validate_extra_register_fields', 10, 3 );

After You want to save this value to database. So let's have a look. Add this following line of code at the end of your theme’s functions.php file of your active child theme (or theme) and Save the file.

function wdo_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      }
 
}
add_action( 'woocommerce_created_customer', 'wdo_save_extra_register_fields' );

Once loging you, you can find these fields on account details edit page. You can see the values from registration form already being populated.

Exit mobile version