How to Create Custom Product Attribute Using Data Patch in Magento 2
In this post, you’ll learn the complete programmatic method to create custom product attributes using data patch in Magento 2. 😀
Magento 2 custom product attributes help the merchants better organize the product information. It is also helpful in increasing the overall visibility of the products and delivering a rich experience. Often, merchants are required to do that! Custom product attributes in Magento 2 can be easily created from the admin panel, which is quite a no-brainer task.
But what if you want to do that programmatically using a data patch?
Let’s find it out through this micro-post on – Magento 2 create product attribute using data patch.
Step-by-step Method to Create Custom Product Attributes Using Data Patch in Magento 2
In Magento 2, data patches are the classes that contain schema and instructions for data manipulation.
The patches are applied only once.
They’re also executed during the Magento upgrade.
Now, let’s see how to add custom product attribute programmatically using data patch in Magento 2.
Create CreateCustomProductAttr.php file at {Vendor}\{Extension}\Setup\Patch\Data directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?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 CreateCustomProductAttr implements DataPatchInterface { private $moduleDataSetup; private $eavSetupFactory; public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute('catalog_product', 'attribute_code', [ 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'attribute label', 'input' => 'boolean', 'class' => '', 'source' => \Magento\Eav\Model\Entity\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' => false, 'unique' => false, 'apply_to' => '', ]); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } } |
In the above code, replace the Vendor and Extension with respective folder names.
Also, tweak the code as per the properties of the custom product attribute. You can change the attribute type, label, input type, and more per your requirements.
Once done, run the following Magento Upgrade command to apply the data patch:
1 |
php bin/magento setup:upgrade |
That’s how you can add product attribute using data patch in Magento 2.
You can find the newly created attribute in the product edit section.
Learned something new today? Share it with your developer friends 🙂
If you find this post helpful, do not forget to rate this post with stars.
Thank you for reading! 🍀
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
Magento Website Audit Checklist to Level up Your Website
How to Cancel or Delete Orders in Shopify – Step-wise Tutorial
Next