I was working on something similar today - I would try to get all current options, push your new option(s) onto that array and do this:
Mage::getSingleton('catalog/product_option')->unsetOptions();
$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true)
->setHasOptions(true);
$product->save();
The first line is explained here (not sure if you'd need it actually): https://stackoverflow.com/questions/4006260/magento-accumulating-custom-options-in-script
Edit: By the way, this code is working perfectly for me, however I'm using it in a script in development
I fallowed this way it is working may it will useful for you
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('admin');
$option = array(
'title' => 'custom option title',
'type' => 'drop_down', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);
$collection = Mage::getModel('catalog/product')->getCollection();
foreach ($collection as $product_all) {
$sku = $product_all['sku'];
$product_id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($product_id);
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);
$product->save();
echo $sku;
}
function getOptions() {
return array(
array(
'title' => 'Ship It',
'price' => 0,
'price_type' => 'fixed',
'sku' => $sku,
'sort_order' => '0'
),
array(
'title' => 'Avon',
'price' => -5.00,
'price_type' => 'fixed',
'sku' => $sku,
'sort_order' => '1'
),
array(
'title' => 'Bristol',
'price' => -3.00,
'price_type' => 'fixed',
'sku' => $sku,
'sort_order' => '2'
),
array(
'title' => 'Barkhamstead',
'price' => -3.00,
'price_type' => 'fixed',
'sku' => $sku,
'sort_order' => '3'
),
);
}
Best Answer
Based in the code of webkul.com you have these options:
Load Product
Custom Options
Configurable Options
The last step is check based in Custom or Configurable Options if your option is created in product before and keep it.