---
title: "How to Add Custom Product Attribute Programmatically using Data Patch in Magento 2?"
url: "https://magecomp.com/blog/add-custom-product-attribute-programmatically-using-data-patch-magento-2/"
date: "2022-03-28T14:57:57+00:00"
modified: "2025-02-17T07:00:53+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 368
reading_time: "2 min read"
summary: "Today’s tutorial is about How to Add Custom Product Attribute Programmatically using Data Patch in Magento 2."
description: "Using a data patch we can add custom product attributes in Magento 2 by creating a PHP file. Learn more"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Fix Customer Name Prefix/Suffix Showing as Numbers in Address"
    url: "https://magecomp.com/blog/magento-2-fix-customer-name-prefix-suffix-showing-as-numbers-in-address/"
  - title: "Magento 2: Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid"
    url: "https://magecomp.com/blog/magento-2-add-admin-user-name-and-action-name-in-order-comment/"
  - title: "How to Copy One Quote to Another Quote Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/copy-one-quote-to-another-quote-programmatically-magento-2/"
---

# How to Add Custom Product Attribute Programmatically using Data Patch in Magento 2?

_Published: March 28, 2022_  
_Author: Dhiren Vasoya_  

![How to Add Custom Product Attribute Programmatically using Data Patch in Magento 2](https://magecomp.com/blog/wp-content/uploads/2022/03/How-to-Add-Custom-Product-Attribute-Programmatically-using-Data-Patch-in-Magento-2-1024x512.webp)

Today’s tutorial is about How to Add Custom Product Attribute Programmatically using Data Patch in Magento 2.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-5.webp)](https://magecomp.com/services/hyva-theme-development/)

While working with an eCommerce website, you need to add product attributes to allow customers to choose the desired option for the product like color, size, material, etc. In Magento 2, the admin can add any custom product attribute.
Here, we will learn to add product attributes programmatically with the help of a data patch in Magento 2. 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.
[![](https://magecomp.com/blog/wp-content/uploads/2022/05/Mobile-App.png)](https://magecomp.com/magento-2-mobile-app-builder)

## Steps to Add Custom Product Attribute Programmatically using Data Patch in Magento 2:
**Step 1:** Add **Addcustomdatapatch.php** in the following file path
**app\code\Vendor\Extention\Setup\Patch\Data**

```
<?php
namespace VendorExtentionSetupPatchData;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;

class Addcustomdatapatch implements DataPatchInterface
{
   private $_moduleDataSetup;

   private $_eavSetupFactory;

   public function __construct(
       ModuleDataSetupInterface $moduleDataSetup,
       EavSetupFactory $eavSetupFactory
   ) {
       $this->_moduleDataSetup = $moduleDataSetup;
       $this->_eavSetupFactory = $eavSetupFactory;
   }

   public function apply()
   {
       /** @var EavSetup $eavSetup */
       $eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_moduleDataSetup]);

       $eavSetup->addAttribute(MagentoCatalogModelProduct::ENTITY, 'custom_datapatch', [
           'type' => 'int',
           'backend' => '',
           'frontend' => '',
           'label' => 'Enable Custom Data Patch',
           'input' => 'select',
           'class' => 'custom_datapatch_class',
           'source' => MagentoCatalogModelProductAttributeSourceBoolean::class,
           'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
           'visible' => true,
           'required' => true,
           'user_defined' => false,
           'default' => '',
           'searchable' => false,
           'filterable' => false,
           'comparable' => false,
           'visible_on_front' => false,
           'used_in_product_listing' => true,
           'unique' => false,
       ]);
   }

   public static function getDependencies()
   {
       return [];
   }

   public function getAliases()
   {
       return [];
   }

   public static function getVersion()
   {
      return '1.0.0';
   }
}
```

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-3.webp)](https://magecomp.com/services/hire-magento-programmer/)

**Step 2:** Run the following command
```
php bin/magento setup:upgrade
```

## Conclusion:
This way you can effectively add any custom product attribute through the data patch in Magento 2. Alternatively, you can check [**How to Remove Product Attribute Programmatically in Magento 2**](https://magecomp.com/blog/remove-product-attribute-programmatically-magento-2/).
In case of any query, you can freely ask me through the comments. Share the solution with your developer friends and stay with us.
**Happy Coding!**

[![](https://magecomp.com/blog/wp-content/uploads/2021/10/Hire-Magento-2.png)](https://magecomp.com/hire-magento-developer.html)


---

_View the original post at: [https://magecomp.com/blog/add-custom-product-attribute-programmatically-using-data-patch-magento-2/](https://magecomp.com/blog/add-custom-product-attribute-programmatically-using-data-patch-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-24 21:39:55 UTC_  
