Wednesday 29 June 2016

How to create multiple custom options programmatically Magento ?


 To create a multiple custom options of a product in single time you can use  below  piece of code

<?php
require_once 'app/Mage.php';
Mage::app();
$productId = 905;   // your product id
$product = Mage::getModel('catalog/product')->load($productId);
                    $options = array(
                        array(
                        'title' => 'Color',
                        'type' => 'drop_down',
                        'is_required' => 1,
                        'sort_order' => 0,
                        'values' => array(
                                        array(
                                            'title' => 'Red',
                                            'price' => 10.50,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        ),
                                        array(
                                            'title' => 'Gold',
                                            'price' => 0,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        )
                                    )
                        ),
                        array('title' => 'size',
                        'type' => 'drop_down',
                        'is_required' => 1,
                        'sort_order' => 0,
                        'values' => array(
                                        array(
                                            'title' => 'small',
                                            'price' => 10.50,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        ),
                                        array(
                                            'title' => 'large',
                                            'price' => 0,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        )
                                    )
                        )
            );
//print_r($options);die;
 foreach($options as $options1){
$product->getOptionInstance()->unsetOptions();
$optionsnew = array($options1);
$product->setProductOptions($optionsnew);
$product->setCanSaveCustomOptions(true);
$product->save();
}
?>