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

Hello Everyone,

In this blog, we will learn about how to Create New Product Attributes Programmatically using Data Patch in Magento 2.

Data Patch feature is introduced from Magento 2.3.x.

Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Product Attribute in Magento 2.

STEPS FOR CREATE PRODUCT ATTRIBUTE IN MAGENTO 2

Step 1: Create ProductAttribute.php file

app/code/Vendor/Extension/Setup/Patch/Data/ProductAttribute.php

<?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 ProductAttribute 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_product_attribute', [
           'type' => 'int',
           'backend' => '',
           'frontend' => '',
           'label' => 'Custom Product Attribute',
           'input' => 'select',
           'class' => 'custom_product_attribute_class',
           'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
           'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
           'visible' => true,
           'required' => false,
           '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';
   }
}

In above code we created an int type attribute you can change as per your requirements.

Step 2: Finally run the below commands

$ php bin/magento setup:upgrade
$ php bin/magento cache:clean
$ php bin/magento cache:flush

Step 3: Output:

Above created product attribute is stored in  eav_attribute table.

Now go to Admin -> Catalog -> Products -> <select any product> -> Edit. 

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Create Product Attribute Programmatically in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *