0

How to add an new option to product dropdown attribute? If the option already exist it has to skip adding else add option with new value.

I followed the below link How Magento2 add attribute option programmatically (not in setup) where it is creating the option but inserting duplicate if already the option is present.

And can we get the option id after insert is success?

asked Jul 6, 2018 at 15:40

2 Answers 2

5

Try following way:


protected $eavAttributeFactory;
protected $attributeOptionManagement;
public function __construct(
 \Magento\Eav\Model\Entity\AttributeFactory $eavAttributeFactory,
 \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement
) {
 $this->eavAttributeFactory = $eavAttributeFactory;
 $this->attributeOptionManagement = $attributeOptionManagement;
}

And then


$magentoAttribute = $this->eavAttributeFactory->create()->loadByCode('catalog_product', 'test_attribute');
$attributeCode = $magentoAttribute->getAttributeCode();
$magentoAttributeOptions = $this->attributeOptionManagement->getItems(
 'catalog_product',
 $attributeCode
);
$attributeOptions = ['Test2', 'Test3'];
$existingMagentoAttributeOptions = [];
$newOptions = [];
$counter = 0;
foreach($magentoAttributeOptions as $option) {
 if (!$option->getValue()) {
 continue;
 }
 if($option->getLabel() instanceof \Magento\Framework\Phrase) {
 $label = $option->getText();
 } else {
 $label = $option->getLabel();
 }
 if($label == '') {
 continue;
 }
 $existingMagentoAttributeOptions[] = $label;
 $newOptions['value'][$option->getValue()] = [$label, $label];
 $counter++;
}
foreach ($attributeOptions as $option) {
 if($option == '') {
 continue;
 }
 if(!in_array($option, $existingMagentoAttributeOptions)) {
 $newOptions['value']['option_'.$counter] = [$option, $option];
 }
 $counter++;
}
if(count($newOptions)) {
 $magentoAttribute->setOption($newOptions)->save();
}
answered Jul 6, 2018 at 17:05
4
  • Hi, this code works Perfectly but can we get the option id inserted once the attribute option is saved. Commented Jul 9, 2018 at 6:44
  • And is there any code to delete the attribute option? Commented Jul 9, 2018 at 6:51
  • @sohail, similar issue I am facing here, can u update me pls, magento.stackexchange.com/questions/258787/… Commented Jan 23, 2019 at 4:20
  • Hi,Thanks you saved my day. But I have two stores, the value is updating only in first store Commented Apr 13, 2020 at 15:37
0

And if we wants to update existing attribute options also insert the new one?

I have a dropdown attribute with in "Admin" value a code and in "Default Store View" a value.

If I found an existing attribute I must update the Default Store View.

Thanks!

answered Sep 10, 2019 at 10:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.