How to add First name and Last Name Field in Register My Account Page? One easy way to do this is by adding a code in functions.php in your theme.
This simple addition can make a difference on registration and how you connect with your customers and personalize their shopping experience.
Here’s the step on how you can easily add a first and last name field to your register my account page.
Preview
Step 1: Enable the allow customer to create an account
go to to your WordPress admin in WooCommerce settings, in Account and Privacy tab
just check and uncheck as you see on the screenshot
Step 2: add code on functions.php
Go to your theme directory and add the code below
add_action( 'woocommerce_register_form_start', 'custom_add_fields_in_register' ); function custom_add_fields_in_register() { ?> <p class="form-row form-row-wide"> <label for="reg_billing_first_name"><?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'] ); ?>" /> </p> <p class="form-row form-row-wide"> <label for="reg_billing_last_name"><?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'] ); ?>" /> </p> <?php } add_filter( 'woocommerce_registration_errors', 'custom_add_fields_in_register_validate' , 10, 3 ); function custom_add_fields_in_register_validate($errors, $username, $email) { if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) { $errors->add( 'billing_first_name_error', __( 'First name is required!', 'woocommerce' ) ); } if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) { $errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'woocommerce' ) ); } return $errors; } add_action( 'woocommerce_created_customer', 'custom_add_fields_in_register_save' ); function custom_add_fields_in_register_save($customer_id) { $billing_first_name = ucfirst($_POST['billing_first_name']); $billing_last_name = ucfirst($_POST['billing_last_name']); if ( isset( $billing_first_name ) ) { // WordPress default first name field. update_user_meta( $customer_id, 'first_name', sanitize_text_field( $billing_first_name ) ); // WooCommerce billing first name. update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $billing_first_name ) ); } if ( isset( $_POST['billing_last_name'] ) ) { // WordPress default last name field. update_user_meta( $customer_id, 'last_name', sanitize_text_field( $billing_last_name ) ); // WooCommerce billing last name. update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $billing_last_name ) ); } update_user_meta( $customer_id, 'nickname', sanitize_text_field( $billing_first_name . ' ' . $billing_last_name ) ); }
Adding a first and last name field to your WooCommerce register page is a simple way to collect more customer information and personalize their shopping experience. Try out these steps today to enhance your store’s registration process!