1

Can we delete product attribute options by using label?

For example say attribute:color has options like green, red, yellow how to delete red from this? I used below code.

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $eavConfig = $objectManager->get('Magento\Eav\Model\Config');

        $attribute = $eavConfig->getAttribute('catalog_product', 'color);

        $id = $attribute->getAttributeId();

        $options = $attribute->getSource()->getAllOptions();
        foreach ($options as $option) {             
            $options['delete'][$option['value']] = true; 
            $options['value'][$option['value']] = true;
        }

        $eavSetup = $this->_objectManager->get('Magento\Eav\Setup\EavSetup');
        $eavSetup->addAttributeOption($options);

It is deleting all the options. I need to delete specific option.

  • It works great. But what if i want to add a new option? Can i use something like $options['create'][$option['value']] = true; $options['value'][$option['Yellow']] = true; – axel Feb 4 '19 at 10:35
5

Try the below code:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$eavAttribute = $objectManager->create('Magento\Eav\Model\Config');
$attribute = $eavAttribute->getAttribute('catalog_product', 'color');

$options = $attribute->getSource()->getAllOptions();

foreach ($options as $option) {
    if($option['label'] == 'Red'){
        $options['delete'][$option['value']] = true; 
        $options['value'][$option['value']] = true;
    }
}

$setupObject = $objectManager->create('Magento\Eav\Setup\EavSetup');
$setupObject->addAttributeOption($options);
?>
  • I have used your code with slight changes, deleting options in bulk by reading the json into array with foreach, i have posted the answer below, that is not working. can you pls check it where i am wrong – jafar pinjar Jan 23 '19 at 4:45
  • Create a question, I ll check and answer. Also provide the json value in correct format. – Sukumar Gorai Jan 23 '19 at 8:16
  • here is my question, magento.stackexchange.com/questions/258787/… – jafar pinjar Jan 23 '19 at 8:19
0
   "OptionValues": [ 
         { 
            "OptionId": "22",  
            "OptionName": "Test" 
         },
         { 
            "OptionId": "21",
            "OptionName" : "Test 2"
         } 

      <?php
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

     $eavAttribute = $objectManager->create('Magento\Eav\Model\Config');
     $attribute = $eavAttribute->getAttribute('catalog_product', 'color');

       $options = $attribute->getSource()->getAllOptions();
       foreach($OptionValues as $optValue){
                    $optionIdVal = $optValue['OptionName'];
                    foreach ($options as $option) {
                        if($option['label'] == $optionIdVal){
                            $options['delete'][$option['value']] = true; 
                            $options['value'][$option['value']] = true;
                        }
                    }
                }


     $setupObject = $objectManager->create('Magento\Eav\Setup\EavSetup');
     $setupObject->addAttributeOption($options);
  ?>

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.