In this tutorial, Today I will explain to how to create a product attribute using data patches in Magento 2. From Magento 2.3.x, Magento brings new features which is data patches. The Data Patch is class that contains data notification instruction.
Previously, Magento 2 uses InstallData and UpgradeData file use add data in core table or custom table. From Magento 2.3 it will be replaced by Data Patch.
How to Add Data Patch in Magento 2.3 :
It’s defined in <Vendor_Name>/<Module_Name>/Setup/Patch/Data/<Patch_File_Name>.php and it will implement \Magento\Setup\Model\Patch\DataPatchInterface interface.
So, Let’s follow the steps :
You may also like this :
- How to Create Product Attribute Programmatically in Magento 2
- How to Add Custom Customer Attribute in Magento 2
Steps of how to create product attribute using data patch in Magento 2 :
1) First of all, Let’s assume that you have created simple module. Now, To create custom product attribute Create CreateCustomAttr.php file at app/code/RH/Helloworld/Setup/Patch/Data/ and paste the below code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Created By : Rohan Hapani
*/
declare (strict_types = 1);
namespace RH\Helloworld\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
/**
* Class CreateCustomAttr for Create Custom Product Attribute using Data Patch.
*/
class CreateCustomAttr implements DataPatchInterface {
/**
* ModuleDataSetupInterface
*
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* EavSetupFactory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $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', 'product_temp_attribute', [
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Product Temp Atrribute',
'input' => 'text',
'class' => '',
'source' => '',
'global' => 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,
'apply_to' => '',
]);
}
/**
* {@inheritdoc}
*/
public static function getDependencies() {
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases() {
return [];
}
}
Here, In this above file there are some new functions available in that file code. So, Let’s understand short details about the use of that functions.
First of all, apply() function is use to implement code logic to installing or upgrading data to the database. For ex, here you can see that there are create product attribute code logic apply inside apply() function.
After that, getAliases() function defines aliases for the patch class. Sometimes, When you want to change the class name then it could possible using getAliases() function. If it does, then we should add old class name so, it’s not executed a second time.
In Last, getVersion() function will return a version of the patch. If the version of the module will be high than the version we specify in our patch, then it will not get executed. It will executed only when it’s equal to or lower than the version here.
2) In Last, Now just execute this below command :
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
That’s it !!
Now, you can see in eav_attribute table that your custom product attribute created successfully using data patch.
I hope this blog is easy to understand about how to create a product attribute using data patches in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.
Keep liking and sharing !!