Adding cusom options automatically to magento products

Hello friends in this post i’m going to give you a nice code to add custom options to your product when it is saved. After searching long time got good resources with that i have created an event to add custom options.

Magento having lot of feature to customize the product

We can add different prices to a single product using custom options.

Adding custom option for a product is easy, but if you want to add same custom option for every product that you are creating will take more time.

So here i have written an observer to add custom options to product when it is created at first time.

Below code will add two color variation custom option for your product when it is created.

Add the below code in your module config.xml. If you want to know hoe to create module refer Magento event module creation post


<global>
<events>
<catalog_product_save_after><!--Event name-->
<observers>
<Company_Myproducts_Model_Addcustomoptions_Observer><!--Unique identifier name-->
<type>singleton</type>
<class>Company_Myproducts_Model_Addcustomoptions_Observer</class><!--Observer Class name-->
<method>add_custom_options</method><!--Observer Class method name-->
</Company_Myproducts_Model_Addcustomoptions_Observer>
</observers>
</catalog_product_save_after>
</events>
</global>

Create a file in app/code/local/Company/Myproducts/Model/Addcustomoptions/Observer.php and copy/paste below code on it


<?php
class Company_Myproducts_Model_Addcustomoptions_Observer extends Mage_Core_Helper_Abstract{
public function add_custom_options(Varien_Event_Observer $observer){

$event = $observer->getEvent();
$product = $event->getProduct();
$arrayOption = array();

//For Creating dropdown,select,multiselect,radio type of custom option
$arrayOption[] = $this->setCustomOption("Red,Blue", "Color variations", "drop_down");

//For Creating textfield and textarea type of custom option
//$arrayOption[] = $this->setCustomOption("Anyvalue", "Area", "area", true);

//Loading the product that is newly created
$product = Mage::getModel("catalog/product")->load($product->getId());
if(count($product->getOptions()) == 0){
foreach ($arrayOption as $options) {
foreach ($options as $option) {
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
}
//Enabling created options to show in backend as well as frontend.
$resource = Mage::getSingleton('core/resource');
$tableName = $resource->getTableName('catalog_product_entity');
$sql = "UPDATE $tableName SET has_options = '1' WHERE entity_id =".$product->getId();
$writeConnection = $resource->getConnection('core_write')->query($sql);
}
}


/**
* @param $value - Must be comma separated options.
* @param $title - Title of the custom option.
* @param $type - Type of custom option - drop_down,radio,checkbox,multiple,area,field.
* @param $noOption - Specifies if the custom options has options or not.
*/
protected function setCustomOption($value, $title, $type, $noOption = false)
{
$custom_options = array();
if ($type && $value != "" && $value) {
$values = explode(',', $value);
if (count($values)) {
/**If the custom option has options*/
if (! $noOption) {
$is_required = 0;
$sort_order = 0;
$custom_options[] = array(
'is_delete' => 0 ,
'title' => $title ,
'previous_group' => '' ,
'previous_type' => '' ,
'type' => $type ,
'is_require' => $is_required ,
'sort_order' => $sort_order ,
'values' => array()
);
foreach ($values as $v) {
$titleopt = ucfirst(trim($v));
switch ($type) {
case 'drop_down':
case 'radio':
case 'checkbox':
case 'multiple':
default:
$title = ucfirst(trim($v));
$custom_options[count($custom_options) - 1]['values'][] = array(
'is_delete' => 0 , 'title' => $titleopt , 'option_type_id' => - 1 , 'price_type' => 'fixed' , 'price' => '' , 'sku' => '' , 'sort_order' => ''
);
break;
}
}
return $custom_options;
}
/**If the custom option doesn't have options | Case: area and field*/
else {
$is_required = 0;
$sort_order = '';
$custom_options[] = array(
"is_delete" => 0 , "title" => $title , "previous_group" => "text" , "price_type" => 'fixed' , "price" => '' , "type" => $type , "is_required" => $is_required
);
return $custom_options;
}
}
}
return false;
}
}?>

Want to know more please refer this link Adding custom options in magento product.

4 Comments

 Add your comment
  1. Hi balaluna,
    I have updated the code, thanks 🙂

  2. hmm.. "event" to "events"

  3. Thanks. Finally a decent tutorial. You should change to in your config file. Otherwise Good Work!

  4. Hello,

    For Add Product Options Value Using Magento API and Excel Sheet great help at folllowing url :

    http://www.webslike.com/Thread-How-To-Add-Product-Options-Value-Using-Magento-API-and-Excel-Sheet

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: