{"id":982,"date":"2020-05-04T07:15:46","date_gmt":"2020-05-04T07:15:46","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/05\/04\/create-custom-customer-attribute-in-magento-2\/"},"modified":"2025-06-16T14:38:55","modified_gmt":"2025-06-16T09:08:55","slug":"create-custom-customer-attribute-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-custom-customer-attribute-in-magento-2\/","title":{"rendered":"How to Create Custom Customer Attribute in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">An attribute is a property of an entity that can be used in various Magento 2 store\u2019s function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here we\u2019ll talk about customer attribute. Create custom customer attribute in Magento 2 with the below solution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento is a user-friendly platform and it allows creating custom attributes. Such customer custom attributes help in understanding the customer in a better way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some examples of <a href=\"https:\/\/meetanshi.com\/blog\/add-custom-customer-attribute-registration-form-magento-2\/\" data-type=\"link\" data-id=\"https:\/\/meetanshi.com\/blog\/add-custom-customer-attribute-registration-form-magento-2\/\">custom customer attributes<\/a> are customers\u2019 mobile number, interests\/hobbies, etc. which can be programmatically created using this method:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Custom Customer Attribute in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Setup;\n\nuse Magento\\Customer\\Api\\CustomerMetadataInterface;\nuse Magento\\Eav\\Setup\\EavSetup;\nuse Magento\\Eav\\Model\\Config as EavConfig;\nuse Magento\\Eav\\Setup\\EavSetupFactory;\nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute;\nuse Magento\\Catalog\\Model\\Product;\n\nclass InstallData implements InstallDataInterface\n{\n    private $eavSetupFactory;\n    \/**\n     * @var EavConfig\n     *\/\n    private $eavConfig;\n\n    public function __construct(EavSetupFactory $eavSetupFactory, EavConfig $eavConfig)\n    {\n        $this->eavSetupFactory = $eavSetupFactory;\n        $this->eavConfig = $eavConfig;\n    }\n\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $eavsetup = $this->eavSetupFactory->create(['setup' => $setup]);\n        $attributecode = 'interests';\n\n        $eavsetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributecode, [\n            'label' => 'Interests',\n            'required' => 0,\n            'user_defined' => 1,\n            'note' => 'Separate Multiple Intrests with comms',\n            'system' => 0,\n            'position' => 100,\n        ]);\n\n        $eavsetup->addAttributeToSet(\n            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,\n            CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,\n            null,\n            $attributecode\n        );\n\n        $attribute = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributecode);\n        $attribute->setData('used_in_forms', [\n            'adminhtml_customer',\n            'customer_account_create',\n            'customer_account_edit'\n        ]);\n\n        $attribute->setData('validate_rules', [\n            'input_validation' => 1,\n            'min_text_length' => 3,\n            'max_text_length' => 30,\n\n        ]);\n        $attribute->getResource()->save($attribute);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, run the below commands:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento setup:upgrade\nphp bin\/magento setup:static-content:deploy -f\nphp bin\/magento cache:flush\nphp bin\/magento cache:clean<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution among fellow Magento developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An attribute is a property of an entity that can be used in various Magento 2 store\u2019s function. Here we\u2019ll talk about customer attribute. Create&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=982"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":16899,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/982\/revisions\/16899"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}