5

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";
 ?>
asked Jul 9, 2015 at 5:00
2
  • Please try adding unset($product ) after the first product custom option creation Commented Jul 9, 2015 at 5:36
  • Same problem I added unset($product); after $product->save(); of first product; Commented Jul 9, 2015 at 5:46

2 Answers 2

8
<?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

answered Jul 9, 2015 at 6:59
2
  • glad to help you @shakthydoss Commented 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 Shah Commented Mar 20, 2019 at 9:35
1
<?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);
 ?>
answered Mar 31, 2017 at 10:43
1
  • 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! Commented Mar 31, 2017 at 11:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.