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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <?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' => , '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', )->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' => , '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', )->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' => , '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', )->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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?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.
1 2 | php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f |
Leave a Reply