{"id":7257,"date":"2019-09-23T10:24:52","date_gmt":"2019-09-23T04:54:52","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7257"},"modified":"2019-09-26T16:15:02","modified_gmt":"2019-09-26T10:45:02","slug":"magento-2-add-customer-attribute-programmatically","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-customer-attribute-programmatically\/","title":{"rendered":"Magento 2 &#8211; How to Add Customer Attribute Programmatically"},"content":{"rendered":"<p>In this post I am going to explain how to add customer attribute programmatically in magento 2.<\/p>\n<p>First we need to create the setup file InstallData.php<\/p>\n<p>File path: app\/code\/Ht\/Mymodule\/Setup\/InstallData.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Setup;\r\n\r\nuse Magento\\Eav\\Model\\Config;\r\nuse Magento\\Eav\\Setup\\EavSetup;\r\nuse Magento\\Eav\\Setup\\EavSetupFactory;\r\nuse Magento\\Framework\\Setup\\InstallDataInterface;\r\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\r\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\r\n\r\nclass InstallData implements InstallDataInterface \r\n{\r\n    private $eavSetupFactory;\r\n\r\n    public function __construct(\r\n        EavSetupFactory $eavSetupFactory,\r\n        Config $eavConfig\r\n    ) \r\n    {\r\n        $this-&gt;eavSetupFactory = $eavSetupFactory;\r\n        $this-&gt;eavConfig = $eavConfig;\r\n    }\r\n\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) \r\n    {\r\n        $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n\r\n        \/\/ Text Field\r\n        $eavSetup-&gt;addAttribute(\\Magento\\Customer\\Model\\Customer::ENTITY, 'custom_text_field', [\r\n            'label' =&gt; 'Custom Text Field',\r\n            'system' =&gt; 0,\r\n            'position' =&gt; 700,\r\n            'sort_order' =&gt; 700,\r\n            'visible' =&gt; true,\r\n            'note' =&gt; '',\r\n            'type' =&gt; 'varchar',\r\n            'input' =&gt; 'text',\r\n            ]\r\n        );\r\n\r\n        $this-&gt;getEavConfig()-&gt;getAttribute('customer', 'custom_text_field')-&gt;setData('is_user_defined', 1)-&gt;setData('is_required', 0)-&gt;setData('default_value', '')-&gt;setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])-&gt;save();\r\n\r\n        \/\/ Dropdown Field\r\n        $eavSetup-&gt;addAttribute(\\Magento\\Customer\\Model\\Customer::ENTITY, 'custom_dropdown', [\r\n            'label' =&gt; 'Custom Dropdown',\r\n            'system' =&gt; 0,\r\n            'position' =&gt; 710,\r\n            'sort_order' =&gt; 710,\r\n            'visible' =&gt; true,\r\n            'note' =&gt; '',\r\n            'type' =&gt; 'int',\r\n            'input' =&gt; 'select',\r\n            'source' =&gt; 'Ht\\Mymodule\\Model\\Source\\Customdropdown',\r\n            ]\r\n        );\r\n\r\n        $this-&gt;getEavConfig()-&gt;getAttribute('customer', 'custom_dropdown')-&gt;setData('is_user_defined', 1)-&gt;setData('is_required', 0)-&gt;setData('default_value', '')-&gt;setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])-&gt;save();\r\n\r\n        \/\/ Yes\/No Field\r\n        $eavSetup-&gt;addAttribute(\\Magento\\Customer\\Model\\Customer::ENTITY, 'custom_yes_no', [\r\n            'label' =&gt; 'Custom Yes\/No',\r\n            'system' =&gt; 0,\r\n            'position' =&gt; 720,\r\n            'sort_order' =&gt; 720,\r\n            'visible' =&gt; true,\r\n            'note' =&gt; '',\r\n            'type' =&gt; 'int',\r\n            'input' =&gt; 'boolean',\r\n            'source' =&gt; 'Magento\\Eav\\Model\\Entity\\Attribute\\Source\\Boolean',\r\n            ]\r\n        );\r\n\r\n        $this-&gt;getEavConfig()-&gt;getAttribute('customer', 'custom_yes_no')-&gt;setData('is_user_defined', 1)-&gt;setData('is_required', 0)-&gt;setData('default_value', '')-&gt;setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])-&gt;save();\r\n    }\r\n    \r\n    public function getEavConfig() {\r\n        return $this-&gt;eavConfig;\r\n    }\r\n}<\/pre>\n<p>We have created the three custom customer attributes.<\/p>\n<p>Custom Text Field -&gt; Text Box<br \/>\nCustom Yes\/No -&gt; Yes\/No Field<br \/>\nCustom Dropdown -&gt; Select Box<\/p>\n<p>&#8220;Custom Dropdown&#8221; is a select box and we have defined a custom source: Ht\\Mymodule\\Model\\Source\\Customdropdown<\/p>\n<p>So, we need to create the source file as well.<\/p>\n<p>File path: app\/code\/Ht\/Mymodule\/Model\/Source\/Customdropdown.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Model\\Source;\r\n\r\n&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Model\\Source;\r\n\r\nclass Customdropdown extends \\Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource \r\n{\r\n    public function getAllOptions() \r\n    {\r\n        $type = [];\r\n        $type[] = [\r\n                'value' =&gt; '',\r\n                'label' =&gt; '--Select--'\r\n            ];\r\n        $type[] = [\r\n                'value' =&gt; 4,\r\n                'label' =&gt; 'Option 1'\r\n            ];\r\n        $type[] = [\r\n                'value' =&gt; 3,\r\n                'label' =&gt; 'Option 2'\r\n            ];\r\n        return $type;\r\n    }\r\n    \r\n    public function getOptionText($value) \r\n    {\r\n        foreach ($this-&gt;getAllOptions() as $option) {\r\n            if ($option['value'] == $value) {\r\n                return $option['label'];\r\n            }\r\n        }\r\n        return false;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>At Final, Open terminal\/SSH and navigate to Magento 2 setup root directory and run below commands.<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento setup:upgrade\r\nphp bin\/magento setup:static-content:deploy -f<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7258\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-customer-attributes.png\" alt=\"Magento 2 - How to Add Customer Attribute Programmatically \" width=\"1566\" height=\"314\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-customer-attributes.png 1566w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-customer-attributes-300x60.png 300w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-customer-attributes-768x154.png 768w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-customer-attributes-1024x205.png 1024w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-customer-attributes-800x160.png 800w\" sizes=\"(max-width: 1566px) 100vw, 1566px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;?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&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-customer-attribute-programmatically\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7297,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[9,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7257"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7257"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7257\/revisions"}],"predecessor-version":[{"id":7298,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7257\/revisions\/7298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7297"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}