Skip to main content

How to delete color attribute option by using label in magento2Magento 2?

Can we delete product attribute optionoptions by using label  ? for

For example say attribute:color havehas options like green,red red,yellow yellow how to delete red from this?.I 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.

How to delete color attribute option by using label in magento2?

Can we delete product attribute option by using label  ? for example say attribute:color have 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.

How to delete color attribute option by using label in Magento 2?

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.

Source Link
Jafar Pinjar
  • 2k
  • 7
  • 70
  • 144

How to delete color attribute option by using label in magento2?

Can we delete product attribute option by using label ? for example say attribute:color have 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.