How To

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

Today’s tutorial is about How to Add Custom Product Attribute Programmatically using Data Patch in Magento 2.

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.

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 Vendor\Extention\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 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(\Magento\Catalog\Model\Product::ENTITY, 'custom_datapatch', [
           'type' => 'int',
           'backend' => '',
           'frontend' => '',
           'label' => 'Enable Custom Data Patch',
           'input' => 'select',
           'class' => 'custom_datapatch_class',
           'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
           'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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';
   }
}

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.

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!

Click to rate this post!
[Total: 9 Average: 4.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Getting following error
    call_user_func(): Argument #1 ($callback) must be a valid callback, class \Vendor\Extention\Setup\Patch\Data\Addcustomdatapatch not found

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

13 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

14 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

1 day ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

3 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

3 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

4 days ago