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

<?php

namespace Ht\Mymodule\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface 
{
    private $eavSetupFactory;

    public function __construct(
        EavSetupFactory $eavSetupFactory,
        Config $eavConfig
    ) 
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        // Text Field
        $eavSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_text_field', [
            'label' => 'Custom Text Field',
            'system' => 0,
            'position' => 700,
            'sort_order' => 700,
            'visible' => true,
            'note' => '',
            'type' => 'varchar',
            'input' => 'text',
            ]
        );

        $this->getEavConfig()->getAttribute('customer', 'custom_text_field')->setData('is_user_defined', 1)->setData('is_required', 0)->setData('default_value', '')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])->save();

        // Dropdown Field
        $eavSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_dropdown', [
            'label' => 'Custom Dropdown',
            'system' => 0,
            'position' => 710,
            'sort_order' => 710,
            'visible' => true,
            'note' => '',
            'type' => 'int',
            'input' => 'select',
            'source' => 'Ht\Mymodule\Model\Source\Customdropdown',
            ]
        );

        $this->getEavConfig()->getAttribute('customer', 'custom_dropdown')->setData('is_user_defined', 1)->setData('is_required', 0)->setData('default_value', '')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])->save();

        // Yes/No Field
        $eavSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_yes_no', [
            'label' => 'Custom Yes/No',
            'system' => 0,
            'position' => 720,
            'sort_order' => 720,
            'visible' => true,
            'note' => '',
            'type' => 'int',
            'input' => 'boolean',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            ]
        );

        $this->getEavConfig()->getAttribute('customer', 'custom_yes_no')->setData('is_user_defined', 1)->setData('is_required', 0)->setData('default_value', '')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])->save();
    }
    
    public function getEavConfig() {
        return $this->eavConfig;
    }
}

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

<?php

namespace Ht\Mymodule\Model\Source;

<?php

namespace Ht\Mymodule\Model\Source;

class Customdropdown extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource 
{
    public function getAllOptions() 
    {
        $type = [];
        $type[] = [
                'value' => '',
                'label' => '--Select--'
            ];
        $type[] = [
                'value' => 4,
                'label' => 'Option 1'
            ];
        $type[] = [
                'value' => 3,
                'label' => 'Option 2'
            ];
        return $type;
    }
    
    public function getOptionText($value) 
    {
        foreach ($this->getAllOptions() as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
        return false;
    }
}

 

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

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f

 

Magento 2 - How to Add Customer Attribute Programmatically