In this post I am going to explain how to add customer attribute programmatically in magento 2.

First we need to create the setup file InstallData.php

File path: app/code/Ht/Mymodule/Setup/InstallData.php

We have created the three custom customer attributes.

Custom Text Field -> Text Box
Custom Yes/No -> Yes/No Field
Custom Dropdown -> Select Box

“Custom Dropdown” is a select box and we have defined a custom source: Ht\Mymodule\Model\Source\Customdropdown

So, we need to create the source file as well.

File path: app/code/Ht/Mymodule/Model/Source/Customdropdown.php

 

At Final, Open terminal/SSH and navigate to Magento 2 setup root directory and run below commands.

 

Magento 2 - How to Add Customer Attribute Programmatically