I am trying to add a custom option programmatically. Below is piece of code I tried to attempt.
Problem is for my first product get updated with one custom option where as my second product gets updated with two custom options.
product 1 with one custom option. enter image description here product 2 with two custom options. enter image description here
I dont know what is going wrong. Please advice how to address this issues. I am using magento 1.9.x version.
<?php
require_once 'app/Mage.php';
require_once 'Zend/Crypt/Hmac.php';
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
function getOptions(){
return array(
array(
'title' => 'Option Value 1',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'Option Value 2',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'Option Value 3',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
)
);
}
$option = array(
'title' => 'custom option title',
'type' => 'radio', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);
$obj = Mage::getModel('catalog/product');
$product_id = $obj->getIdBySku('skuid1');
$product = $obj->load($product_id);
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);
$product->save();
unset($product);
echo "Done";
$product_id = $obj->getIdBySku('skuid2');
$product = $obj->load($product_id);
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);
$product->save();
unset($product);
echo "Done";
?>
2 Answers 2
<?php
require_once 'app/Mage.php';
require_once 'Zend/Crypt/Hmac.php';
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
function getOptions(){
return array(
array(
'title' => 'Option Value 1',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'Option Value 2',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'Option Value 3',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
)
);
}
$option = array(
'title' => 'custom option title',
'type' => 'radio', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);
$obj = Mage::getModel('catalog/product');
$product_id = $obj->getIdBySku('skuid1');
$product = $obj->load($product_id);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->addOption($option);
$optionInstance->setProduct($product);
$product->save();
unset($product);
echo "Done";
$product_id = $obj->getIdBySku('skuid2');
$product = $obj->load($product_id);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->addOption($option);
$optionInstance->setProduct($product);
$product->save();
unset($product);
echo "Done";
?>
try this script it works fine iin my system
-
glad to help you @shakthydossKeyur Shah– Keyur Shah2015年07月09日 08:03:42 +00:00Commented Jul 9, 2015 at 8:03
-
My custom options dropdown showing error in prototype.js:612 how can i solve this error? magento.stackexchange.com/q/266640/57334 @Keyur Shahzus– zus2019年03月20日 09:35:12 +00:00Commented Mar 20, 2019 at 9:35
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$app = Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
Mage::app()->setCurrentStore(0);
function getOptions(){
return array(
array(
'title' => 'Option 1',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'Option 2',
'price' =>100,
'price_type' => 'fixed',
'sort_order' => '1'
)
);
}
$option = array(
'title' => 'Custom Name',
'type' => 'radio', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);
$obj = Mage::getModel('catalog/product');
$product_id = $obj->getIdBySku('skuid1');
$product = $obj->load($product_id);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->setProduct($product);
$product->setProductOptions($option);
$product->setCanSaveCustomOptions(true);
$product->save();
unset($product);
?>
-
Hi Sagar, welcome to Magento SE! Thank you for your answer to this question. I see a lot of code, which is good. Could you maybe add some explanation to what your code is doing (for instance step by step) and what part specifically solves the question that was asked? Thanks! and keep up the good work!7ochem– 7ochem2017年03月31日 11:05:19 +00:00Commented Mar 31, 2017 at 11:05
unset($product )after the first product custom option creation