How to Update Product Attribute using Data Patch in Magento 2

 Steps to Update Product Attribute using Data Patch in Magento 2:

Step 1: First, create the ColorAttribute.php file inside the Setup folder


app\code\Vendor\Extension\Setup\Patch\Data


Then add the below code


<?php

namespace Vendor\Extension\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 ColorAttribute implements DataPatchInterface

{

    private $moduleDataSetup;

    private $eavSetupFactory;

 

    public function __construct(

        ModuleDataSetupInterface $moduleDataSetup,

        EavSetupFactory $eavSetupFactory

    ) {

        $this->moduleDataSetup = $moduleDataSetup;

        $this->eavSetupFactory = $eavSetupFactory;

      }

      public function apply()

      {   

          $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

          $eavSetup->updateAttribute(

          Product::ENTITY,

          'color',

            [

              'frontend_label' => 'Paint'

            ]

          );

      }

      public static function getDependencies()

      {

          return [];

      }

      public function getAliases()

      {

           return [];

      }     

}


Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2