0

I want to set some options on certain products through an observer. I cannot use regular Custom Options saved to the product because they do not meet our requirement.

I am able to add field type options without problems using:

    $options = array(
        array(
            'title'     => $type->getDescription(),
            'type'      => 'field',
            'is_require'=> 0,
            'id'        => $type->getId(),
            'price'     => '20',
            'price_type'=> 'fixed',
            'sku'       => $type->getSku(),
            'sort_order'=> 0
        )
    );
    /*

        */
    // Unset options and declare new options
    $optionInstance = $product->getOptionInstance()->unsetOptions();
    $product->setHasOptions(true);

    if (count($options)) {
        foreach ($options as $option) {
            $optionModel = Mage::getModel('catalog/product_option')
                ->setProductId($product->getId())
                ->setStoreId($product->getStoreId())
                ->setId($option['id'])
                ->addData($option);
            $product->addOption($optionModel);
        }
    }

I have read every tutorial I can find on creating Custom Options programmatically. There seems to be several ways to do this, but this method was the only one that seems to work. If you answer and simply provide a link to some Wordpress tutorial copied from some other site with unreadable code formatting and incorrect encoding, you will just be downvoted.

I want to add radio type options to this product. Everything I have found suggests this should be as simple as adding a values key to the option array:

    $options = array(
        array(
            'title'     => $type->getDescription(),
            'type'      => 'radio',
            'is_require'=> 0,
            'id'        => $type->getId(),
            'price'     => '20',
            'price_type'=> 'fixed',
            'sku'       => $type->getSku(),
            'sort_order'=> 0,
            'values'    => array(
                array(
                    'price' => '10',
                    'price_type' => 'fixed',
                    'sku' => 'some sku',
                    'title' => 'some title',
                    'id' => 3
                ),
                array(
                    'price' => '10',
                    'price_type' => 'fixed',
                    'sku' => 'some sku 2',
                    'title' => 'some title also',
                    'id' => 6
                )
            )
        )
    );

This flat out does not work. All I see in the frontend is a single radio input with 'None' as the title. Trying some different methods for adding the option to the product, sometimes I will see two radio inputs, the first says 'None' and the second input is the second value array.

I want to set radio/select options to a product while observing catalog_controller_product_view, how can this be done?

1 Answer 1

2

Ultimately, I relied on my instinct rather than on poorly written tutorials.

Instantiate new models for each option and each option's values. This is the key.

$product = $o->getEvent()->getProduct();
$optionModel = Mage::getModel('catalog/product_option')
    ->setTitle('My title')
    ->setProductId($product->getId())
    ->setStoreId($product->getStoreId())
    ->setId($myOptionId)
    ->setType('radio')
    ->setPrice(null)
    ->setPriceType(null)
    ->setSku($myOptionSku)
    ->setIsRequire(true);

$myValues = getModel('my_module/custom_model')->getCollection();

foreach ($myValues as $myValue) {
    $valueModel = Mage::getModel('catalog/product_option_value')
        ->setTitle($myValue->getDescription())
        ->setProduct($product)
        ->setOption($optionModel)
        ->setId($myValue->getId())
        ->setPrice($myValue->getMultiplier())
        ->setPriceType('fixed')
        ->setSku($myValue->getDescription());
    $optionModel->addValue($valueModel);
}
$product->setHasOptions(1);
$product->addOption($optionModel);
4
  • feel free to accept this if it is working Jan 24, 2015 at 18:17
  • How are you getting the option to save onto the quote? I only see it in the info_buyRequest; it doesn't seem to be saved to the options array when I call Mage::helper('catalog/product_configuration')->getCustomOptions($quoteItem);
    – Laura
    May 22, 2015 at 0:02
  • I will have to go back and look, but as I recall they get added through the additional_options option.
    – pspahn
    May 22, 2015 at 0:30
  • this is working Jan 9, 2016 at 11:33

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.