How to add custom fields in user registration on the “My Account” page

It’s possible to fully customize the registration form on the “My Account” page starting in WooCommerce 2.1.

To begin you must enable the registration:

registration-options

By default the form is displayed as follows:

my-account-fields

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 “First name”, “Last name”, and “Phone number” fields:

Note that we use fields as billing_* to be able to associate with the fields customer’s account on WooCommerce later.

Here is the list of all the fields that we can use:

  • billing_first_name
  • billing_last_name
  • billing_company
  • billing_address_1
  • billing_address_2
  • billing_city
  • billing_postcode
  • billing_country
  • billing_state
  • billing_email
  • billing_phone

Now the registration form looks like:

new-registration-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:

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 the woocommerce_created_customer action:

And voila! All the fields saved and working perfectly 🙂

2 comentários sobre “How to add custom fields in user registration on the “My Account” page

Os comentários estão desativados.