Magento 2 Update product additional option added in add to cartAttribute added to Mini Cart Magento 2Set...

Biological Blimps: Propulsion

Mimic lecturing on blackboard, facing audience

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Which Article Helped Get Rid of Technobabble in RPGs?

What does Apple's new App Store requirement mean

What to do when eye contact makes your coworker uncomfortable?

What is going on with gets(stdin) on the site coderbyte?

Is it allowed to activate the ability of multiple planeswalkers in a single turn?

Merge org tables

How do you make your own symbol when Detexify fails?

Is there a RAID 0 Equivalent for RAM?

Which was the first story featuring espers?

Can you use Vicious Mockery to win an argument or gain favours?

Does the reader need to like the PoV character?

Confused about Cramer-Rao lower bound and CLT

Is it necessary to use pronouns with the verb "essere"?

Is there a nicer/politer/more positive alternative for "negates"?

How much theory knowledge is actually used while playing?

Why does Carol not get rid of the Kree symbol on her suit when she changes its colours?

Non-trope happy ending?

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Why Shazam when there is already Superman?

Make a Bowl of Alphabet Soup

Creating two special characters



Magento 2 Update product additional option added in add to cart


Attribute added to Mini Cart Magento 2Set custom price of product when adding to cart code not workingMagento : Not able to set posted value in the observerPrice not getting updated for second product…how to add a new customizable product optionLocalization for Cart/Quote APIs doesn't work properly in magento 2Magento2 : How to add a custom option to Item in cartAdd event after user add the product to cart to update quantityMagento 2.2.5: Add, Update and Delete existing products Custom Options













4















I have configurable product in my website and I added the product additional option in add to cart to hold the custom value, Now on cart page I want to allow the user to update those additional option value.



I created a custom controller to handle the edit action, But here the value updated by the user is not reflecting into the cart.



Please give me your suggestion to fix the issue, Below is my code.



My Observer code



catalog_product_load_after:

public function execute(MagentoFrameworkEventObserver $observer) {
// Check and set information according to your need
if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
$post = $this->_request->getParam('personalized');

$additionalOptions = [];
$additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text1');
$additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text2');

if (count($additionalOptions) > 0) {
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}


The above code is working fine, I am able to add these additonal option in cart and it's displaying in cart page.



Now for updating on cart page I have done the following code which is not working.



$quoteObj = $this->_cart->getQuote();

$item_id = 201;
$item = $quoteObj->getItemById($item_id);


if (!empty($item)) {


$additionalOptions = [];


### Setting up additional options
$additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text3');
$additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text4');


if (count($additionalOptions) > 0) {

$item->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}

$item->getProduct()->setIsSuperMode(true);
$item->save();
}









share|improve this question














bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    4















    I have configurable product in my website and I added the product additional option in add to cart to hold the custom value, Now on cart page I want to allow the user to update those additional option value.



    I created a custom controller to handle the edit action, But here the value updated by the user is not reflecting into the cart.



    Please give me your suggestion to fix the issue, Below is my code.



    My Observer code



    catalog_product_load_after:

    public function execute(MagentoFrameworkEventObserver $observer) {
    // Check and set information according to your need
    if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
    $post = $this->_request->getParam('personalized');

    $additionalOptions = [];
    $additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text1');
    $additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text2');

    if (count($additionalOptions) > 0) {
    $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
    }
    }
    }


    The above code is working fine, I am able to add these additonal option in cart and it's displaying in cart page.



    Now for updating on cart page I have done the following code which is not working.



    $quoteObj = $this->_cart->getQuote();

    $item_id = 201;
    $item = $quoteObj->getItemById($item_id);


    if (!empty($item)) {


    $additionalOptions = [];


    ### Setting up additional options
    $additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text3');
    $additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text4');


    if (count($additionalOptions) > 0) {

    $item->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
    }

    $item->getProduct()->setIsSuperMode(true);
    $item->save();
    }









    share|improve this question














    bumped to the homepage by Community 10 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      4












      4








      4








      I have configurable product in my website and I added the product additional option in add to cart to hold the custom value, Now on cart page I want to allow the user to update those additional option value.



      I created a custom controller to handle the edit action, But here the value updated by the user is not reflecting into the cart.



      Please give me your suggestion to fix the issue, Below is my code.



      My Observer code



      catalog_product_load_after:

      public function execute(MagentoFrameworkEventObserver $observer) {
      // Check and set information according to your need
      if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
      $post = $this->_request->getParam('personalized');

      $additionalOptions = [];
      $additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text1');
      $additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text2');

      if (count($additionalOptions) > 0) {
      $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
      }
      }
      }


      The above code is working fine, I am able to add these additonal option in cart and it's displaying in cart page.



      Now for updating on cart page I have done the following code which is not working.



      $quoteObj = $this->_cart->getQuote();

      $item_id = 201;
      $item = $quoteObj->getItemById($item_id);


      if (!empty($item)) {


      $additionalOptions = [];


      ### Setting up additional options
      $additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text3');
      $additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text4');


      if (count($additionalOptions) > 0) {

      $item->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
      }

      $item->getProduct()->setIsSuperMode(true);
      $item->save();
      }









      share|improve this question














      I have configurable product in my website and I added the product additional option in add to cart to hold the custom value, Now on cart page I want to allow the user to update those additional option value.



      I created a custom controller to handle the edit action, But here the value updated by the user is not reflecting into the cart.



      Please give me your suggestion to fix the issue, Below is my code.



      My Observer code



      catalog_product_load_after:

      public function execute(MagentoFrameworkEventObserver $observer) {
      // Check and set information according to your need
      if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
      $post = $this->_request->getParam('personalized');

      $additionalOptions = [];
      $additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text1');
      $additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text2');

      if (count($additionalOptions) > 0) {
      $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
      }
      }
      }


      The above code is working fine, I am able to add these additonal option in cart and it's displaying in cart page.



      Now for updating on cart page I have done the following code which is not working.



      $quoteObj = $this->_cart->getQuote();

      $item_id = 201;
      $item = $quoteObj->getItemById($item_id);


      if (!empty($item)) {


      $additionalOptions = [];


      ### Setting up additional options
      $additionalOptions[] = array('label' => "labelVal1", 'value' => 'Text3');
      $additionalOptions[] = array('label' => "labelVal2", 'value' => 'Text4');


      if (count($additionalOptions) > 0) {

      $item->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
      }

      $item->getProduct()->setIsSuperMode(true);
      $item->save();
      }






      magento2 magento-2.1 php






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 15 '17 at 12:09









      Irfan.gwbIrfan.gwb

      211




      211





      bumped to the homepage by Community 10 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 10 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          First of all, let's check the MagentoQuoteModelQuoteItem::beforeSave method. This is the only one method invoked after $item->save() call. As you can see, the item object doesn't refresh the custom options from product object. To make it work, you need to pass values directly in quote item using addOption method, like so:



          $item->addOption(['label' => 'some_key', 'value' => $this->request->getPostValue('some_key')]);
          $item->save();


          Also, you need to connect new quote item option with additional_options array on rendering stage.



          Please take into account that additional_options might be used by other developers and it's better to merge your data to this array instead of replacing.






          share|improve this answer























            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%2f205994%2fmagento-2-update-product-additional-option-added-in-add-to-cart%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









            0














            First of all, let's check the MagentoQuoteModelQuoteItem::beforeSave method. This is the only one method invoked after $item->save() call. As you can see, the item object doesn't refresh the custom options from product object. To make it work, you need to pass values directly in quote item using addOption method, like so:



            $item->addOption(['label' => 'some_key', 'value' => $this->request->getPostValue('some_key')]);
            $item->save();


            Also, you need to connect new quote item option with additional_options array on rendering stage.



            Please take into account that additional_options might be used by other developers and it's better to merge your data to this array instead of replacing.






            share|improve this answer




























              0














              First of all, let's check the MagentoQuoteModelQuoteItem::beforeSave method. This is the only one method invoked after $item->save() call. As you can see, the item object doesn't refresh the custom options from product object. To make it work, you need to pass values directly in quote item using addOption method, like so:



              $item->addOption(['label' => 'some_key', 'value' => $this->request->getPostValue('some_key')]);
              $item->save();


              Also, you need to connect new quote item option with additional_options array on rendering stage.



              Please take into account that additional_options might be used by other developers and it's better to merge your data to this array instead of replacing.






              share|improve this answer


























                0












                0








                0







                First of all, let's check the MagentoQuoteModelQuoteItem::beforeSave method. This is the only one method invoked after $item->save() call. As you can see, the item object doesn't refresh the custom options from product object. To make it work, you need to pass values directly in quote item using addOption method, like so:



                $item->addOption(['label' => 'some_key', 'value' => $this->request->getPostValue('some_key')]);
                $item->save();


                Also, you need to connect new quote item option with additional_options array on rendering stage.



                Please take into account that additional_options might be used by other developers and it's better to merge your data to this array instead of replacing.






                share|improve this answer













                First of all, let's check the MagentoQuoteModelQuoteItem::beforeSave method. This is the only one method invoked after $item->save() call. As you can see, the item object doesn't refresh the custom options from product object. To make it work, you need to pass values directly in quote item using addOption method, like so:



                $item->addOption(['label' => 'some_key', 'value' => $this->request->getPostValue('some_key')]);
                $item->save();


                Also, you need to connect new quote item option with additional_options array on rendering stage.



                Please take into account that additional_options might be used by other developers and it's better to merge your data to this array instead of replacing.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 18 '17 at 17:00









                Andrey KonosovAndrey Konosov

                381111




                381111






























                    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%2f205994%2fmagento-2-update-product-additional-option-added-in-add-to-cart%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

                    迭戈·戈丁...

                    A phrase ”follow into" in a context The 2019 Stack Overflow Developer Survey Results Are...

                    1960s short story making fun of James Bond-style spy fiction The 2019 Stack Overflow Developer...