0

I am trying to update attribute option label with attribute optionid.

Used below code.

class Attribute
 {
  protected $eavAttributeFactory;
  public function __construct(
   \Magento\Eav\Model\Entity\AttributeFactory $eavAttributeFactory
  ) {
    $this->eavAttributeFactory = $eavAttributeFactory;
 }

public function attributeUpdate() { 
  $attribute_code = 'color';
  $optionid = 12345;
  $option_label = 'red';
  $attr = $this->productAttributeRepository->get($attribute_code );
             $options = $attr->getOptions();
             $values = 0;
             foreach ($options as $option) {
                if ($option->getValue() == $magento_id) {
                    $option->setLabel($option_label);
                    //$option->setStoreLabels([$option_label]);
                    $attr->setOptions([$option]);                               
                    $this->productAttributeRepository->save($attr);                     
                    break;
                }
                $values++;

            }

}

Option id 12345 is Green now. I need to Update to Red. What code i need to use to update label of option. Please anyone help on this. Right now it is updating only in admin end. not for all stores, like in Default store view Label in empty. Thanks

  • please check my answer. – Rohan Hapani Dec 14 '18 at 12:53
  • @RohanHapani, It is updating only in Admin, but not in other Store views. can you look into it please? – jafar pinjar Dec 14 '18 at 14:24
  • @RohanHapani, have u checked from ur end? foreach seems not correct – jafar pinjar Dec 15 '18 at 6:09
  • I updated my answer. – Rohan Hapani Dec 15 '18 at 7:42
  • 1
    @RohanHapani, i am checking it. site is down now, will updated u once it worked – jafar pinjar Dec 17 '18 at 5:15
1

Try to use this below code :

protected $_attributeFactory;

public function __construct(
    ....
    \Magento\Framework\App\Action\Context $context,
    \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory
    ....
) {
    ....
    $this->_attributeFactory = $attributeFactory;
    parent::__construct($context);
    ....
}
public function yourFunction()
{
    $attribute_id = 93; //color attribute id
    $attribute = $this->_attributeFactory->load(93);

    $option_id = 213; // your option ID
    $opt_default_name = 'Red Lab';
    $opt_default_store = 'Red Lab';
    // $opt_2nd_store = 'Red Lab German'; //For other store

    $attribute->setData('option', array('value' => array(
        $option_id => array($opt_default_name, $opt_default_store)), //$opt_2nd_store add param here for other store
    ));
    $attribute->save();
}

For more you can take a look at \Magento\Eav\Model\ResourceModel\Entity\Attribute::_saveStoreLabels(AbstractModel $object)

  • hi @Rohan, its not worked for me – jafar pinjar Dec 14 '18 at 13:07
  • Did you remove generated and flush cache? – Rohan Hapani Dec 14 '18 at 13:12
  • yes, got this error Uncaught TypeError: Argument 1 passed to Magento\\Eav\\Model\\Entity\\Attribute\\Option::setStoreLabels() – jafar pinjar Dec 14 '18 at 13:12
  • Try to apply just setLabel() – Rohan Hapani Dec 14 '18 at 13:13
  • yes before used that one only, its not updated, is it necessary to use this line? $product_options->setOptions([$option]); – jafar pinjar Dec 14 '18 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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