Create custom option programatically magento


If you want to create product custom option dynamically using observer when creating product just follow the steps.

Add below code in you module etc file.

<events>
	  <catalog_product_save_before> <!-- identifier of the event we want to catch -->
        <observers>
          <catalog_product_save_before_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>customoptioncreater/observer</class> <!-- observers class alias -->
            <method>createMyCustomOption</method>  <!-- observer's method to be called -->
           </catalog_product_save_before_handler>
        </observers>
</event

Create observer for listen event

public function createMyCustomOption(Varien_Event_Observer $observer)
    {
        $product = $observer->getProduct();
        
        if ($product->getAdult() != '' && $product->getChild() != '') {
            
            
            $options = $product->getProductOptions();
            if ($options) {
                foreach ($options as $option) {
                    
                    if ($option['title'] == 'Adult' || $option['title'] == 'Child') {
                        //we've already added the option
                        return;
                    }
                    
                }
            }
            $adult         = $product->getAdult(); /* this is my custom attribute to get value */
            $adultPrice    = $product->getAdultPrice();  /* this is my custom attribute to get value */
            $child         = $product->getChild();  /* this is my custom attribute to get value */
            $childPrice    = $product->getChildPrice();  /* this is my custom attribute to get value */
            $isRequired    = array(
                1,
                0
            );
            $optionTitle   = array(
                'Adult',
                'Child'
            );
            $optionPrice   = array(
                $adultPrice,
                $childPrice
            );
            $adultchildFor = array(
                $adult,
                $child
            );
            $optionsArray  = $this->getOptions($optionTitle, 'drop_down', $adultchildFor, $optionPrice, $isRequired);
            try {
                $product->setCanSaveCustomOptions(true);
                foreach ($optionsArray as $option) {
                    $product->getOptionInstance()->addOption($option);
                }
                
                $product->setHasOptions(true);
            }
            catch (Exception $e) {
                Mage::Log($e->getMessage());
            }
            //echo '<pre>';
            
            //print_r($options);
            //exit;
            //$productmo = Mage::getModel('catalog/product')->load($product->getId());
            //$productmo->setProductOptions($options);
            //$productmo->setCanSaveCustomOptions(true);
            //$productmo->setHasOptions(1)->save();
            //return;
        }
        
    }
    
    protected function getOptions($title = '', $type = '', $adultchildFor, $optionPrice, $isRequired)
    {
        
        if (is_array($title)) {
            $j = 0;
            foreach ($title as $ttl) {
                $option[$j]               = array();
                $optionvalue              = array();
                $option[$j]['title']      = $ttl;
                $option[$j]['type']       = $type;
                $option[$j]['is_require'] = $isRequired[$j];
                $option[$j]['sort_order'] = $j;
                for ($i = 1; $i <= $adultchildFor[$j]; $i++) {
                    $optionvalue[$i]['title']      = $i;
                    $optionvalue[$i]['price']      = $i * $optionPrice[$j];
                    $optionvalue[$i]['price_type'] = 'fixed';
                    $optionvalue[$i]['sku']        = '';
                    $optionvalue[$i]['sort_order'] = $i;
                    
                }
                $option[$j]['values'] = $optionvalue;
                $j++;
            }
        } else {
            $optionvalue = array();
            for ($i = 1; $i <= $for; $i++) {
                $optionvalue[$i]['title']      = $i;
                $optionvalue[$i]['price']      = $i * $price;
                $optionvalue[$i]['price_type'] = 'fixed';
                $optionvalue[$i]['sku']        = '';
                $optionvalue[$i]['sort_order'] = $i;
            }
            $option = array(
                'title' => (string) $title,
                'type' => $type, // could be drop_down ,checkbox , multiple
                'is_require' => $isrequired,
                'sort_order' => 0,
                'values' => $optionvalue
            );
            
            
        }
        return $option;
    }

and you are done.Happy Codding 🙂

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s