1

I have a php code for creating product custom options programmatically but don't know where to place this code.

`

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

`

Any help?

0

Code looks like its a custom code outside magento as you have added Mage.php etc.

So you need to create a create_custom_options.php(as per your requirement) on your root directory of magento and place the code to that file.

Hope this helps.

2
  • I tried but no option is displaying.Can you describe in detail? I want to create custom options for all products. – Shoaib Saleem Apr 30 '18 at 8:28
  • My custom options dropdown showing error in prototype.js:612 how can i solve this error? magento.stackexchange.com/q/266640/57334 @Sukumar Gorai – zus Mar 20 '19 at 9:33
0

In the directory where all magento files and folders are placed like "app" , "downloader" , "errors" , "media" etc folders are places , create php file call createcustomoptions.php and write your code in it.

After it Run it through the site url like: www.example.com/createcustomoptions.php

where example.com will be your site url

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.