0

I am trying to add custom options in my product programmatically with a observer when I save an custom product in admin:

<catalog_product_save_before>
    <observers>
        <fourward_comprandoforaproduct>
            <type>singleton</type>
            <class>comprandoforaproduct/observeradmin</class>
            <method>catalogProductSaveBefore</method>
        </fourward_comprandoforaproduct>
    </observers>
</catalog_product_save_before>

and this is my observer:

public function catalogProductSaveBefore($observer) {
    $product = $observer->getProduct();

    if ($product->getTypeID() == "cartaovirtual") { //this is my custom product type
        //here I am failing to delete old custom option
        $product->setCanSaveCustomOptions(true);
        $oldOptions = $product->getOptionInstance()->getOptions();
        foreach ($oldOptions as $key => $option) {
            if ($oldOptions[$key]['title'] == "Número do Cartão") {
                $oldOptions[$key]['is_delete'] = 1;
            }
        }
        $product->getOptionInstance()->setOptions($oldOptions);

        //here I create my custom option (this works fine!)
        $option = array(
            "title" => "Número do Cartão",
            "type" => "field",
            "is_require" => 1,
            "sort_order" => 1,
            "price" => 0,
            "price_type" => "fixed",
            "sku" => ""
        );

        $product->getOptionInstance()->addOption($option);
        $product->setHasOptions(true);
    }
}

I can only delete the "Custom Options" when I'm editing the product and I click on the tab "Custom Options".

Eg.

  • If I open this product type, change the product name and save, the product creates new attributes and does not delete the old attributes.

  • If I open this product type, change the product name, open "Custom Options" tab (without making any changes) and save. This works correctly deleting the old attributes and create the new attribute.

Thanks

  • His problem is different, and that reply code has the same problem as mine. – Eduardo Jul 30 '14 at 19:04
  • 1
    Shouldn't $product->getTypeID() be $product->getTypeId()? – benmarks Jul 30 '14 at 19:14
  • Either one or the other work. – Eduardo Jul 30 '14 at 19:22
0

have you tried doing:

$product->setProductOptions($oldOptions);

instead of:

$product->getOptionInstance()->setOptions($oldOptions);
  • Thanks for your reply. I tried but this doesn't worked. – Eduardo Jul 30 '14 at 19:08

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