I have created custom options programatically with reference of below url : https://webkul.com/blog/create-custom-option-programmatically-in-magento2/ How to update any option value or option details programatically for options which are already added in product.
2 Answers
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = 1;
$_product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId, true);
$newValues = array();
foreach ($_product->getOptions() as $option) {
$optionValues = $option->getValues();
}
}
foreach($optionValues as $value)
{
$newValues[$value->getId()]['option_type_id']= $value->getId();
$newValues[$value->getId()]['title']= 'Custom Title';
$newValues[$value->getId()]['price']= 100;
$newValues[$value->getId()]['price_type']= 'fixed';
}
$value->setValues($newValues);
$value->saveValues();
$_product->save();
Try this:
$product->setData('your_option_name', $yourOptionValue);
$product->save();