{"id":3218,"date":"2022-07-04T10:54:59","date_gmt":"2022-07-04T10:54:59","guid":{"rendered":"https:\/\/www.cynoinfotech.com\/?p=3218"},"modified":"2022-07-04T10:58:19","modified_gmt":"2022-07-04T10:58:19","slug":"how-to-create-custom-product-attribute-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/","title":{"rendered":"How to Create Custom Product Attribute Programmatically in Magento 2"},"content":{"rendered":"<p>In this blog, we are going to show you how to create a product attribute using the latest Declarative<\/p>\n<p>Schema &#8220;data patch &#8220;.<\/p>\n<p>A Data patch is a class that stores instructions for data modification. Using a data patch we can add custom product attributes in Magento 2 by creating a PHP file.<\/p>\n<p>Once you Create the file <strong>AddCustomProductAttribute.php <\/strong>in this directory .\u00a0 <strong>app\/code\/VenderName\/ModuleName\/<\/strong> <strong>Setup\/Patch\/Data\/. <\/strong>and paste the below code :<\/p>\n<p><strong>AddCustomProductAttribute.php<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nnamespace VenderName\\ModuleName\\Setup\\Patch\\Data;\r\n \r\nuse Magento\\Eav\\Setup\\EavSetup;\r\nuse Magento\\Eav\\Setup\\EavSetupFactory;\r\nuse Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\r\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\r\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\r\n\r\nclass AddCustomProductAttribute implements DataPatchInterface\r\n{\r\n    protected $_eavSetupFactory;\r\n\t\r\n\tprotected $_moduleDataSetup;\r\n    \r\n    \/**\r\n     * @param ModuleDataSetupInterface $_moduleDataSetup\r\n     *\/\r\n  \r\n    public function __construct(\r\n\t\tEavSetupFactory $_eavSetupFactory, \r\n\t\tModuleDataSetupInterface $_moduleDataSetup\r\n\t){\r\n        $this-&gt;eavSetupFactory = $_eavSetupFactory;\r\n\t\t$this-&gt;_moduleDataSetup = $_moduleDataSetup;\r\n    }\r\n  \r\n    public function apply()\r\n    {\r\n        $eavSetup = $this-&gt;eavSetupFactory-&gt;create(array('setup' =&gt; $this-&gt;_moduleDataSetup));\r\n        \r\n        $eavSetup-&gt;addAttribute(\r\n            \\Magento\\Catalog\\Model\\Product::ENTITY,\r\n            'indexfollow_enable',\r\n            array(\r\n                'group' =&gt; 'Index Follow',\r\n                'type' =&gt; 'text',\r\n                'backend' =&gt; '',   \r\n                'frontend' =&gt; '',\r\n                'label' =&gt; 'Enable Index Follow',\r\n                'input' =&gt; 'boolean',\r\n                'class' =&gt; 'indexfollow_enable',\r\n                'source' =&gt; \\Magento\\Eav\\Model\\Entity\\Attribute\\Source\\Boolean::class,\r\n                'global' =&gt; \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_GLOBAL,\r\n                'visible' =&gt; true,\r\n                'required' =&gt; false,\r\n                'user_defined' =&gt; true,\r\n                'default' =&gt; '',\r\n                'searchable' =&gt; false,\r\n                'filterable' =&gt; false,\r\n                'comparable' =&gt; false,\r\n                'visible_on_front' =&gt; false,\r\n                'used_in_product_listing' =&gt; true,\r\n                'unique' =&gt; false,                \r\n            )    \r\n        );\r\n    }\r\n\t\r\n\t\/**\r\n\t* {@inheritdoc}\r\n\t*\/\r\n\tpublic static function getDependencies()\r\n\t{\r\n\t\treturn array();\r\n\t}\r\n\r\n\t\/**\r\n\t* {@inheritdoc}\r\n\t*\/\r\n\tpublic function getAliases()\r\n\t{\r\n\t\treturn array();\r\n\t}\r\n\r\n\t\/**\r\n\t* {@inheritdoc}\r\n\t*\/\r\n\tpublic static function getVersion()\r\n\t{\r\n\t\treturn \u20181.0.0'}\r\n}\r\n<\/pre>\n<p><strong>after will running the below command :<\/strong><\/p>\n<ul>\n<li>php -dmemory_limit=2G bin\/magento setup:upgrade<\/li>\n<li>php -dmemory_limit=2G bin\/magento setup:static-content:deploy -f -j<\/li>\n<li>php -dmemory_limit=2G bin\/magento cache:flush<\/li>\n<\/ul>\n<p>Now, you can see in the <strong>eav_attribute<\/strong> table that your custom product attribute was created successfully using the data patch.<\/p>\n<p>If you have any questions let me know in the comment section.<\/p>\n<p>Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we are going to show you how to create a product attribute using the latest Declarative Schema &#8220;data patch &#8220;. A Data patch is a class that stores instructions for data modification. Using a data patch we can add custom product attributes in Magento 2 by creating a PHP file. Once you [&hellip;]<\/p>\n","protected":false},"author":670,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3218","post","type-post","status-publish","format-standard","hentry","category-magento-blog"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create Custom Product Attribute Programmatically in Magento 2 - cynoinfotech<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/\"},\"author\":{\"name\":\"Arjun Makadiya\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/person\\\/f1be8186602d3ebf417cf58baf125f24\"},\"headline\":\"How to Create Custom Product Attribute Programmatically in Magento 2\",\"datePublished\":\"2022-07-04T10:54:59+00:00\",\"dateModified\":\"2022-07-04T10:58:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/\"},\"wordCount\":152,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\"},\"articleSection\":[\"Magento blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/\",\"name\":\"How to Create Custom Product Attribute Programmatically in Magento 2 - cynoinfotech\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#website\"},\"datePublished\":\"2022-07-04T10:54:59+00:00\",\"dateModified\":\"2022-07-04T10:58:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-create-custom-product-attribute-programmatically-in-magento-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Custom Product Attribute Programmatically in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\",\"name\":\"cynoinfotech\",\"description\":\"web design &amp; development\",\"publisher\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\",\"name\":\"Cynoinfotech\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.cynoinfotech.com\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/final-logo-ci.png\",\"contentUrl\":\"https:\\\/\\\/www.cynoinfotech.com\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/final-logo-ci.png\",\"width\":240,\"height\":70,\"caption\":\"Cynoinfotech\"},\"image\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/cynoinfotech\\\/\",\"https:\\\/\\\/x.com\\\/cynoinfotech\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/person\\\/f1be8186602d3ebf417cf58baf125f24\",\"name\":\"Arjun Makadiya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"caption\":\"Arjun Makadiya\"},\"description\":\"Arjun Makadiya is a Junior Magento Developer at Cynoifotech. He is passionate about technology. Apart from work he likes to play cricket.\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/author\\\/arjun-makadiya\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Custom Product Attribute Programmatically in Magento 2 - cynoinfotech","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:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/"},"author":{"name":"Arjun Makadiya","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/person\/f1be8186602d3ebf417cf58baf125f24"},"headline":"How to Create Custom Product Attribute Programmatically in Magento 2","datePublished":"2022-07-04T10:54:59+00:00","dateModified":"2022-07-04T10:58:19+00:00","mainEntityOfPage":{"@id":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/"},"wordCount":152,"commentCount":0,"publisher":{"@id":"https:\/\/cynoinfotech.com\/blog\/#organization"},"articleSection":["Magento blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/","url":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/","name":"How to Create Custom Product Attribute Programmatically in Magento 2 - cynoinfotech","isPartOf":{"@id":"https:\/\/cynoinfotech.com\/blog\/#website"},"datePublished":"2022-07-04T10:54:59+00:00","dateModified":"2022-07-04T10:58:19+00:00","breadcrumb":{"@id":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cynoinfotech.com\/blog\/how-to-create-custom-product-attribute-programmatically-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cynoinfotech.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Custom Product Attribute Programmatically in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/cynoinfotech.com\/blog\/#website","url":"https:\/\/cynoinfotech.com\/blog\/","name":"cynoinfotech","description":"web design &amp; development","publisher":{"@id":"https:\/\/cynoinfotech.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cynoinfotech.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cynoinfotech.com\/blog\/#organization","name":"Cynoinfotech","url":"https:\/\/cynoinfotech.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.cynoinfotech.com\/wp-content\/uploads\/2017\/11\/final-logo-ci.png","contentUrl":"https:\/\/www.cynoinfotech.com\/wp-content\/uploads\/2017\/11\/final-logo-ci.png","width":240,"height":70,"caption":"Cynoinfotech"},"image":{"@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cynoinfotech\/","https:\/\/x.com\/cynoinfotech"]},{"@type":"Person","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/person\/f1be8186602d3ebf417cf58baf125f24","name":"Arjun Makadiya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","caption":"Arjun Makadiya"},"description":"Arjun Makadiya is a Junior Magento Developer at Cynoifotech. He is passionate about technology. Apart from work he likes to play cricket.","url":"https:\/\/cynoinfotech.com\/blog\/author\/arjun-makadiya\/"}]}},"_links":{"self":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/users\/670"}],"replies":[{"embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=3218"}],"version-history":[{"count":3,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3218\/revisions"}],"predecessor-version":[{"id":3221,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3218\/revisions\/3221"}],"wp:attachment":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=3218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=3218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=3218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}