How to add dynamic Select Custom Options in ObserverChanging the product custom option rendererCustom Options Price ValueCustom options operationsAdd custom options pragmatically in productsMagento2 : How to add a new product custom option type?Magento 2 custom options title not working while csv importhow to create custom product options that there price is dependent on another?how many custom options a product can haveHow to edit the custom option price type programmatically?Add class or data-attribute to product optionsMagento 2 - How to show amount "save $100 considering custom options also on detail page?

Can a creature tell when it has been affected by a Divination wizard's Portent?

Pulling the rope with one hand is as heavy as with two hands?

Has any spacecraft ever had the ability to directly communicate with civilian air traffic control?

Advice on laptop battery life

Sci-fi novel series with instant travel between planets through gates. A river runs through the gates

Is GOCE a satellite or aircraft?

How to back up a running remote server?

TikZ how to make supply and demand arrows for nodes?

How deep to place a deadman anchor for a slackline?

What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?

How can I get precisely a certain cubic cm by changing the following factors?

Packing rectangles: Does rotation ever help?

Does jamais mean always or never in this context?

Is it possible to Ready a spell to be cast just before the start of your next turn by having the trigger be an ally's attack?

Why does processed meat contain preservatives, while canned fish needs not?

Colliding particles and Activation energy

In gnome-terminal only 2 out of 3 zoom keys work

Can fracking help reduce CO2?

How to creep the reader out with what seems like a normal person?

Confusion about capacitors

How to set the font color of quantity objects (Version 11.3 vs version 12)?

Unexpected email from Yorkshire Bank

How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?

Is thermodynamics only applicable to systems in equilibrium?



How to add dynamic Select Custom Options in Observer


Changing the product custom option rendererCustom Options Price ValueCustom options operationsAdd custom options pragmatically in productsMagento2 : How to add a new product custom option type?Magento 2 custom options title not working while csv importhow to create custom product options that there price is dependent on another?how many custom options a product can haveHow to edit the custom option price type programmatically?Add class or data-attribute to product optionsMagento 2 - How to show amount "save $100 considering custom options also on detail page?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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?










share|improve this question






























    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?










    share|improve this question


























      0












      0








      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?










      share|improve this question
















      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?







      custom-options






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 16 '15 at 19:55







      pspahn

















      asked Jan 16 '15 at 19:11









      pspahnpspahn

      3,70822457




      3,70822457




















          1 Answer
          1






          active

          oldest

          votes


















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





          share|improve this answer























          • feel free to accept this if it is working

            – David Manners
            Jan 24 '15 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 '15 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 '15 at 0:30











          • this is working

            – Shyam Ranpara
            Jan 9 '16 at 11:33











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f52412%2fhow-to-add-dynamic-select-custom-options-in-observer%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









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





          share|improve this answer























          • feel free to accept this if it is working

            – David Manners
            Jan 24 '15 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 '15 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 '15 at 0:30











          • this is working

            – Shyam Ranpara
            Jan 9 '16 at 11:33















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





          share|improve this answer























          • feel free to accept this if it is working

            – David Manners
            Jan 24 '15 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 '15 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 '15 at 0:30











          • this is working

            – Shyam Ranpara
            Jan 9 '16 at 11:33













          2












          2








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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 '15 at 22:35









          pspahnpspahn

          3,70822457




          3,70822457












          • feel free to accept this if it is working

            – David Manners
            Jan 24 '15 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 '15 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 '15 at 0:30











          • this is working

            – Shyam Ranpara
            Jan 9 '16 at 11:33

















          • feel free to accept this if it is working

            – David Manners
            Jan 24 '15 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 '15 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 '15 at 0:30











          • this is working

            – Shyam Ranpara
            Jan 9 '16 at 11:33
















          feel free to accept this if it is working

          – David Manners
          Jan 24 '15 at 18:17





          feel free to accept this if it is working

          – David Manners
          Jan 24 '15 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 '15 at 0:02






          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 '15 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 '15 at 0:30





          I will have to go back and look, but as I recall they get added through the additional_options option.

          – pspahn
          May 22 '15 at 0:30













          this is working

          – Shyam Ranpara
          Jan 9 '16 at 11:33





          this is working

          – Shyam Ranpara
          Jan 9 '16 at 11:33

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f52412%2fhow-to-add-dynamic-select-custom-options-in-observer%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form