Question

I’m trying to precomplete custom options during the process of creating or editing a product in the Magento admin : after product type choice.

I add an event trigger on the Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs _prepareLayout function. So basically when you open the edition or creation of a product, the event is triggered.

My aim is : when you start editing your new product, 3 custom options are already present in the “Custom Options” tab.

I can’t trigger it with the catalog_product_save_before event because one option is a dropdown type and have to be filled by the admin.

So i’ve coded my observer and succeeded to modify the product name with setName() function, but I can"t find how to precomplete/add custom options.

I tried with the code of the following blog : http://kamal250.wordpress.com/2012/10/22/create-custom-option-programatically-while-creating-product/#comments

But it doesn’t seems to work.

Anyone can help me with that ?

Here is my code in the observer :

        $option_data = array(
        'is_delete'         => 0,
        'is_require'        => true,
        'previous_group'    => '',
        'title'             => 'Height',
        'type'              => 'field',
        'price_type'        => 'fixed',
        'price'             => '0',
        'sort_order'        => 1,
        'values'            => array()
    );

    $product->setHasOptions(1);
    $product->setCanSaveCustomOptions(1);
    $product->setOptions(array($option_data));
    $product->setProductOptions(array($option_data));

    $opt = Mage::getSingleton('catalog/product_option');
    $opt->setProduct($product);
    $opt->addOption($option_data);
    $opt->saveOptions();
    $product->setOption($opt);
Was it helpful?

Solution

Finally i have found the solution : I modified the trigger to the action of opening the custom options tab.

And here is my code in the observer :

 $product = $observer->getProduct();

    $option_data = array(
        'is_delete'         => 0,
        'is_require'        => true,
        'previous_group'    => '',
        'title'             => 'Height',
        'type'              => 'field',
        'price_type'        => 'fixed',
        'price'             => '0',
        'sort_order'        => 1
    );

    $product->setHasOptions(1);
    $option = Mage::getModel('catalog/product_option')->setProductId($product->getId())->setStoreId($product->getStoreId())->addData($option_data);
    $option->save();
    $product->addOption($option); 

Hope it will help someone sooner or later.

See ya.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top