Delete / Unset existing custom options on product

by Matoeil   Last Updated July 12, 2019 05:09 AM

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


Answers 2


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();
Dave
Dave
July 03, 2014 10:55 AM

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);
Matoeil
Matoeil
August 13, 2014 15:06 PM

Related Questions


Updated March 10, 2018 13:09 PM

Updated June 19, 2018 15:09 PM

Updated March 05, 2019 09:09 AM

Updated March 02, 2018 12:09 PM

Updated May 22, 2019 12:09 PM