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!

CynoInfotech

CynoInfotech was founded in 2019, in Ahmedabad, Gujrat. we have a trusted Web Development and Design Company that helps businesses in streamlining their operations through Magento 2 Extension and advanced modern technology solutions.

Let’s connect