Magento Observer and Adding Custom Options Programmatically

Magento Observer is one of the powerful feature of magento, it enable you to do anything on any magento event.

Like you want to run a method when customer adding product in cart, or you want to run a script when admin creating product in back-end etc.

you have to bind your method with event. here event is “catalog_product_save_before”

 

            
                
                    
                        aa_cartmonitor/observer
                        myDoSomethingMethod
                    
                
            
        

here the method which adds custom options when u create product

public function enforceSingleOrderLimit($observer)
    {
        /* if (!$this->_helper->isModuleEnabled()) {
             return;
         }*/

        $event = $observer->getEvent();
        $product = $event->getProduct();
        //check that we haven't made the option already
        $options = $product->getOptions();
        $arr = true;
        //if (sizeof($options)) {
            foreach ($options as $option) {
                if (($option['title'] == 'Finish') && ($option['type'] == 'drop_down') && (!$option['is_delete'])) {
                    $arr = false;
                    mail('[email protected]', 'mage obj', sizeof($options));
                    return;
                }
                if (($option['title'] == 'Metal') && ($option['type'] == 'drop_down') && (!$option['is_delete'])) {
                    $arr = false;
                    return;
                }
                if (($option['title'] == 'Size') && ($option['type'] == 'drop_down') && (!$option['is_delete'])) {
                    $arr = false;
                    return;
                }
                if (($option['title'] == 'Year') && ($option['type'] == 'drop_down') && (!$option['is_delete'])) {
                    $arr = false;
                    return;
                }
                if (($option['title'] == 'Inside Engraving') && ($option['type'] == 'field') && (!$option['is_delete'])) {
                    $arr = false;
                    return;
                }
                if (($option['title'] == 'Additional Info') && ($option['type'] == 'field') && (!$option['is_delete'])) {
                    $arr = false;
                    return;
                }
            }
        //}
        $option = array(
            'title' => 'Finish',
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 2,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group' => '',
            'price' => '0.00',
            'price_type' => 'fixed',
            'sku' => '',
            'values' => array(
                array(
                    'is_delete' => 0,
                    'title' => 'Faceted',
                    'price_type' => 'fixed',
                    'price' => 0,
                    'option_type_id' => -1,
                ),
                array(
                    'is_delete' => 0,
                    'title' => 'Smooth',
                    'price_type' => 'fixed',
                    'price' => 0,
                    'option_type_id' => -1,
                )
            )
        );

        $option2 = array(
            'title' => 'Metal',
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 1,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group' => '',
            'price' => '0.00',
            'price_type' => 'fixed',
            'sku' => '',
            'values' => array(
                array(
                    'is_delete' => 0,
                    'title' => '10k Gold',
                    'price_type' => 'fixed',
                    'price' => 695,
                    'option_type_id' => -1,
                ), 
                array(
                    'is_delete' => 0,
                    'title' => 'Celestrium',
                    'price_type' => 'fixed',
                    'price' => 1095,
                    'option_type_id' => -1,
                )
            )
        );

        $option3 = array(
            'title' => 'Size',
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 4,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group' => '',
            'price' => '0.00',
            'price_type' => 'fixed',
            'sku' => '',
            'values' => array(
                array(
                    'is_delete' => 0,
                    'title' => '4',
                    'option_type_id' => -1,
                    'price' => '0.00',
                    'price_type' => 'fixed',
                    'sort_order' => 1
                ), array(
                    'is_delete' => 0,
                    'title' => '15.5',
                    'option_type_id' => -1,
                    'price' => '0.00',
                    'price_type' => 'fixed',
                    'sort_order' => 25
                )
            )
        );

        $option4 = array(
            'title' => 'Year',
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 5,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group' => '',
            'price' => '0.00',
            'price_type' => 'fixed',
            'sku' => '',
            'values' => array(
                array(
                    'is_delete' => 0,
                    'title' => '2015',
                    'option_type_id' => -1,
                    'price' => '0.00',
                    'price_type' => 'fixed',
                ),array(
                    'is_delete' => 0,
                    'title' => '2014',
                    'option_type_id' => -1,
                    'price' => '0.00',
                    'price_type' => 'fixed',
                ),array(
                    'is_delete' => 0,
                    'title' => '2000',
                    'option_type_id' => -1,
                    'price' => '0.00',
                    'price_type' => 'fixed',
                )
            )
        );
        
        $option6 = array(
            'title' => 'Additional Info',
            'type' => 'field',
            'is_require' => 0,
            'sort_order' => 7,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group' => '',
            'price' => '0.00',
            'price_type' => 'fixed',
            'sku' => ''
        );

        $option7 = array(
            'title' => 'Stone',
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 3,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group' => '',
            'price' => '0.00',
            'price_type' => 'fixed',
            'sku' => '',
            'values' => array(
                array(
                    'is_delete' => 0,
                    'title' => 'Alexandrite',
                    'price_type' => 'fixed',
                    'price' => 0,
                    'option_type_id' => -1,
                ),
                array(
                    'is_delete' => 0,
                    'title' => 'White Spinel',
                    'price_type' => 'fixed',
                    'price' => 0,
                    'option_type_id' => -1,
                )
            )
        );

        if($arr){
//don't use it like this because it has no effect
//$product->setProductOptions($options);
        $product->setCanSaveCustomOptions(true);
//use it this way. Populate `$product->getOptionInstance()` directly
        $product->getOptionInstance()->addOption($option);
        $product->getOptionInstance()->addOption($option2);
        $product->getOptionInstance()->addOption($option3);
        $product->getOptionInstance()->addOption($option4);
        //$product->getOptionInstance()->addOption($option5);
        $product->getOptionInstance()->addOption($option6);
        $product->getOptionInstance()->addOption($option7);
//don't forget to state that the product has custom options
        $product->setHasOptions(true);
        //$product->save();
        }

    }

Posted

in

by

Tags: