3

I m creating custom optoin programatically on catalog_product_save_after event .but the problem is when observer run it will create 3 to 4 same option instead of just create one option on save.one another problem is when there is no custom option in product it will not create any option when i create just one option manualy and then save it will work.

enter image description here

Observer code

public function createMyCustomOption(Varien_Event_Observer $observer)
    {
        $product = $observer->getProduct();
        $options = $this->getOptions('test','drop_down',8,40,1);
        $product->setHasOptions(1);
        $option = Mage::getModel('catalog/product_option')
                  ->setProductId($product->getId())
                  ->setStoreId($product->getStoreId())
                  ->addData($options);
        try {
            $option->save();

        }catch (Exception $e){
            echo $e->getMessage();
            exit('error');
        }
        $product->addOption($option);

        //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='',$for=NULL,$price=1,$isrequired = 1){

    $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;
    }
  • try this event "catalog_product_prepare_save" – Keyul Shah Dec 17 '13 at 5:07
  • and try to log data using Mage::log('your message'); so can find how many times this message logged – Keyul Shah Dec 17 '13 at 5:09
  • @Keyul Shah Thanks for your quick replay.I have logged saved entry it display only once still custom option create two to three times. – Zaheerabbas Dec 17 '13 at 5:29
  • If he logged once then your observer called one time there is some mistake in code please check your code twise – Keyul Shah Dec 17 '13 at 5:31
  • yes @Marius that's the exactly i was looking for.Thanks – Zaheerabbas Dec 17 '13 at 8:29
4

you can try below code for the create custom option.

 $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();
2

You might want to check if the product created is a new product or is an edited old product. Otherwise any change that the admin makes to a product will result in new custom options.

You could check if the origData is empty or something like that but a while ago I stumbled on an article about the isObjectNew flag which might be of use here. You can read the article here.

It probably also prevents your code from being triggered multiple times when using the check.

Not the answer you're looking for? Browse other questions tagged or ask your own question.