0

I created a setup Patch file to update the label of attribute options.

Tried below code

Vendor\Custom\Setup\Patch\Data\UpdateColorAttributeOptions.php

  <?php

  namespace Vendor\Custom\Setup\Patch\Data;

  use Magento\Eav\Api\AttributeRepositoryInterface;
  use Magento\Eav\Api\Data\AttributeOptionLabelInterfaceFactory;
  use Magento\Eav\Api\Data\AttributeOptionInterface;
  use Magento\Eav\Model\Config;
  use Magento\Framework\Setup\Patch\DataPatchInterface;
  use Magento\Framework\Setup\ModuleDataSetupInterface;
  use Magento\Framework\App\ResourceConnection;

class UpdateColorAttributeOptions implements DataPatchInterface
 {
  private $moduleDataSetup;
  private $eavConfig;
  private $attributeRepository;
  private $optionLabelFactory;
  private $resource;

  public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    Config $eavConfig,
    AttributeRepositoryInterface $attributeRepository,
    AttributeOptionLabelInterfaceFactory $optionLabelFactory,
    ResourceConnection $resource
) {
    $this->moduleDataSetup = $moduleDataSetup;
    $this->eavConfig = $eavConfig;
    $this->attributeRepository = $attributeRepository;
    $this->optionLabelFactory = $optionLabelFactory;
    $this->resource = $resource;
}

public function apply()
{
    
    $this->moduleDataSetup->getConnection()->startSetup();

    // Define attribute code and the options you want to update
    $attributeCode = 'color'; // Replace with your attribute code
    $optionsToUpdate = [
        'Red Color' => 'Red',
        'Green Color' => 'Green',
    ];

    // Load the attribute
    $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);

   
    $options = $attribute->getOptions();
   
    foreach ($options as $option) {
        foreach ($optionsToUpdate as $oldLabel => $newLabel) {
            if ($option->getLabel() === $oldLabel) {
                /** @var AttributeOptionInterface $option */
                $option->setLabel($newLabel);
                $attribute->setOptions([$option]); 
                $this->attributeRepository->save($attribute);
            }
        }
    }


    $this->moduleDataSetup->getConnection()->endSetup();
}

public static function getDependencies()
{
    return [];
}

   public function getAliases()
   {
    return [];
   }
}

The above code is not updating the labels of attribute options, Please someone check and let me know where I am doing wrong.

Thanks in Advance!

1 Answer 1

0

I have made it work with below code.

<?php

namespace Vendor\Custom\Setup\Patch\Data;

use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Api\Data\AttributeOptionLabelInterfaceFactory;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Eav\Model\Config;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\App\ResourceConnection;

class UpdateColorAttributeOptions implements DataPatchInterface
{ 
 private $moduleDataSetup;
 private $eavConfig;
 private $attributeRepository;
 private $optionLabelFactory;
 private $resource;

 public function __construct(
 ModuleDataSetupInterface $moduleDataSetup,
 Config $eavConfig,
 AttributeRepositoryInterface $attributeRepository,
 AttributeOptionLabelInterfaceFactory $optionLabelFactory,
  ResourceConnection $resource
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavConfig = $eavConfig;
$this->attributeRepository = $attributeRepository;
$this->optionLabelFactory = $optionLabelFactory;
$this->resource = $resource;
 }

public function apply()
{

$this->moduleDataSetup->getConnection()->startSetup();

// Define attribute code and the options you want to update
$attributeCode = 'color'; // Replace with your attribute code
$optionsToUpdate = [
    'Red Color' => 'Red',
    'Green Color' => 'Green',
];

// Load the attribute
  $attribute = $this->eavConfig->getAttribute('catalog_product', 
   $attributeCode);


   $options = $attribute->getOptions();

   foreach ($options as $option) {
        foreach ($optionsToUpdate as $oldLabel => $newLabel) {
            if ($option->getLabel() === $oldLabel) {
                $option->setLabel($newLabel); 
                $optionId = $option->getValue();
                $attribute->setOption(['value' => [$optionId => 
                [$newLabel]]]);
                $attribute->save();
                break;
            }
        }
    }


  $this->moduleDataSetup->getConnection()->endSetup();
 }

public static function getDependencies()
{
 return [];
}

public function getAliases()
  {
   return [];
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.