{"id":892,"date":"2023-12-05T18:44:29","date_gmt":"2023-12-05T13:14:29","guid":{"rendered":"https:\/\/magecurious.com\/blog\/?p=892"},"modified":"2024-11-18T00:37:13","modified_gmt":"2024-11-17T19:07:13","slug":"how-to-create-customer-attribute-using-data-patch-in-magento-2","status":"publish","type":"post","link":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/","title":{"rendered":"How To Create Customer Attribute using Data Patch in Magento 2"},"content":{"rendered":"\n<p>Hello Everyone,<\/p>\n\n\n\n<p>In this blog, we will learn about how to Create Custom Customer Attributes using Data Patch in Magento 2.<\/p>\n\n\n\n<p>Data Patch feature is introduced from Magento 2.3.x.<\/p>\n\n\n\n<p>Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Customer Attribute in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>STEPS FOR CREATE CUSTOM CUSTOMER ATTRIBUTE IN MAGENTO 2<\/strong><\/h2>\n\n\n\n<p><strong>Step 1: Create CustomerAttribute.php file<\/strong><\/p>\n\n\n\n<p><strong>app\/code\/Vendor\/Extension\/Setup\/Patch\/Data\/CustomerAttribute.php<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\nnamespace Vendor\\Extension\\Setup\\Patch\\Data;\n\nuse Magento\\Customer\\Model\\Customer;\nuse Magento\\Customer\\Setup\\CustomerSetup;\nuse Magento\\Customer\\Setup\\CustomerSetupFactory;\nuse Magento\\Eav\\Model\\Entity\\Attribute\\Set;\nuse Magento\\Eav\\Model\\Entity\\Attribute\\SetFactory;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\nuse Magento\\Framework\\Setup\\Patch\\PatchRevertableInterface;\n\nclass CustomerAttribute implements DataPatchInterface, PatchRevertableInterface\n{\n\n    \/**\n     * @var ModuleDataSetupInterface\n     *\/\n    private $moduleDataSetup;\n    \/**\n     * @var CustomerSetup\n     *\/\n    private $customerSetupFactory;\n    \/**\n     * @var SetFactory\n     *\/\n    private $attributeSetFactory;\n\n    \/**\n     * Constructor\n     *\n     * @param ModuleDataSetupInterface $moduleDataSetup\n     * @param CustomerSetupFactory $customerSetupFactory\n     * @param SetFactory $attributeSetFactory\n     *\/\n    public function __construct(\n        ModuleDataSetupInterface $moduleDataSetup,\n        CustomerSetupFactory     $customerSetupFactory,\n        SetFactory               $attributeSetFactory\n    )\n    {\n        $this-&gt;moduleDataSetup = $moduleDataSetup;\n        $this-&gt;customerSetupFactory = $customerSetupFactory;\n        $this-&gt;attributeSetFactory = $attributeSetFactory;\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function apply()\n    {\n        $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;startSetup();\n        \/** @var CustomerSetup $customerSetup *\/\n        $customerSetup = $this-&gt;customerSetupFactory-&gt;create(&#x5B;&#039;setup&#039; =&gt; $this-&gt;moduleDataSetup]);\n        $customerEntity = $customerSetup-&gt;getEavConfig()-&gt;getEntityType(Customer::ENTITY);\n        $attributeSetId = $customerEntity-&gt;getDefaultAttributeSetId();\n\n        \/** @var $attributeSet Set *\/\n        $attributeSet = $this-&gt;attributeSetFactory-&gt;create();\n        $attributeGroupId = $attributeSet-&gt;getDefaultGroupId($attributeSetId);\n\n        $customerSetup-&gt;addAttribute(\n            Customer::ENTITY,\n            &#039;mobile_number&#039;,\n            &#x5B;\n\t     &#039;input&#039; =&gt; &#039;text&#039;,\n                &#039;type&#039; =&gt; &#039;varchar&#039;,\n                &#039;label&#039; =&gt; &#039;Mobile Number&#039;, \n                &#039;source&#039; =&gt; &#039;&#039;,\n                &#039;required&#039; =&gt; false,\n                &#039;position&#039; =&gt; 40,\n                &#039;visible&#039; =&gt; true,\n                &#039;system&#039; =&gt; false,\n                &#039;is_used_in_grid&#039; =&gt; true,\n                &#039;is_visible_in_grid&#039; =&gt; true,\n                &#039;is_filterable_in_grid&#039; =&gt; true,\n                &#039;is_searchable_in_grid&#039; =&gt; false,\n                &#039;backend&#039; =&gt; &#039;&#039;\n            ]\n        );\n\n        $attribute = $customerSetup-&gt;getEavConfig()-&gt;getAttribute(Customer::ENTITY, &#039;mobile_number&#039;);\n        $attribute-&gt;addData(&#x5B;\n            &#039;used_in_forms&#039; =&gt; &#x5B;\n                &#039;adminhtml_customer&#039;,\n                &#039;customer_account_create&#039;,\n                &#039;customer_account_edit&#039;\n            ]\n        ]);\n        $attribute-&gt;addData(&#x5B;\n            &#039;attribute_set_id&#039; =&gt; $attributeSetId,\n            &#039;attribute_group_id&#039; =&gt; $attributeGroupId\n\n        ]);\n        $attribute-&gt;save();\n\n        $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;endSetup();\n    }\n\n    public function revert()\n    {\n        $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;startSetup();\n        \/** @var CustomerSetup $customerSetup *\/\n        $customerSetup = $this-&gt;customerSetupFactory-&gt;create(&#x5B;&#039;setup&#039; =&gt; $this-&gt;moduleDataSetup]);\n        $customerSetup-&gt;removeAttribute(\\Magento\\Customer\\Model\\Customer::ENTITY, &#039;mobile_number&#039;);\n\n        $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;endSetup();\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function getAliases()\n    {\n        return &#x5B;];\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public static function getDependencies()\n    {\n        return &#x5B;];\n    }\n}\n<\/pre><\/div>\n\n\n<p>In above code we created text type attribute you can change as per your requirements.<\/p>\n\n\n\n<p><strong>Step 2: Finally run the below commands<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$ php bin\/magento setup:upgrade\n$ php bin\/magento cache:clean\n$ php bin\/magento cache:flush\n<\/pre><\/div>\n\n\n<p><strong>Step 3: Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/_r55vMI7bIGd7Hip7RDZK5Pozr7GxAvVNHfTYOdwsqBCfVz0FoAhl593pYgtpVNODsZUXKJW_Dt7PiJY5pDfxZL1a3ChulktRn0Wz1BbnH4Rk61Y0dNkFys0lwOrYqGez1Si1HXYj0bAhA0gb_AlzFnFcWDXlwFOUgwCD_QwPQuJH8bIlIA77mBw-NnaPA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Above created custom customer attribute is stored in&nbsp; eav_attribute table.<\/p>\n\n\n\n<p>Now go to Admin -&gt; Customers -&gt; All Customers -&gt; Edit -&gt; Account Information.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Final Thoughts:<\/strong><\/h2>\n\n\n\n<p>So this was the easiest way which we have told you in this blog. This is how you can Create Custom Customer Attribute in Magento 2. Hope you liked the blog.<\/p>\n\n\n\n<p>So quickly go to the comment box and tell me how you like this blog?<\/p>\n\n\n\n<p><strong>Stay tuned with us on our site to get new updates of Magento.<\/strong><\/p>\n\n\n\n<p>Thanks for reading and visiting our site.<\/p>\n\n\n<!-- FeedbackWP Plugin --><div  class=\"rmp-widgets-container rmp-wp-plugin rmp-main-container js-rmp-widgets-container js-rmp-widgets-container--892 \"  data-post-id=\"892\">    <!-- Rating widget -->  <div class=\"rmp-rating-widget js-rmp-rating-widget\">          <p class=\"rmp-heading rmp-heading--title\">        How useful was this post?      <\/p>              <p class=\"rmp-heading rmp-heading--subtitle\">        Click on a star to rate it!      <\/p>        <div class=\"rmp-rating-widget__icons\">      <ul class=\"rmp-rating-widget__icons-list js-rmp-rating-icons-list\">                  <li class=\"rmp-rating-widget__icons-list__icon js-rmp-rating-item\" data-descriptive-rating=\"Not at all useful\" data-value=\"1\">              <i class=\"js-rmp-rating-icon rmp-icon rmp-icon--ratings rmp-icon--star rmp-icon--full-highlight\"><\/i>          <\/li>                  <li class=\"rmp-rating-widget__icons-list__icon js-rmp-rating-item\" data-descriptive-rating=\"Somewhat useful\" data-value=\"2\">              <i class=\"js-rmp-rating-icon rmp-icon rmp-icon--ratings rmp-icon--star rmp-icon--full-highlight\"><\/i>          <\/li>                  <li class=\"rmp-rating-widget__icons-list__icon js-rmp-rating-item\" data-descriptive-rating=\"Useful\" data-value=\"3\">              <i class=\"js-rmp-rating-icon rmp-icon rmp-icon--ratings rmp-icon--star rmp-icon--full-highlight\"><\/i>          <\/li>                  <li class=\"rmp-rating-widget__icons-list__icon js-rmp-rating-item\" data-descriptive-rating=\"Fairly useful\" data-value=\"4\">              <i class=\"js-rmp-rating-icon rmp-icon rmp-icon--ratings rmp-icon--star rmp-icon--full-highlight\"><\/i>          <\/li>                  <li class=\"rmp-rating-widget__icons-list__icon js-rmp-rating-item\" data-descriptive-rating=\"Very useful\" data-value=\"5\">              <i class=\"js-rmp-rating-icon rmp-icon rmp-icon--ratings rmp-icon--star rmp-icon--full-highlight\"><\/i>          <\/li>              <\/ul>    <\/div>    <p class=\"rmp-rating-widget__hover-text js-rmp-hover-text\"><\/p>        <button class=\"rmp-rating-widget__submit-btn rmp-btn js-submit-rating-btn\">      Submit Rating    <\/button>    <p class=\"rmp-rating-widget__results js-rmp-results \">      Average rating <span class=\"rmp-rating-widget__results__rating js-rmp-avg-rating\">5<\/span> \/ 5. Vote count: <span class=\"rmp-rating-widget__results__votes js-rmp-vote-count\">1<\/span>    <\/p>    <p class=\"rmp-rating-widget__not-rated js-rmp-not-rated rmp-rating-widget__not-rated--hidden\">      No votes so far! Be the first to rate this post.    <\/p>    <p class=\"rmp-rating-widget__msg js-rmp-msg\"><\/p>  <\/div>  <!--Structured data -->        <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello Everyone, In this blog, we will learn about how to Create Custom Customer Attributes using Data Patch in Magento 2. Data Patch feature is introduced from Magento 2.3.x. Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Customer Attribute in Magento 2. STEPS FOR CREATE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":894,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-892","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create Customer Attribute using Data Patch in Magento 2<\/title>\n<meta name=\"description\" content=\"Learn to create customer attribute in Magento 2 using a data patch for adding attributes, creating the required PHP files.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-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 using Data Patch in Magento 2\" \/>\n<meta property=\"og:description\" content=\"Learn to create customer attribute in Magento 2 using a data patch for adding attributes, creating the required PHP files.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Magecurious\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-05T13:14:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-17T19:07:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1260\" \/>\n\t<meta property=\"og:image:height\" content=\"496\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Magecurious\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Magecurious\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Customer Attribute using Data Patch in Magento 2","description":"Learn to create customer attribute in Magento 2 using a data patch for adding attributes, creating the required PHP files.","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:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"Create Customer Attribute using Data Patch in Magento 2","og_description":"Learn to create customer attribute in Magento 2 using a data patch for adding attributes, creating the required PHP files.","og_url":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/","og_site_name":"Magecurious","article_published_time":"2023-12-05T13:14:29+00:00","article_modified_time":"2024-11-17T19:07:13+00:00","og_image":[{"width":1260,"height":496,"url":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png","type":"image\/png"}],"author":"Magecurious","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Magecurious","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/"},"author":{"name":"Magecurious","@id":"https:\/\/magecurious.com\/blog\/#\/schema\/person\/6bced7da0991e7fbbd4550006a6494e2"},"headline":"How To Create Customer Attribute using Data Patch in Magento 2","datePublished":"2023-12-05T13:14:29+00:00","dateModified":"2024-11-17T19:07:13+00:00","mainEntityOfPage":{"@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/"},"wordCount":204,"commentCount":0,"publisher":{"@id":"https:\/\/magecurious.com\/blog\/#organization"},"image":{"@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png","articleSection":["Magento 2 Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/","url":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/","name":"Create Customer Attribute using Data Patch in Magento 2","isPartOf":{"@id":"https:\/\/magecurious.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png","datePublished":"2023-12-05T13:14:29+00:00","dateModified":"2024-11-17T19:07:13+00:00","description":"Learn to create customer attribute in Magento 2 using a data patch for adding attributes, creating the required PHP files.","breadcrumb":{"@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#primaryimage","url":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png","contentUrl":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png","width":1260,"height":496},{"@type":"BreadcrumbList","@id":"https:\/\/magecurious.com\/blog\/how-to-create-customer-attribute-using-data-patch-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/magecurious.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Create Customer Attribute using Data Patch in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/magecurious.com\/blog\/#website","url":"https:\/\/magecurious.com\/blog\/","name":"Magecurious","description":"Blog","publisher":{"@id":"https:\/\/magecurious.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/magecurious.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/magecurious.com\/blog\/#organization","name":"Magecurious","url":"https:\/\/magecurious.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/magecurious.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2026\/03\/cropped-New_Project.png","contentUrl":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2026\/03\/cropped-New_Project.png","width":596,"height":113,"caption":"Magecurious"},"image":{"@id":"https:\/\/magecurious.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/magecurious.com\/blog\/#\/schema\/person\/6bced7da0991e7fbbd4550006a6494e2","name":"Magecurious","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/magecurious.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c9178df2190b2309da97c9f28dd475bc338275cf664d12678d7e5da51c761271?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c9178df2190b2309da97c9f28dd475bc338275cf664d12678d7e5da51c761271?s=96&d=mm&r=g","caption":"Magecurious"},"sameAs":["https:\/\/magecurious.com"],"url":"https:\/\/magecurious.com\/blog\/author\/magecurious-wp\/"}]}},"jetpack_featured_media_url":"https:\/\/magecurious.com\/blog\/wp-content\/uploads\/2023\/12\/image_2023_12_05T12_06_31_059Z.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/posts\/892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/comments?post=892"}],"version-history":[{"count":0,"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/posts\/892\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/media\/894"}],"wp:attachment":[{"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/media?parent=892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/categories?post=892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magecurious.com\/blog\/wp-json\/wp\/v2\/tags?post=892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}