5

I m creating a custom option programmatically using observer with the catalog_product_save_before event.The custom option was created successfully on product save. but the problem is whenever the product saves a second time it will create the same option on every save action.So I want to remove all the options first before save and recreate all the options on save. My observer code for creating custom option is as below

public function createMyCustomOption(Varien_Event_Observer $observer)
{
    //Mage::getSingleton('catalog/product_option')->unsetOptions();
    $this->saveProductTabData($observer);
    $product = $observer->getProduct();
    $globlaOptions = unserialize(Mage::getStoreConfig("toursection/tourgroup/product_option"));
    $globlaOptionsArray = array();
    foreach ($globlaOptions as $key=>$value){
        $item['optiontitle'] = $value['optiontitle'];
        $item['optionprice'] = $value['optionprice'];
        $globlaOptionsArray[] = $item;
    }
    //print_r($globlaOptionsArray);
   
    //print_r($options);exit;
    
        $options = $product->getProductOptions();
    echo '<pre>';
    
      print_r($options);exit;
        /*if ($options) {
            foreach ($options as $option) {
                if ($option['title'] == 'Adult' || $option['title'] == 'Child') {
                    return;
                }
            }
        }*/
        
        $noOfAdult         = Mage::getStoreConfig("toursection/tourgroup/noofadult");
        $noOfchild         = Mage::getStoreConfig("toursection/tourgroup/nofochild");
        $adultPrice    = $product->getAdultOurprice();
        $childPrice    = $product->getChildOurprice();
        if(!$product_id = Mage::app()->getRequest()->getParam('id')){
                $productModel = Mage::getModel('catalog/product');
                $product_entity_table = $productModel->getResource()->getEntityTable();    
                $resource = Mage::getSingleton('core/resource');
                $connection = $resource->getConnection('core_read');
                $result = $connection->showTableStatus($product_entity_table);
                $product_id = $result['Auto_increment'];
            }
        $optionModel = Mage::getModel('touroptions/touroptions')->load($product_id,'product_id');
        $tourOptionArray = unserialize($optionModel->getTouroptions());
        print_r($tourOptionArray);
        $optionsTitle = array('Adult','Child');
        $optionsPrice = array($adultPrice,$childPrice);
        $noOfOption = array(
            $noOfAdult,
            $noOfchild
        );
        $optionsSku = array(NULL,NULL);
        $isRequired    = array(
            1
        );
        $typeArray = array(
        'drop_down',
        'drop_down'
        );
        $inc = 0;
        foreach($globlaOptionsArray as $title){
            if(!in_array($title['optiontitle'],$tourOptionArray)){
                continue;
            }
            $optionsTitle[]=$title['optiontitle'];
            $optionsPrice[]=$title['optionprice'];
            $noOfOption[] = '1';
            $optionsSku[] = $tourOptionArray['optionsku'][$inc];
            $isRequired[] = 0;
            $typeArray[] = 'checkbox';
            $inc++;
            
        }
        
      
        $optionsArray  = $this->getOptions($optionsTitle, $typeArray, $noOfOption, $optionsPrice, $isRequired , $optionsSku);
        //echo '<pre>';
        //print_r($optionsArray);exit;
        try {
            $product->setCanSaveCustomOptions(true);
            foreach ($optionsArray as $option) {
                $product->getOptionInstance()->addOption($option);
            }
            $product->setHasOptions(true);
        }
        catch (Exception $e) {
            Mage::Log($e->getMessage());
        }
       
    
    
}
protected function getOptions($optionsTitle = '', $typeArray = '', $noOfOption, $optionsPrice, $isRequired, $optionsSku)
{
    
    if (is_array($optionsTitle)) {
        $j = 0;
        foreach ($optionsTitle as $ttl) {
            $option[$j]               = array();
            $optionvalue              = array();
            $option[$j]['title']      = $ttl;
            $option[$j]['type']       = $typeArray[$j];
            $option[$j]['is_require'] = $isRequired[$j];
            $option[$j]['sort_order'] = $j;
            for ($i = 1; $i <= $noOfOption[$j]; $i++) {
                $optionvalue[$i]['title']      = $i;
                $optionvalue[$i]['price']      = $i * $optionsPrice[$j];
                $optionvalue[$i]['price_type'] = 'fixed';
                $optionvalue[$i]['sku']        = $optionsSku[$j];
                $optionvalue[$i]['sort_order'] = $i;
                
            }
            $option[$j]['values'] = $optionvalue;
            $j++;
        }
    } 
    return $option;
}

any help would be much appreciated. Thanks.

0

Try this before adding your new options:

$oldOptions = $product->getOptionInstance()->getOptions();
foreach ($oldOptions as $option){
    $option->setIsDelete(true);
}
1
  • I tried your code but it give me error Call to a member function setIsDelete() on a non-object. Jan 18 '14 at 9:52
-1

I just had the same problem, the answer from marius did not work for me. I got "Call to a member function setIsDelete() on a non-object" because $product->getOptionInstance()->getOptions() returns an array.

This is what worked for me:

$oldOptions = $product->getOptionInstance()->getOptions();
foreach ($oldOptions as $key => $option){
    if($option['title'] == "Custom Option Title") {
        $oldOptions[$key]['is_delete'] = 1;                 
        $product->getOptionInstance()->addOption($oldOptions[$key]);
    }
}

After deleting you can add your updated custom option with this code:

$newOption = array(
    'title' => "Custom Option Title",
    'type' => 'radio',
    'is_require' => 1,
    'sort_order' => 20,
    'values' => array(
        array(
            'title' => "Value 1 Title",
            'price' => 42.00,
            'price_type' => 'fixed',
            'sku' => "",
            'sort_order' => '1'
        ),
        array(
            'title' => "Value 2 Title",
            'price' => 50,
            'price_type' => 'percent',
            'sku' => "",
            'sort_order' => '2'
        )
    )
);
$product->getOptionInstance()->addOption($newOption);

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.