How to Create Product Attribute in Magento 2 Programmatically Using Data Patch

Previously, we have seen how to create category attributes using a data patch in Magento 2. And today we are back with a similar topic how to create product attributes in Magento 2 programmatically using a data patch.

The eCommerce web store has so many products and it contains all the product details such as size, color, description, quantity, price, etc. But you have to add Magento product attribute to your store to display this product information.

However, Magento 2 allows the admin to create a Magento custom product from the backend. Earlier, Magento 2 uses InstallData and UpgradeData files to add data to the custom tables or default tables. But after the update of Magento 2.3, these files are replaced by a data patch. So let's see how to create product attributes in Magento 2 programmatically using a data patch.

How to Create Product Attribute in Magento 2 Programmatically using Data Patch

A data patch class is a that remembers all data modification instructions. Therefore, the Magento data patch helps to add the product attribute easily. You just need to create one PHP file.

Step 1: Firstly, you need to add customdatapatch.php in the directory app\code\Vendor\Extention\Setup\Patch\Data. Now paste the below code.

<?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 customdatapatch 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: After that, run the following command in cmd.

php bin/magento setup:upgrade

You're done.

That's all you need to do to create product attributes in Magento 2 programmatically.

We hope this article on how to create product attributes in Magento 2 programmatically using a data patch helps you. However, in the eCommerce product attributes are selected by the product type. So add custom product attributes based on the product type.

Moreover, you can also check out another article How to create category attributes using a data patch in Magento 2.

Feel free to ask any of your doubt in the comment section. We will surely help you resolve your issue. Else you can directly contact us.

Happy Coding:)