{"id":2288,"date":"2023-10-02T02:30:28","date_gmt":"2023-10-02T02:30:28","guid":{"rendered":"https:\/\/easycodingschool.com\/?p=2288"},"modified":"2023-10-02T02:30:28","modified_gmt":"2023-10-02T02:30:28","slug":"create-customer-attribute-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/","title":{"rendered":"Create customer attribute programmatically in Magento 2"},"content":{"rendered":"<p>To programmatically build a custom customer attribute in Magento 2, you must first develop a custom module. Here are the steps to take to accomplish this:<\/p>\n<h3>Make Your Own Module:<\/h3>\n<p>If you haven&#8217;t previously, you should develop a custom module. It can be created in the <strong>app\/code<\/strong> directory. Let&#8217;s call it CustomModule in this case.<\/p>\n<h3>Make a setup script as follows:<\/h3>\n<p>Create a file named InstallData.php at the following path: <strong>app\/code\/CustomModule\/Setup\/InstallData.php<\/strong> within your custom module directory (<strong>app\/code\/CustomModule<\/strong>). This script will be used to create your unique customer attribute.<\/p>\n<p>Here&#8217;s an example of how InstallData.php could look:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>de\/CustomModule). This script will be used to create your unique customer attribute.\r\n\r\nHere's an example of how InstallData.php could look:\r\n\r\n&lt;?php namespace CustomModule\\Setup; use Magento\\Customer\\Setup\\CustomerSetupFactory; use Magento\\Framework\\Setup\\InstallDataInterface; use Magento\\Framework\\Setup\\ModuleContextInterface; use Magento\\Framework\\Setup\\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { private $customerSetupFactory; public function __construct(CustomerSetupFactory $customerSetupFactory) { $this-&gt;customerSetupFactory = $customerSetupFactory;\r\n    }\r\n\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\r\n    {\r\n        $customerSetup = $this-&gt;customerSetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n\r\n        $customerSetup-&gt;addAttribute(\r\n            \\Magento\\Customer\\Model\\Customer::ENTITY,\r\n            'custom_attribute',\r\n            [\r\n                'type' =&gt; 'varchar',\r\n                'label' =&gt; 'Custom Attribute',\r\n                'input' =&gt; 'text',\r\n                'source' =&gt; '',\r\n                'required' =&gt; false,\r\n                'visible' =&gt; true,\r\n                'position' =&gt; 999,\r\n                'system' =&gt; 0,\r\n                'backend' =&gt; ''\r\n            ]\r\n        );\r\n\r\n        $attribute = $customerSetup-&gt;getEavConfig()-&gt;getAttribute(\r\n            \\Magento\\Customer\\Model\\Customer::ENTITY,\r\n            'custom_attribute'\r\n        );\r\n\r\n        $attribute-&gt;setData(\r\n            'used_in_forms',\r\n            ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']\r\n        );\r\n\r\n        $attribute-&gt;save();\r\n    }\r\n}<\/code><\/pre>\n<\/div>\n<p>In this script, we build a custom customer attribute named <strong>&#8216;custom_attribute&#8217;<\/strong>. You can change the attribute properties (type, label, input type, and so on) to suit your needs.<\/p>\n<h3>Execute the following Setup Upgrade Command:<\/h3>\n<p>To apply the changes to your Magento 2 database, use the following command after creating the script:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>bin\/magento setup:upgrade<\/code><\/pre>\n<\/div>\n<h3>Clear Cache:<\/h3>\n<p>Clear the cache to ensure that the changes take effect:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>bin\/magento cache:clean<\/code><\/pre>\n<\/div>\n<p>You have now established a custom customer attribute in Magento 2 programmatically. This attribute can be used for client registration as well as customer edit forms in the admin panel. You can further alter the property and use it to meet your specific company needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.<\/p>\n","protected":false},"author":1,"featured_media":2290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[208,205],"tags":[214,213,211],"class_list":["post-2288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2","category-news","tag-attribute","tag-custom-custom-attribute","tag-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create customer attribute programmatically in Magento 2 - Easy Coding School<\/title>\n<meta name=\"description\" content=\"Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create customer attribute programmatically in Magento 2 - Easy Coding School\" \/>\n<meta property=\"og:description\" content=\"Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Easy Coding School\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-02T02:30:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"660\" \/>\n\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"gauravkumarjha19@gmail.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"gauravkumarjha19@gmail.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\"},\"author\":{\"name\":\"gauravkumarjha19@gmail.com\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/#\/schema\/person\/9cabe8e90728da7f26676cbd1744558b\"},\"headline\":\"Create customer attribute programmatically in Magento 2\",\"datePublished\":\"2023-10-02T02:30:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\"},\"wordCount\":221,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg\",\"keywords\":[\"Attribute\",\"Custom custom attribute\",\"Magento 2\"],\"articleSection\":[\"Magento 2\",\"News\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\",\"url\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\",\"name\":\"Create customer attribute programmatically in Magento 2 - Easy Coding School\",\"isPartOf\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg\",\"datePublished\":\"2023-10-02T02:30:28+00:00\",\"description\":\"Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.\",\"breadcrumb\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage\",\"url\":\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg\",\"contentUrl\":\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg\",\"width\":660,\"height\":350},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/easycodingschool.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create customer attribute programmatically in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/#website\",\"url\":\"https:\/\/easycodingschool.com\/blog\/\",\"name\":\"Easy Coding School\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/easycodingschool.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/#organization\",\"name\":\"Easy Coding School\",\"url\":\"https:\/\/easycodingschool.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/07\/New-Project-10.png\",\"contentUrl\":\"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/07\/New-Project-10.png\",\"width\":310,\"height\":114,\"caption\":\"Easy Coding School \"},\"image\":{\"@id\":\"https:\/\/easycodingschool.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/#\/schema\/person\/9cabe8e90728da7f26676cbd1744558b\",\"name\":\"gauravkumarjha19@gmail.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/easycodingschool.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6846a3cc9bd6cb5b779aef260fe379c4b93fa20e1a4c753eb023ff7c08d7c668?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6846a3cc9bd6cb5b779aef260fe379c4b93fa20e1a4c753eb023ff7c08d7c668?s=96&d=mm&r=g\",\"caption\":\"gauravkumarjha19@gmail.com\"},\"sameAs\":[\"https:\/\/easycodingschool.com\/blog\"],\"url\":\"https:\/\/easycodingschool.com\/blog\/author\/gauravkumarjha19gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create customer attribute programmatically in Magento 2 - Easy Coding School","description":"Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"Create customer attribute programmatically in Magento 2 - Easy Coding School","og_description":"Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.","og_url":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/","og_site_name":"Easy Coding School","article_published_time":"2023-10-02T02:30:28+00:00","og_image":[{"width":660,"height":350,"url":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg","type":"image\/jpeg"}],"author":"gauravkumarjha19@gmail.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"gauravkumarjha19@gmail.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/"},"author":{"name":"gauravkumarjha19@gmail.com","@id":"https:\/\/easycodingschool.com\/blog\/#\/schema\/person\/9cabe8e90728da7f26676cbd1744558b"},"headline":"Create customer attribute programmatically in Magento 2","datePublished":"2023-10-02T02:30:28+00:00","mainEntityOfPage":{"@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/"},"wordCount":221,"commentCount":0,"publisher":{"@id":"https:\/\/easycodingschool.com\/blog\/#organization"},"image":{"@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg","keywords":["Attribute","Custom custom attribute","Magento 2"],"articleSection":["Magento 2","News"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/","url":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/","name":"Create customer attribute programmatically in Magento 2 - Easy Coding School","isPartOf":{"@id":"https:\/\/easycodingschool.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg","datePublished":"2023-10-02T02:30:28+00:00","description":"Learn how to add custom customer properties to Magento 2. Customized client data fields can help you improve your e-commerce store.","breadcrumb":{"@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#primaryimage","url":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg","contentUrl":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/10\/Create-customer-attribute-programmatically-in-Magento-2.jpg","width":660,"height":350},{"@type":"BreadcrumbList","@id":"https:\/\/easycodingschool.com\/blog\/create-customer-attribute-programmatically-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/easycodingschool.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create customer attribute programmatically in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/easycodingschool.com\/blog\/#website","url":"https:\/\/easycodingschool.com\/blog\/","name":"Easy Coding School","description":"","publisher":{"@id":"https:\/\/easycodingschool.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/easycodingschool.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/easycodingschool.com\/blog\/#organization","name":"Easy Coding School","url":"https:\/\/easycodingschool.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/easycodingschool.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/07\/New-Project-10.png","contentUrl":"https:\/\/easycodingschool.com\/blog\/wp-content\/uploads\/2023\/07\/New-Project-10.png","width":310,"height":114,"caption":"Easy Coding School "},"image":{"@id":"https:\/\/easycodingschool.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/easycodingschool.com\/blog\/#\/schema\/person\/9cabe8e90728da7f26676cbd1744558b","name":"gauravkumarjha19@gmail.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/easycodingschool.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6846a3cc9bd6cb5b779aef260fe379c4b93fa20e1a4c753eb023ff7c08d7c668?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6846a3cc9bd6cb5b779aef260fe379c4b93fa20e1a4c753eb023ff7c08d7c668?s=96&d=mm&r=g","caption":"gauravkumarjha19@gmail.com"},"sameAs":["https:\/\/easycodingschool.com\/blog"],"url":"https:\/\/easycodingschool.com\/blog\/author\/gauravkumarjha19gmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/posts\/2288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2288"}],"version-history":[{"count":2,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions"}],"predecessor-version":[{"id":2291,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions\/2291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/media\/2290"}],"wp:attachment":[{"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easycodingschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}