3

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";

        ?>
  • Please try adding unset($product ) after the first product custom option creation – Rahul Kumar Das Jul 9 '15 at 5:36
  • Same problem I added unset($product); after $product->save(); of first product; – shakthydoss Jul 9 '15 at 5:46
7
<?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

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);

    ?>
  • 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 Mar 31 '17 at 11:05

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.