Magento – Programmatically added custom options not showing in admin panel

custom-optionsmagento-1.9

I am programmatically adding products from the front end along with custom options. After adding, the custom options are missing from the product in the admin. They only show up when I add another option from admin.
I am using this for custom options:

$product->setCanSaveCustomOptions(true);
$product->getOptionInstance()->addOption($optiondata);
$product->setHasOptions(true); 

Best Answer

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'
        ),

    );
}
Related Topic