This is the code that I’m using in my functions.php file to add the name fields BTW:
<?php
/** ----------------------------------------------------------
* Add new register fields for WooCommerce registration.
*
* @return string Register fields HTML.
*
* Instructions from here:
* https://support.woothemes.com/hc/en-us/articles/203182373-How-to-add-custom-fields-in-user-registration-on-the-My-Account-page
* ----------------------------------------------------------------- */
function wooc_extra_register_fields() {
/* I'm using FNAME as the field name for firstname so that MailChimp receives it */
?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="mc4wp-FNAME" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['mc4wp-FNAME'] ) ) esc_attr_e( $_POST['mc4wp-FNAME'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="mc4wp-LNAME" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* Validate the extra register fields.
*
* @param string $username Current username.
* @param string $email Current email.
* @param object $validation_errors WP_Error object.
*
* @return void
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['mc4wp-FNAME'] ) && empty( $_POST['mc4wp-FNAME'] ) ) {
$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' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Save the extra register fields.
*
* @param int $customer_id Current customer ID.
*
* @return void
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['mc4wp-FNAME'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['mc4wp-FNAME'] ) );
// WooCommerce billing first name.
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['mc4wp-FNAME'] ) );
// Set default display name.
update_user_meta( $customer_id, 'nickname', sanitize_text_field( $_POST['mc4wp-FNAME'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// WordPress default last name field.
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// WooCommerce billing last name.
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
/* ---- this code sets the first name as the display name ---- */
add_filter('pre_user_display_name','default_display_name');
function default_display_name($name) {
if ( isset( $_POST['mc4wp-FNAME'] ) ) {
$name = sanitize_text_field( $_POST['mc4wp-FNAME'] );
}
return $name;
}
?>