When a new customer registers at your Magento 2 online shop, he is required to fill in certain fields, such as first name, last name, email. He or she may want to provide some additional information. For that purpose, we need to create our own fields which will be used in a registration form. If you have Magento 2 Commerce version, new fields can be created by adding new customer attributes in the backoffice.
I will look into the following aspects:
Add new customer attribute in Magento 2 Commerce
In the backoffice menu click Stores and choose Customer under Attributes subcategory.
You’ll see a list of customer attributes. Perhaps the attribute you want to create already exists, but is currently disabled. So all you need to do is to switch it on.
If you haven’t found the attribute you need, click the Add new attribute button.
Let’s suppose we have an online shop selling travelling equipment and we need to create a field asking if a customer ready to travel.
Enter the name of the field in Default Label and an Attribute code that must be unique.
Then, choose from different types of input:
- Text Field,
- Text Area,
- Multiple Line,
- Multiple Select,
- File (attachment),
- Image File.
I will choose Yes/No input type.
You will need to complete other settings as well:
- Values Required;
- Default Value;
- Input Validation. You can choose whether the field must be validated (if not – leave “None”), and if yes – then by which parameter: Alphanumeric (should contain letters, numbers, punctuation marks), Numeric Only, Alpha Only (only letters), URL, Email, Length Only;
- Input/Output Filter: None, Strip HTML tags, Escape HTML entities (e.g. < will be replaced with < and so on).
In our case the field won’t contain any input text, that’s why we don’t fill in both Input Validation and Input/Output Filter.
Storefront properties block of settings is necessary to fill in, if your attribute will become a field in forms for your customers on the site. If yes, you can choose the forms where it must appear. You should also set up the position of the field in the form (I set it at 5).
So let’s check out what we get after adding our attribute. Save it and open registration form.
Here’s our field, it’s 5th in the list. It looks the way we wanted to, with “yes/no” dropdown.
Appearance in Customer Grid
There’s another block of settings concerning customer grid.
Let’s go to attribute settings, enable Add to Column Options and save.
Go to Customers -> All Customers, and you will see the Customer Grid, an indexed version of it. To see the changes we made, update the index first. In console use command bin/magento indexer:reindex customer_grid.
After rebuilding the index you’d expect to see the new customer attribute in the columns, but it’s still not there.
In the Customer Grid you can add a column from existing attributes. Press Columns button and tick our new attribute “Ready for travel?”.
Afterward you’ll be able to see the new attribute as a column.
You can create a label name in different languages and set up its reflection depending on the country of each customer. Go to Manage Label / Options tab.
Customer Address Attribute
Bear in mind that if you wish to add an attribute concerning customer’s address, you should create a Customer Address Attribute. In backoffice menu click Stores and choose Customer Address under Attributes heading. From then on the mechanism of working with an attribute in the same as has been already described above.
Question: Can’t export csv and xml with a custom attribute
Answer: Magento 2 export creates a file only with those columns that are visible in customer grid. So add the column to the customer grid as it was described in this article.