1

i developed a module that automatically imports simple products to magento. Furthermore it adds custom options for every simple product.

As i can see in the database everything is working fine. My products are there and also the custom options.

The big problem now is, that these custom options are not visible in magento backend or frontend. If i add an additional custom option manually to any product, then the automatical created options also appear in backend and frontend. Strange thing.

Here you can see my function to create custom options for a given product. Is there anything wrong?

THX

protected function _createCustomOptionsForProduct($product, $event){
    try{
        // create custom options for given product
        $options = array(
            //'is_delete' => '',
            //'previous_type' => '',
            //'previous_group' => 'select',
            //'id' => '1',
            //'option_id' => '0',
            'product_id' => $product->getId(),
            'title' => 'Price Category',
            'type' => 'drop_down',
            'is_require' => true,
            'sort_order' => '1',
            'values' => array()
        );

        // iterate over pricecategories and their containing price models
        $counter = 1;
        if(count($event->getData('priceCategories')) > 1) {
            foreach($event->getData('priceCategories') as $priceCategory ) {

                foreach($priceCategory->getData('priceModels') as $priceModel){

                    // check if price model is active otherwise skip it
                    if($priceModel->getData('active') == true){
                        // fill values array of custom option
                        // containing price category and price model
                        $options['values'][] = array(
                            //'option_type_id' => '-1',
                            //'is_delete' => '',
                            'title' => $priceCategory->getData('name') . ' - ' . $priceModel->getData('name') ,
                            'price' => number_format( $priceModel->getData('price')/100, 2 ),
                            'price_type' => 'fixed',
                            'sku' => $priceModel->getData('key'), // price model key as sku
                            'sort_order' => $counter
                        );
                    }
                    $counter++;
                }
            }
        } else {
            foreach($event->getData('priceCategories')->getData('priceModels') as $priceModel){
                // check if price model is active otherwise skip it
                if($priceModel->getData('active') == true){
                    // fill values array of custom option
                    // containing price category and price model
                    $options['values'][] = array(
                        //'option_type_id' => '-1',
                        //'is_delete' => '',
                        'title' => $event->getData('priceCategories')->getData('name') . ' - ' . $priceModel->getData('name') ,
                        'price' => number_format( $priceModel->getData('price')/100, 2 ),
                        'price_type' => 'fixed',
                        'sku' => $priceModel->getData('key'), // price model key as sku
                        'sort_order' => $counter
                    );
                }
                $counter++;
            }
        }

        if(count($options['values']) > 0 && !$product->getOptionsReadonly()) {

            $product->setHasOptions(1);

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

            //$product->setProductOptions(array($options));

            $product->setCanSaveCustomOptions(true);

            return true;
        }

        return false;

    }catch (Exception $e){
        Mage::log('Error: could not create custom ticket / product options for '.
            $product->getName(), ZEND_LOG::CRIT, 'blafoo.log', false);

        Mage::log($e->getMessage(), ZEND_LOG::CRIT, 'blafoo.log', false);

        return false;
    }

Deleting caches or reindexing data didn't help.

  • Wonder if you're just missing a simple $product->save() after the options have been applied. – pspahn Aug 23 '13 at 19:02
  • No its there outside of this function. :-( Any other hints? – Andre Aus B Aug 26 '13 at 6:55
  • @AndreAusB did you try it without commenting out the line $product->setProductOptions(array($options)) – David Manners Jun 27 '14 at 13:43
  • See this answer: magento.stackexchange.com/a/10118/146 It may help explaining to what the problem is about creating custom options using the save events. – Marius Jun 27 '14 at 13:57
1

You should be able to simply set the options against a product and then call save on a product as the options are created in the _afterSave. After creating your options array simply set that the product has options, attach the options and then continue with your normal save process.

$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true);

Forget about creating the options as the product model should do this for you, so you no longer need the following code.

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

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.