Magento 2: How To Make Customer Attribute ?

Magento 2 How to make customer attribute
Since Magento 2 is pretty close to release we can review how the adding of custom attribute process has been changed. Today we will make Magento 2 extension which adds customer attribute into customer edit page at admin. To start with it we will have to have Magento 2 installed with Demo data. You can find Magento 2 Installation Guide at Magento 2 Developer Documentation.

UPD. For Magento 2.1 & newest editions please read updated article.

1. Making Magento 2 extension skeleton

The extension files now will be at the app/code/{Company_Name}/{Extension_Name} location. Where {Company_Name} and {Extension_Name} our values. Exactly same way was in Magento 1.x . For our extension we will use Company name Sashas and Extension name CustomerAttribute . Once we will create folders the path to our extension will be app/code/Sashas/CustomerAttribute/ Folder structure inside extension folder is very similar from Magento 2.x to Magento 1.x. There are same Block, Controller, etc, Helper and Model folders. In our case we will need to make etc and Setup folder. The final folder tree for our Magento 2 extension will look this way: Magento 2 Customer Attribute Now we need to create module.xml file for our extension to declare version and make it visible for Magento 2. The module.xml file in Magento 2 replacement for previous module file at app/etc/modules folder. This way now all files related to our extension will be in the one folder.  

2. Magento 2 Customer Attribute Module Files

The first file we will need to make it is app/code/Sashas/CustomerAttribute/etc/module.xml This file will make our extension visible for Magento and there we can declare dependency our customer attribute extension  from the Magento_Customer module. Also you can notice there that at <module/> section we have definition of extension name and version. The second and the list file we will add its is Installation script. It will have path app/code/Sashas/CustomerAttribute/Setup/InstallData.php This file i had issues when I tried it the first time, but thanks to Magento 2 team they helped to figure it out. All action there happens at the install function where we add attribute for entity type "customer" and after it we set this attribute to be used in form "adminhtml_customer". Customer attributes can be used in different forms, this example will show customer attribute at the admin customer edit page.  You can also use also other forms. For example: "used_in_forms" =>  ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'] The final code of the InstallData.php file will look like this: In the example we use attribute code "magento_username" and you can see at Lines 68-76 we set properties of the attribute. The properties identical to the Magento 1.x values. After we done with code we need to launch magento upgrade and clean cache.  Launch Magento 2 upgrade we can from the shell by using command:
php bin/magento  setup:upgrade
This will go thru installed extension and perform upgrade or extension installation with data and database tables. Clean cache we can also with shell command:
 php bin/magento  cache:flush --all
 

3. The result

In the result we can see our field at the admin of Magento 2 customer edit page, it is in General Tab: Magento 2 Customer Attribute I hope this article would help you with exploring new Magento 2 platform. You can also access extension code at the GitHub

4. Still having difficulties? Watch how it was done on Video:

Created On July 25, 2015