1

I have created an observer on catalog_product_save_before in order to add custom options to a product.

What i don't manage to achieve is clearing out existing custom options beforehand.

Here is my code:

       $product = $observer->getProduct();    

       $option1 = array (
            'is_delete' => 0,
            'title' => 'extra Option for that product',
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 1,
            'values' => array (
                0 => array (
                    'value_id' => '3',
                    'title' => 'Yes',
                    'price' => 10.00,
                    'price_type' => 'fixed',
                    'sku' => NULL,
                    'sort_order' => 1,
                ),
                1 => array (
                    'value_id' => 3,
                    'title' => 'No',
                    'price' => 0.00,
                    'price_type' => 'fixed',
                    'sku' => NULL,
                    'sort_order' => 2,
                ),
            ),
        );           


    $product->setCanSaveCustomOptions(true);    
    $product->getOptionInstance();
    /* @var $product Mage_Catalog_Model_Product_Option */
    $product->getOptionInstance()->unsetOptions();
    $product->getOptionInstance()->addOption($option1);         
    $product->setHasOptions(true);

this does not seem to have any effect:

     $product->getOptionInstance()->unsetOptions();
3

i have ended up doing it like this :

         $resource = Mage::getSingleton('core/resource');    
         $writeConnection = $resource->getConnection('core_write'); 
         $table = '`catalog_product_option` O JOIN `catalog_product_option_type_value` V on O.option_id=V.option_id';    
         $query = "DELETE O, V FROM {$table} WHERE `product_id`=".(int)$product->getId()."";     
         $writeConnection->query($query);
1

Try using:

$product->getOptionInstance()->setOptions($option1);   

This will reset the entire $this->_options array to the supplied array

/**
 * Set options for array
 *
 * @param array $options
 * @return Mage_Catalog_Model_Product_Option
 */
public function setOptions($options)
{
    $this->_options = $options;
    return $this;
}

Edit: The above didn't seem to function as expected.

Why not try removing all the options before we make any changes?

$oldOptions = $product->getOptionInstance()->getOptions();
foreach ($oldOptions as $key => $option){
    $oldOptions[$key]['is_delete'] = 1;
}
// Set the old options back into the product options
$product->getOptionInstance()->setOptions($oldOptions);
// Save with the is_delete key
$product->getOptionInstance()->saveOptions();
  • no, this does not reset the entire options – Matoeil Jul 3 '14 at 11:11
  • @Matoeil I've updated my response with some more code, hopefully this will solve your issue. – Dave Jul 3 '14 at 11:17
  • this does not work coz $option is an array and not an object – Matoeil Jul 3 '14 at 12:37
  • @Matoeil Whoops! Sorry I'm reading the source and trying to interrupt stuff, I've updated it again to update the array. You may need to call the saveOptions function after. – Dave Jul 3 '14 at 12:45
  • Thanx for your help.do u know how to put the modified array back to the object then? could u provide a full working code? – Matoeil Jul 3 '14 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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