How to Create Custom Product Attribute Programmatically in Magento 2

 In Magento blog

In this blog, we are going to show you how to create a product attribute using the latest Declarative

Schema “data patch “.

A 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.

Once you Create the file AddCustomProductAttribute.php in this directory .  app/code/VenderName/ModuleName/ Setup/Patch/Data/. and paste the below code :

AddCustomProductAttribute.php

after will running the below command :

  • php -dmemory_limit=2G bin/magento setup:upgrade
  • php -dmemory_limit=2G bin/magento setup:static-content:deploy -f -j
  • php -dmemory_limit=2G bin/magento cache:flush

Now, you can see in the eav_attribute table that your custom product attribute was created successfully using the data patch.

If you have any questions let me know in the comment section.

Thank you!

Leave a Comment


0