How to add custom fields to the Customer Registration page in Magento 2

By default, Magento 2 allows customers to create a new account with some required info such as email addresses and passwords. However, we can easily add some more custom fields to the registration page of Magento 2 to collect more customer data.

1. Customer Attributes

Basically, customer attributes provide the information that is required to support the order, fulfillment, and customer management processes. Because your business is unique, you might need more fields in addition to those provided by the system. You can add custom attributes to the Account Information, Address Book, and Billing Information sections of the customer’s account. Customer address attributes can also be used in the Billing Information section during checkout, or when guests register for an account.

Custom customer attributes are necessary to make Magento 2 suit with your type of eCommerce.

2. Code and Explanation

Here is the situation:

Imagine that you need more customer information (E.g: phone number, ID number, etc.) for further confirmation, the most simple way is to create new fields in the customer registration page so that your customer can fill in. To make it work, you have to create a new customer attribute then add a new field into your customer registration page.

Note: This content is for anyone who has some basic knowledge in PHP and Magento 2. If you are not or just wanna see the result, you can download the code, put it in the app/code folder then run the upgrade command.

The code is written for Magento version 2.3 or above.

Step 1: Create new customer attribute

First, you need to create a new attribute to store the data in which the customer registers in the form. We will create a datapatch to create our new customer attribute and add this attribute to the database. In the Setup/Patch/Data/AddPhoneAttribute.php

When you increase your version number and run the upgrade command. The script will be executed. We create an attribute with code id is phone_number and their type id is customer and bind some attribute for it. There are many attributes can be used such as:

  • type: data type of the attribute. Magento 2 allows 6 following datatypes: static, DateTime, decimal, int, text, varchar.
  • input: input type of the attribute will be used in some specific form.
  • required: define the attribute value is required or not.
  • visible: define whether the attribute is appear in backend forms.
  • user_define: define whether admin is allowed to delete the attribute in backend site.
  • position: define the position of attribute in grid
  • system: define where the attribute is system attribute or not .System attributes are the default attributes shipped natively with the software.

After having created the attribute, we bind it to some specific form of Magento 2 by adding data into ‘used_in_form’.

Step 2: Show customer attribute in the customer registration form

For showing customer attribute on the registration page, we need to do the following steps:

First, we will add our phtml files to ‘’ reference name using Magenest/CustomerAttribute/view/frontend/layout/customer_account_create.xml

By default, Magento 2 already creates container for developer custom registration form. We have just created a new block inside that container.

Second, in the /view/frontend/templates/extra_field.phtml file, we will write the code to show the additional textbox.

After performing all of the above tasks, the attribute will be shown on the customer registration page.

Let’s activate the module by running the command php bin/magento setup:upgrade.

Note: When compiling these scripts, there are some unrespected errors that can crash your code. In this case, if you need to run the script again:

  • In the patch_list table in the database, delete the record which has a patch_name value like Magenest\CustomerAttribute\Setup\Patch\Data\AddPhoneAttribute.
  • Then rerun the setup:upgrade command again.

All are done! Check the result.

You can add more fields on the registration page but also on the checkout and account page in Magento 2. This is the smart way to collect more customer data and analyze the insights to identify effective strategies for your business.

Thank you for reading!

Default image
Sagar Doshi
CEO / Managing Partner

An expert in his field, Sagar Doshi is the CEO and team leader in


His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover."

Articles: 26

Leave a Reply