Blog

How to add custom fields to user registration form (my account) in Woocommerce?

Adding new fields

Appears quite modest and without many fields, but we can add additional fields using the actions:

  • woocommerce_register_form_start – Appears before the “Address Email” field
  • woocommerce_register_form – Appears after the “Password” field

For example, let’s add “EIN Number”, “Company Name”  fields:

/**
 * Add new register fields for WooCommerce registration.
 *
 * @return string Register fields HTML.
 */
function wooc_extra_register_fields() {
 ?>

 <p class="form-row form-row-wide">
 <label for="billing_ein_number"><?php _e( 'EIN Number', 'woocommerce' ); ?> </label>
 <input type="text" class="input-text" name="billing_ein_number" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_ein_number'] ) ) esc_attr_e( $_POST['billing_ein_number'] ); ?>" />
 </p>

 <p class="form-row form-row-wide">
 <label for="billing_company_name"><?php _e( 'Company Name', 'woocommerce' ); ?> </label>
 <input type="text" class="input-text" name="billing_company_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_company_name'] ) ) esc_attr_e( $_POST['billing_company_name'] ); ?>" />
 </p>
 
 <?php
}

add_action( 'woocommerce_register_form', 'wooc_extra_register_fields' );

Validating the custom fields

It’s very important to validate the fields and make sure that the user is filling them. To do this we will use the woocommerce_register_post action

See an example of how to validate our custom fields:

/**
 * Validate the extra register fields.
 *

 */
function wooc_validate_ewt_extra_register_fields( $username, $email, $validation_errors ) {
	if ( isset( $_POST['billing_ein_number'] ) && empty( $_POST['billing_ein_number'] ) ) {
		$validation_errors->add( 'billing_ein_number_erro', __( '<strong>Error</strong>:  ein number', 'woocommerce' ) );
	}

	if ( isset( $_POST['billing_company_name'] ) && empty( $_POST['billing_company_name'] ) ) {
		$validation_errors->add( 'billing_company_name_error', __( '<strong>Error</strong>:  Company name  is required!.', 'woocommerce' ) );
	}

 
}

add_action( 'woocommerce_register_post', 'wooc_validate_ewt_extra_register_fields', 10, 2 );

Saving custom fields

Finally we just need to save the custom fields in the database.

We should save that custom fields in user profile and for do this we’ll use thewoocommerce_created_customer action:

/**
* Save the extra register fields.
 *
 * @param  int  $customer_id Current customer ID.
 *
 * @return void
 */
function wooc_save_ewt_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_ein_number'] ) ) {
update_user_meta( $customer_id, 'einno', sanitize_text_field( $_POST['billing_ein_number'] ) );
}
if ( isset( $_POST['billing_company_name'] ) ) {
update_user_meta( $customer_id, 'cmpnyname', sanitize_text_field( $_POST['billing_company_name'] ) );
}
}

add_action( 'woocommerce_created_customer', 'wooc_save_ewt_extra_register_fields)

now how can administrator view the filed and update that

have to use these 2 hook


/**
* Show the extra register fields in admin profile.
 *
 * @param  int  $user Current customer.
 *
 * @return void
 */ 
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

 <h3>Extra profile information</h3>

 <table class="form-table">
 <tr>
 <th><label for="verify">Verify EIN Number</label></th>

 <td>
 <input type="checkbox" name="verifyein" id="verifyein" value="1" <?php if (esc_attr(get_the_author_meta('verifyein',$user->ID )) == '1' ) { ?>checked="checked"<?php }?> /> 
 
 </td>
 </tr>
 <tr>
 <th><label for="ein no">EIN Number</label></th>

 <td>
 <input type="text" name="einno" id="einno" value="<?php echo esc_attr( get_the_author_meta( 'einno', $user->ID ) ); ?>" class="regular-text" /> 
 </td>
 </tr>
 <tr>
 <th><label for="company name">Company Name</label></th>

 <td>
 <input type="text" name="cmpnyname" id="cmpnyname" value="<?php echo esc_attr( get_the_author_meta( 'cmpnyname', $user->ID ) ); ?>" class="regular-text" /> 
 </td>
 </tr>


 </table>
<?php }
 
add_action( 'personal_options_update', 'my_save_extra_profile_fields1' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields1' );


function my_save_extra_profile_fields1( $user_id ) { 
 
if(isset($_POST['verifyein'])){
 update_user_meta($user_id, 'verifyein', $_POST['verifyein'] );
}
else
{
 update_user_meta($user_id, 'verifyein', 0);
}

 if ( isset( $_POST['einno'] ) ) { 
 update_user_meta( $user_id, 'einno', $_POST['einno'] );
 }
if ( isset( $_POST['cmpnyname'] ) ) { 
 update_user_meta( $user_id, 'cmpnyname', $_POST['cmpnyname'] );
 }
 
}

 

Leave a Reply