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:

21 thoughts on “Magento 2: How To Make Customer Attribute ?

  1. David

    Hello,

    Nice tutorial, thanks for the good work!
    But after the update from 2.1.7 to 2.1.9, register.phtml form was not working anymore. The custom register.phtml file was missing the form key.

    I added
    getBlockHtml(‘formkey’); ?>
    just after the tag, and it’s working fine.

    Same change has to be made on the custom edit.phtml.

    Best regards,
    David

    Reply
  2. Pingback: Magento 2.1.3 How To Make Customer Attribute [Update] | Magento Extensions

  3. DanielSEM

    Hello,

    I try it, but now I have an error on checkout:

    Class Magento\Customer\Block\Address\Renderer\AbstractAddress does not exist

    How I can resolve it?

    Thanks!

    Reply
  4. Luca

    Hi, how do I update previously saved customer attributes?

    They were saved during data migration from mag 1 to mag 2, now I need to update them and set them to not required

    Reply
  5. Ritu

    I have been trying to make it work for account information form in the front end. However, once the fields are added, the system logs out the user(or so it shows the customer login page) and does not progress from here.

    Any pointers would be appreciated.

    Reply
  6. Ankit Shah

    I have installed your module from GitHub. But it’s not showing in DB or Backend new fields.

    Using Magento 2 CE 2.1.0.

    Cleared cache many times.

    Reply
  7. Abdul

    Hi,

    In my local machine i have placed all folders then launched magento upgrade and clean cache.
    php bin/magento setup:upgrade
    php bin/magento cache:flush –all

    But still i dont get any files shown in the additional information in admin area.
    I checked in the “Customer_entity_varchar” table after the upgrade, but i dont see any column created in it.
    Please let me know if anything i am missing.

    Thanks in advance

    Reply
  8. Tadeo Barranco

    I do this and it works very well, the new customer attribute is added, but when I tried to load the customer data this new attribute doesn’t appear.

    Can you tell me why? Or how can I access to this attribute in a model, controller or block?

    I am creating an instance of a Magento\Customer\Model\Customer with an object manager but i can’t see my custom attribute in the data.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *