In this article we’ll see what’re customer attributes and how we add them programmatically in Magento 2. So we’ll proceed with following points :
- What’re customer attributes
- Why we need to add them programmatically
- Steps to create customer attribute.
1. What’re customer attributes
In Magento, fields which are used in registration, login and filling customer address are basically customer attributes. They’re further divided into customer attributes and customer address attributes. Now if attributes used in customer registration / login are customer attributes and attribute used in address information for customers are customer address attributes. Some examples of customer attributes are below :
a. Customer Attributes
– First Name, Last Name, DOB etc
b. Customer Address Attributes
– Zip, City, Street, Country, Region/State etc
2. Why we need to add them programmatically
Magento by default didn’t provide functionality to add customer attributes in CE and sometime our project requirement have to add some extra fields(customer attributes) in registration form or address form. To do so we need to add them programmatically or through third party modules.
3. Steps to create customer attribute.
Let see how we can create customer attributes programmatically in Magento 2 step by step :-
Step 1: Create setup file InstallData.php
Firstly, we will create the InstallData.php file:
In this class, we define the EAV setup model which will be use to interact with Magento 2 attribute.
Step 2: Define the install() method
After that, we have to define the install() method and create eav setup model:
Step 3: Create custom attribute
Finally, we need to set the forms in which the attributes will be used. In this step, we need define the eavConfig object which allow us to call the attribute back and set the data for it and the full code to create customer attribute is:
Now our final InstallData.php looks like below :-
Note: We are using InstallData script (but not InstallSchema or UpgradeSchema) here as we are not going to change anything related to schema (database/table structure). When the script is run, the attribute is created by creating a new entry in
eav_attribute (as Magento uses EAV ORM).
Step 4: Show Customer Attribute in Register form
For showing this customer attribute on the registration page, we need to do following things :
We will add our phtml files to ‘form.additional.info’ reference name using Excellence/Customerattr/view/frontend/layout/customer_account_create.xml
Explaination : we’re not overriding the entire vendor/magento/module-customer/view/frontend/templates/form/register.phtml file. Instead we used container name form.additional.info as reference and adding a new block to it. ‘form.additional.info’ is being called in register.phtml using this code getChildHtml(‘form_additional_info’); ?>
So by above xml we can simply add new attribute fields with editing register.phtml of core or without overriding it and getChildHtml(‘form_additional_info’); ?> automatically read content of ‘form.additional.info’ child block.
Now, in the
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.
Now, when a user creates an account along with the GST number, it gets stored into the database in
customer_entity_varchar. This table contains following field:
value_id: It is just the auto-increament primary key for the table.
attribute_id: The attribute’s id which we had previosuly seen in eav_attribute table.
entity_id: It will contain the customer id.
value: It contains the value for the attribute which was filled while registration.
Now the value that was entered can be seen in admin panel as well. Navigate to Customers -> All Customers and click on edit for the newly created customer. Here, in the account information tab, you will find the field for this attribute and the value that was entered while creating the account as well.
That’s all for adding customer attribute programmatically.