Nithin
Nithin

Reputation: 435

Dynamically create custom options for a product

I have created a new Product Type and have created a new tab to specify the details of the product type. Now what i am try to do is dynamically create some custom options upon saving based on the details of the product.

This is what i have tried. I put the following inside product/type.php inside save()

public function save($product = null)
{
     //get $myProductDetails
        $product = $this->getProduct($product);
        foreach($myProductDetails as $fieldName){

                $opt = Mage::getModel('catalog/product_option');
                $opt->setProduct($product);

                $values = array(
                        'title'                => $fieldName,
                        'type'                => 'field',
                        'is_require'        => true
                        );


                $product->setHasOptions(1);
                $opt->setData($values);
                $opt->saveOptions();
                $product->addOption($opt);

    }
    parent::save($product);
}

Now when i debug i see that the options have been added to the $product variable before saving, but after saving when i go to edit the product i dont see these options in the custom options tabs.

Upvotes: 1

Views: 2122

Answers (1)

Nithin
Nithin

Reputation: 435

Figured out the solution this helped. Posting here if it helps someone. I had to use

$opt->addOption($values);

instead of

$opt->setData($values);

Upvotes: 2

Related Questions