Site icon MageComp Blog

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

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!

Exit mobile version