{"id":2243,"date":"2023-08-10T06:03:45","date_gmt":"2023-08-10T06:03:45","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/create-custom-product-attribute-using-data-patch-in-magento-2\/"},"modified":"2025-03-17T05:55:00","modified_gmt":"2025-03-17T05:55:00","slug":"create-custom-product-attribute-using-data-patch-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-custom-product-attribute-using-data-patch-in-magento-2\/","title":{"rendered":"How to Create Custom Product Attribute Using Data Patch in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this post, you\u2019ll learn the complete programmatic method to\u00a0create custom product attributes using data patch in Magento 2. \ud83d\ude00<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 custom product attributes help the merchants better organize the product information. It is also helpful in increasing the overall visibility of the products and delivering a rich experience. Often, merchants are required to do that!&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-product-attribute-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Custom product attributes in Magento 2<\/a>&nbsp;can be easily created from the admin panel, which is quite a no-brainer task.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But what if you want to do that programmatically using a data patch?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s find it out through this micro-post on \u2013 Magento 2 create product attribute using data patch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step-by-step Method to Create Custom Product Attributes Using Data Patch in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Magento 2,&nbsp;<a href=\"https:\/\/developer.adobe.com\/commerce\/php\/development\/components\/declarative-schema\/patches\/\" target=\"_blank\" rel=\"noreferrer noopener\">data patches<\/a>&nbsp;are the classes that contain schema and instructions for data manipulation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The patches are applied only once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They\u2019re also executed during the Magento upgrade.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let\u2019s see how to add custom product attribute programmatically using data patch in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>CreateCustomProductAttr.php<\/strong>&nbsp;file at&nbsp;<strong><em>{Vendor}\\{Extension}\\Setup\\Patch\\Data<\/em><\/strong>&nbsp;directory with the following code:<\/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=\"\">&lt;?php namespace Vendor\\Extension\\Setup\\Patch\\Data; use Magento\\Eav\\Setup\\EavSetup; use Magento\\Eav\\Setup\\EavSetupFactory; use Magento\\Framework\\Setup\\ModuleDataSetupInterface; use Magento\\Framework\\Setup\\Patch\\DataPatchInterface; class CreateCustomProductAttr implements DataPatchInterface { private $moduleDataSetup; private $eavSetupFactory; public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup;\n        $this->eavSetupFactory = $eavSetupFactory;\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function apply()\n    {\n        \/** @var EavSetup $eavSetup *\/\n        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);\n        $eavSetup->addAttribute('catalog_product', 'attribute_code', [\n            'type' => 'int',\n            'backend' => '',\n            'frontend' => '',\n            'label' => 'attribute label',\n            'input' => 'boolean',\n            'class' => '',\n            'source' => \\Magento\\Eav\\Model\\Entity\\Attribute\\Source\\Boolean::class,\n            'global' => \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_GLOBAL,\n            'visible' => true,\n            'required' => false,\n            'user_defined' => false,\n            'default' => '',\n            'searchable' => false,\n            'filterable' => false,\n            'comparable' => false,\n            'visible_on_front' => false,\n            'used_in_product_listing' => false,\n            'unique' => false,\n            'apply_to' => '',\n        ]);\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public static function getDependencies()\n    {\n        return [];\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function getAliases()\n    {\n        return [];\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, replace the Vendor and Extension with respective folder names.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, tweak the code as per the properties of the custom product attribute. You can change the attribute type, label, input type, and more per your requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once done, run the following Magento Upgrade command to apply the data patch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>php bin\/magento setup:upgrade<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s how you can add product attribute using data patch in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can find the newly created attribute in the product edit section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Learned something new today? Share it with your developer friends \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you find this post helpful, do not forget to rate this post with stars.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you for reading! \ud83c\udf40<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, you\u2019ll learn the complete programmatic method to\u00a0create custom product attributes using data patch in Magento 2. \ud83d\ude00 Magento 2 custom product attributes&#8230;<\/p>\n","protected":false},"author":13,"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-2243","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2243","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2243"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2243\/revisions"}],"predecessor-version":[{"id":5758,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2243\/revisions\/5758"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}