Magento 2.1.3 Make Customer Attribute [Update]

Magento 2.1.3 How To Make Customer Attribute
We had the post for pre-release Magento 2 - "How to make customer attribute?". After this time Magento has several updates and we did update of files according to the latest Magento 2.1.3 release. The more detailed information can be found in the previous article.

Update For Magento 2 How To Make Customer Attribute

This article will show updated codebase accordign with Magento 2.1.3 upgrade. Also, Magento 2.1.x has update to Magento 2 CLI commands, you can look them at out previous article: "The Top 7 Magento 2 CLI Commands You Should Know"

Updates to the files from previous article:

There are 3 files. One more file added. Please don't forget to make it. It allows extension to be visible for Magento 2. app/code/Sashas/CustomerAttribute/etc/module.xml app/code/Sashas/CustomerAttribute/Setup/InstallData.php app/code/Sashas/CustomerAttribute/registration.php

Updates for Magento 2 CLI commands

The example of commands shown for a store with developer mode enabled. If you are using production mode please perform additional commands for dependency injection, static content deploy, clean cache and reindex indexers. You can find these commands at the Our Magento 2 CLI Article
After you created files you will need to run following commands: php bin/magento setup:upgrade php bin/magento cache:flush (if you had cache enabled) php bin/magento indexer:reindex In case any of Magento 2 CLI commands have error please check permissions of bin/magento and path to php

Result: New Custom Customer Attribute At Magento 2

As you can see in the code we set that attribute should be used in forms: adminhtml_customer
Magento 2 How To Make Customer Attribute
If don't you want to use it at the Customer Edit and Customer Registration Page then you need to remove customer_account_edit and customer_account_create (Line 75)
Instead of ['adminhtml_customer', 'customer_account_edit', 'customer_account_create'] it will be: ['adminhtml_customer'] In another case if you want to use customer attribute at the Customer Edit and Customer Registration Page then you will need to rewrite .phtml templates for these forms at the: vendor/magento/module-customer/view/frontend/templates/form/edit.phtml and vendor/magento/module-customer/view/frontend/templates/form/register.phtml To achieve this you need to make folder structure at your theme app/design/frontend/{theme_company}/{theme_name}/Magento_Customer/templates/form/ and copy files above there. After this, you can add fields similar way as Magento have in the default template. The input field name will be our custom attribute code.


After Magento perform several updates the old article code does not work anymore, but article has explanation of code and video were shown how to make it. As for now when you going to make custom customer attribute for Magento 2.1.x versions please use the code above as well as Magento 2 CLI commands. I hope this information will help you to create custom attributes without any issues. In case you still have troubles please post it in comments section and I will try to help you.
Created On January 24, 2017
You May Also Like