How to create a product attribute using Data Patches

In this tutorial, we will learn how to create product attributes using data patches. In Magento 2.3.x, magento introduced a new concept Data patch for add/update magento eva attributes.

A Data patch is a class that contains data modification instructions. You can defined data patch class at /Setup/Patch/Data/.php file and that implements \Magento\Framework\Setup\Patch\DataPatchInterface interface. Let’s start with how to add attributes with a data patch.

Step 1: we assume that you have created a simple module with required files.

Step 2: To create a custom product attribute ‘subtitle’ with AddSubTitleProductAttribute.php file at path app/code/Mageclues/ProductAttr/Setup/Patch/Data/

<?php

namespace Mageclues\ProductAttr\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

class AddSubTitleProductAttribute implements DataPatchInterface
{
    protected $_moduleDataSetup;
    protected $_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('catalog_product', 'product_subtitle', [
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'label' => 'Sub Title',
            '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' => '',
        ]);
    }
   
    public static function getDependencies() 
    {
        return [];
    }
   
    public function getAliases() 
    {
        return [];
    }

    public static function getVersion()
    {
        return '1.0.2';
    }
}



The DataPatchInterface is required to implement three functions: apply, getDependencies and getAliases.

The apply() function is used to create or update our custom attribute. We need to create an instance of the EavSetupFactory, passing in our moduleDataSetup object, and add our attribute required configuration. We don’t need to call startSetup and endSetup functions here anymore.

The getDependencies() function, we can return an array of strings that contains class names of dependencies. This tells Magento to execute the “patches” we define here first, before our setup script. With the help of this function Magento controls the order of how patch scripts are executed.

The getAliases() function, we can define aliases for this patch class. When you need to change the class name then it can be possible using the getAliases() function. If it does, then we should add the old class name so that patch is not executed a second time.

The getVersion() function return a string with a version number. It’s an optional method. Here we are looking at how the getVersion() function works with data patches with examples.

If the database version number of the module is lower than the version specified in the file, the patch will execute.

Database Version: 1.0.1
File Version: 1.0.2
Result: 1.0.1 < 1.0.2, patch will execute!

If the database version number of the module is equal to or higher than the version specified in the file, the patch will not execute.

Database Version: 1.0.1
File Version: 1.0.1
Result: 1.0.1 = 1.0.1, patch doesn’t execute!

Database Version: 1.0.6
File Version: 1.0.5
Result: 1.0.6 > 1.0.5, patch doesn’t execute!

Step 3: You need to execute following comment from CLI


php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean

When you run php bin/magento setup:upgrade command then our data patch is executed and the EAV attribute is created. All patches which are successfully executed, Magento inserts a patch record into the patch_list database table with the value of the patch_name field being the value of our patch.

We hope this tutorial is useful for you. Please leave comment if you have any questions.

Thank you):

Related Posts

Leave a Reply

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