Home » WooCommerce: Add First & Last Name to My Account Register Form

WooCommerce: Add First & Last Name to My Account Register Form

by Tutor Aspire

Here’s another useful PHP snippet that adds the Billing First Name and Billing Last Name to the Registration Form on the WooCommerce My Account page.

This is a great first step. If you learn to add these simple text fields, you can then add any custom input field to the form such as dropdowns, radio buttons, checkboxes – and link these to the relevant WooCommerce/WordPress user fields. Enjoy!

Add name fields to the WooCommerce Registration form

PHP Snippet: Add First & Last Name to WooCommerce My Account Registration Form

/**
 * @snippet       Add First & Last Name to My Account Register Form - WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WC 3.9
 * @donate $9     https://www.tutoraspire.com
 */
 
///////////////////////////////
// 1. ADD FIELDS
 
add_action( 'woocommerce_register_form_start', 'tutoraspire_add_name_woo_account_registration' );
 
function tutoraspire_add_name_woo_account_registration() {
    ?>
 
    

add( 'billing_first_name_error', __( 'Error: First name is required!', 'woocommerce' ) ); } if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) { $errors->add( 'billing_last_name_error', __( 'Error: Last name is required!.', 'woocommerce' ) ); } return $errors; } /////////////////////////////// // 3. SAVE FIELDS add_action( 'woocommerce_created_customer', 'tutoraspire_save_name_fields' ); function tutoraspire_save_name_fields( $customer_id ) { if ( isset( $_POST['billing_first_name'] ) ) { update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) ); } if ( isset( $_POST['billing_last_name'] ) ) { update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); update_user_meta( $customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']) ); } }

You may also like