0

I would like to add value field. Find the below screenshot for custom options in magento2

Screenshot: enter image description here

0

Use Below Code.

$productFactory = $this->_objectManager->get("\Magento\Catalog\Model\ProductFactory");
$product = $productFactory->create()->load(1);
$options = $product->getOptions();
// \Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface
$productOptionValue = $this->_objectManager->create("\Magento\Catalog\Model\Product\Option\Value");

$newValue = array(
        "sku" => "xxl", 
        "title" => "XXL",  
        "price" => 10,
        "price_type" => "percent"
    );
$productOptionValue->addValue($newValue);


if(count($options) > 0) {
    foreach($options as $key=>$option){ 
        if($option->getTitle() == "Size"){      
            try {
                $productOptionValue->setOption($option)->saveValues();   
                echo "value added";
            } catch (Exception $e) {
               echo $e->getMessage();
            }
        }
    }
}

Instead of Using _objectManager define class in to __construct method

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.