Magento 2 How to create a customer attribute

Previously, we published a pre-release Magento 2 article titled "How to Create a Customer Attribute?" Since then, Magento has undergone several updates, and we have updated our files accordingly to align with the latest Magento 2.1.3 release. For more detailed information, please refer to our previous article.

Update For Magento 2 How To Make Customer Attribute

In this article, we will present the updated codebase in accordance with the Magento 2.1.3 upgrade. Additionally, it's worth noting that Magento 2.1.x has introduced some updates to its CLI commands. For detailed information on these commands, we recommend referring to our previous article titled "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
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.

Conclusion

Due to several updates in Magento, the code provided in the previous article may no longer function correctly. However, the article includes a detailed explanation of the code, and there is a video tutorial demonstrating how to create custom customer attributes. If you are currently working with Magento 2.1.x versions and intend to create custom customer attributes, please refer to the code provided above, along with the relevant Magento 2 CLI commands. By following these instructions, you should be able to create custom attributes without encountering any issues. If you encounter any difficulties during the process, please feel free to leave a comment in the section below the article. I will be more than happy to assist you in resolving any problems you may face.

Video

Created On January 24, 2017
You May Also Like