Adding First & Last Name Extra Fields

Looking to add extra WooCommerce registration fields such as first name, last name and phone number? All you need is to add this code to your functions.php file.

Adding extra WooCommerce registration fields

To add the extra WooCommerce registration fields, you should add the code below to your theme’s function.php file.

function custom_extra_register_fields() { ?>
     <p class="form-row form-row-wide">
          <label for="reg_billing_first_name" class="screen-reader-only"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
          <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" placeholder="First Name*" />
     </p>
     <p class="form-row form-row-wide">
          <label for="reg_billing_last_name" class="screen-reader-only"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
          <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" placeholder="Last Name*" />
     </p>
     <p class="form-row form-row-wide">
          <label for="reg_billing_phone" class="screen-reader-only"><?php _e( 'Phone', 'woocommerce' ); ?></label>
          <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" placeholder="Phone Number" />
     </p>
     <div class="clearfix"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'custom_extra_register_fields' );

function custom_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', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
     }
     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!.', 'woocommerce' ) );
     }
     return $validation_errors;
}
add_action( 'woocommerce_register_post', 'custom_validate_extra_register_fields', 10, 3 );

function custom_save_extra_register_fields( $customer_id ) {
     if ( isset( $_POST['billing_phone'] ) ) {
          // Phone number input for WooCommerce
          update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
     }
     if ( isset( $_POST['billing_first_name'] ) ) {
          // First name input for WordPress
          update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
          // First name input for WooCommerce
          update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
     }
     if ( isset( $_POST['billing_last_name'] ) ) {
          // Last name input for WordPress
          update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
          // Last name input for WooCommerce
          update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
     }

}
add_action( 'woocommerce_created_customer', 'custom_save_extra_register_fields' );