Monday, July 15, 2013

Magento : How to add custom options programmatically

Here guys i am here with another interesting post on magento for creating product custom options programmatically.

[php]
$option = array(
'title' => 'Your custom option title',
'type' => 'radio', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);

function getOptions(){
return array(
array(
'title' => 'Option Value 1',
'price' =>100,
'price_type' => 'fixed',
'sku' => 'any sku for 1',
'sort_order' => '1'
),
array(
'title' => 'Option Value 2',
'price' =>100,
'price_type' => 'fixed',
'sku' => 'any sku for 2',
'sort_order' => '1'
),
array(
'title' => 'Option Value 3',
'price' =>100,
'price_type' => 'fixed',
'sku' => 'any sku for 3',
'sort_order' => '1'
)
);
}

//Suppose we are creating a new product.
$product = Mage::getModel('catalog/product');
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);

//Or if we are adding the options to a already created product.
$product = Mage::getModel('catalog/product')->load($id);
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);

//Do not forget to save the product
$product->save();
[/php]

Hope it helps some one :-)

3 comments:

  1. Hey, looks good so far. But how to add different values for custom options per store view.

    ReplyDelete
  2. thanx bro its good for me and working fien

    ReplyDelete
  3. thanks... it works for me.

    ReplyDelete