Magento, Magento 2

How to Update Product Attribute using Data Patch in Magento 2

How to Update Product Attribute using Data Patch in Magento 2

In this tutorial, Today I will explain to how to update 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.

In addition, Data patch file created 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 :

1) First of all, Let’s assume that you have created simple module. Now, To update product attribute Create UpdateProductAttr.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
 */
namespace RH\Helloworld\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Catalog\Model\Product;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class UpdateProductAttr implements DataPatchInterface
{
     /** @var ModuleDataSetupInterface */
    private $moduleDataSetup;

    /** @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->updateAttribute(Product::ENTITY, 'dob', ['is_required' => 1]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

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 product attribute updated successfully using data patch. However, you can update multiple values of attribute also using data patch.

I hope this blog is easy to understand about how to update 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 !!

Tagged , ,