How To Add Additional Options In Magento 2 Cart Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?get Some custom attributes in Cart magento 2Adding products with same configurable attributes to cartMagento 2 - Adding 2 (or more) configurable products to cart with custom options causes Integrity constraint violationMagento 2 Some item options or their combination are not currently availableMagento 2 remove options from item cartShow Configurable Product Options in UpSell Block and Add To CartYou need to choose options for your item while adding product to cartYou need to choose options for your item even if option is chosen - Magento 2Magento 2 add products to cart by using code?Add multiple items to cart in Magento2Magento 2 Bundle Product Add To Cart Like Simple Product

Is it OK if I do not take the receipt in Germany?

Why is water being consumed when my shutoff valve is closed?

Philosophers who were composers?

What *exactly* is electrical current, voltage, and resistance?

Arriving in Atlanta (after US Preclearance in Dublin). Will I go through TSA security in Atlanta to transfer to a connecting flight?

Putting Ant-Man on house arrest

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

What to do with someone that cheated their way though university and a PhD program?

Simulate round-robin tournament draw

How do I deal with an erroneously large refund?

What was Apollo 13's "Little Jolt" after MECO?

SQL Server placement of master database files vs resource database files

Why isPrototypeOf() returns false?

`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY

Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?

Is it accepted to use working hours to read general interest books?

What does the black goddess statue do and what is it?

A journey... into the MIND

How would you suggest I follow up with coworkers about our deadline that's today?

Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table

How did Elite on the NES work?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

Why did Israel vote against lifting the American embargo on Cuba?

Has a Nobel Peace laureate ever been accused of war crimes?



How To Add Additional Options In Magento 2 Cart



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?get Some custom attributes in Cart magento 2Adding products with same configurable attributes to cartMagento 2 - Adding 2 (or more) configurable products to cart with custom options causes Integrity constraint violationMagento 2 Some item options or their combination are not currently availableMagento 2 remove options from item cartShow Configurable Product Options in UpSell Block and Add To CartYou need to choose options for your item while adding product to cartYou need to choose options for your item even if option is chosen - Magento 2Magento 2 add products to cart by using code?Add multiple items to cart in Magento2Magento 2 Bundle Product Add To Cart Like Simple Product



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








1















I used this to Add Additional Options In Magento 2 Cart




https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/




But this is working only with Simple product.



For configurable products, I did the following change



$item->addOption(array(
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));


After that, the additional_options going to the cart. But it is not getting saved when complete the order.



seems because of this ….



//filter out config/bundle etc product
if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)

$this->quoteItems[$item->getId()] = $item;



How can I fix it ? I need this functionality for configurable products.










share|improve this question






























    1















    I used this to Add Additional Options In Magento 2 Cart




    https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/




    But this is working only with Simple product.



    For configurable products, I did the following change



    $item->addOption(array(
    'product_id' => $item->getProductId(),
    'code' => 'additional_options',
    'value' => serialize($additionalOptions)
    ));


    After that, the additional_options going to the cart. But it is not getting saved when complete the order.



    seems because of this ….



    //filter out config/bundle etc product
    if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)

    $this->quoteItems[$item->getId()] = $item;



    How can I fix it ? I need this functionality for configurable products.










    share|improve this question


























      1












      1








      1


      1






      I used this to Add Additional Options In Magento 2 Cart




      https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/




      But this is working only with Simple product.



      For configurable products, I did the following change



      $item->addOption(array(
      'product_id' => $item->getProductId(),
      'code' => 'additional_options',
      'value' => serialize($additionalOptions)
      ));


      After that, the additional_options going to the cart. But it is not getting saved when complete the order.



      seems because of this ….



      //filter out config/bundle etc product
      if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)

      $this->quoteItems[$item->getId()] = $item;



      How can I fix it ? I need this functionality for configurable products.










      share|improve this question
















      I used this to Add Additional Options In Magento 2 Cart




      https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/




      But this is working only with Simple product.



      For configurable products, I did the following change



      $item->addOption(array(
      'product_id' => $item->getProductId(),
      'code' => 'additional_options',
      'value' => serialize($additionalOptions)
      ));


      After that, the additional_options going to the cart. But it is not getting saved when complete the order.



      seems because of this ….



      //filter out config/bundle etc product
      if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)

      $this->quoteItems[$item->getId()] = $item;



      How can I fix it ? I need this functionality for configurable products.







      magento2 quoteitem






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 19 at 9:06









      Muhammad Anas

      8031322




      8031322










      asked Jun 30 '17 at 9:30









      Samantha RajasinghaSamantha Rajasingha

      63




      63




















          3 Answers
          3






          active

          oldest

          votes


















          0














          Step 1: Create registration.php and module.xml file the for custom module.



          Step 2: Assign Observers to Events




          /app/code/Namespace/Mymodule/etc/events.xml




           <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
          <event name="catalog_product_load_after">
          <observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
          </event>
          </config>


          Step 3: Create the Observers




          app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php




          <?php
          namespace NamespaceMymoduleObserver;

          use MagentoFrameworkEventObserverInterface;
          use MagentoFrameworkAppRequestInterface;

          class SetAdditionalOptions implements ObserverInterface

          /**
          * @var RequestInterface
          */
          protected $_request;

          /**
          * @param RequestInterface $request
          */
          public function __construct(
          RequestInterface $request
          )
          $this->_request = $request;


          /**
          * @param MagentoFrameworkEventObserver $observer
          */
          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
          $product = $observer->getProduct();
          $additionalOptions = [];
          $additionalOptions[] = array(
          'label' => "Some Label",
          'value' => "Your Information",
          );
          $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));





          run below command after



          rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*

          php bin/magento setup:upgrade

          php bin/magento setup:di:compile

          php bin/magento cache:clean

          php bin/magento cache:flush

          php bin/magento indexer:reindex


          reference links



          https://www.cloudways.com/blog/add-additional-options-in-magento-2/



          https://webkul.com/blog/additional-options-cart-item-magento2/






          share|improve this answer

























          • Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

            – Samantha Rajasingha
            Jun 30 '17 at 12:12











          • @SamanthaRajasingha - Can you refer the above both links?

            – Jjo
            Jun 30 '17 at 12:14












          • I used those links already, I got the same issue from there.

            – Samantha Rajasingha
            Jun 30 '17 at 12:19











          • how to add additional options from the controller by item id in magento 2

            – Prathap Gunasekaran
            Oct 30 '18 at 15:26


















          0














          After add additional options to cart You need to create another observer for save additional attributes order item from quote item.



          Please add this event observer code in Module's etc/events.xml



          <event name="sales_model_service_quote_submit_before">
          <observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
          </event>


          Please create observer file in your module



          VendorModuleObserverSalesOrderItemAdditionalOptions.php


          And here is Observer code for save Additional Options to Order Item.



          <?php
          namespace VendorModuleObserverSales;
          use MagentoFrameworkEventObserverInterface;
          use MagentoFrameworkAppObjectManager;
          use MagentoFrameworkSerializeSerializerJson;
          class OrderItemAdditionalOptions implements ObserverInterface

          public function __construct(
          Json $serializer = null
          )
          $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);

          /**
          * @param MagentoFrameworkEventObserver $observer
          */
          public function execute(MagentoFrameworkEventObserver $observer)

          try
          $quote = $observer->getQuote();
          $order = $observer->getOrder();
          $quoteItems = [];

          // Map Quote Item with Quote Item Id
          foreach ($quote->getAllVisibleItems() as $quoteItem)
          $quoteItems[$quoteItem->getId()] = $quoteItem;


          foreach ($order->getAllVisibleItems() as $orderItem)
          $quoteItemId = $orderItem->getQuoteItemId();
          $quoteItem = $quoteItems[$quoteItemId];
          $additionalOptions = $quoteItem->getOptionByCode('additional_options');


          if (count($additionalOptions) > 0)
          // Get Order Item's other options
          $options = $orderItem->getProductOptions();
          // Set additional options to Order Item
          $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
          $orderItem->setProductOptions($options);


          catch (Exception $e)
          // catch error if any





          Please check this I think it will help you.






          share|improve this answer






























            0














            after $item->addOption(...) try calling $item->saveItemOptions().

            Also a side note, don't add the options as serialized using serialize($additionalOptions), use json_encode or better yet the method MagentoFrameworkSerializeSerializerJson::serialize.






            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%2f181480%2fhow-to-add-additional-options-in-magento-2-cart%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Step 1: Create registration.php and module.xml file the for custom module.



              Step 2: Assign Observers to Events




              /app/code/Namespace/Mymodule/etc/events.xml




               <?xml version="1.0"?>
              <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
              <event name="catalog_product_load_after">
              <observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
              </event>
              </config>


              Step 3: Create the Observers




              app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php




              <?php
              namespace NamespaceMymoduleObserver;

              use MagentoFrameworkEventObserverInterface;
              use MagentoFrameworkAppRequestInterface;

              class SetAdditionalOptions implements ObserverInterface

              /**
              * @var RequestInterface
              */
              protected $_request;

              /**
              * @param RequestInterface $request
              */
              public function __construct(
              RequestInterface $request
              )
              $this->_request = $request;


              /**
              * @param MagentoFrameworkEventObserver $observer
              */
              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
              $product = $observer->getProduct();
              $additionalOptions = [];
              $additionalOptions[] = array(
              'label' => "Some Label",
              'value' => "Your Information",
              );
              $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));





              run below command after



              rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*

              php bin/magento setup:upgrade

              php bin/magento setup:di:compile

              php bin/magento cache:clean

              php bin/magento cache:flush

              php bin/magento indexer:reindex


              reference links



              https://www.cloudways.com/blog/add-additional-options-in-magento-2/



              https://webkul.com/blog/additional-options-cart-item-magento2/






              share|improve this answer

























              • Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

                – Samantha Rajasingha
                Jun 30 '17 at 12:12











              • @SamanthaRajasingha - Can you refer the above both links?

                – Jjo
                Jun 30 '17 at 12:14












              • I used those links already, I got the same issue from there.

                – Samantha Rajasingha
                Jun 30 '17 at 12:19











              • how to add additional options from the controller by item id in magento 2

                – Prathap Gunasekaran
                Oct 30 '18 at 15:26















              0














              Step 1: Create registration.php and module.xml file the for custom module.



              Step 2: Assign Observers to Events




              /app/code/Namespace/Mymodule/etc/events.xml




               <?xml version="1.0"?>
              <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
              <event name="catalog_product_load_after">
              <observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
              </event>
              </config>


              Step 3: Create the Observers




              app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php




              <?php
              namespace NamespaceMymoduleObserver;

              use MagentoFrameworkEventObserverInterface;
              use MagentoFrameworkAppRequestInterface;

              class SetAdditionalOptions implements ObserverInterface

              /**
              * @var RequestInterface
              */
              protected $_request;

              /**
              * @param RequestInterface $request
              */
              public function __construct(
              RequestInterface $request
              )
              $this->_request = $request;


              /**
              * @param MagentoFrameworkEventObserver $observer
              */
              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
              $product = $observer->getProduct();
              $additionalOptions = [];
              $additionalOptions[] = array(
              'label' => "Some Label",
              'value' => "Your Information",
              );
              $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));





              run below command after



              rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*

              php bin/magento setup:upgrade

              php bin/magento setup:di:compile

              php bin/magento cache:clean

              php bin/magento cache:flush

              php bin/magento indexer:reindex


              reference links



              https://www.cloudways.com/blog/add-additional-options-in-magento-2/



              https://webkul.com/blog/additional-options-cart-item-magento2/






              share|improve this answer

























              • Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

                – Samantha Rajasingha
                Jun 30 '17 at 12:12











              • @SamanthaRajasingha - Can you refer the above both links?

                – Jjo
                Jun 30 '17 at 12:14












              • I used those links already, I got the same issue from there.

                – Samantha Rajasingha
                Jun 30 '17 at 12:19











              • how to add additional options from the controller by item id in magento 2

                – Prathap Gunasekaran
                Oct 30 '18 at 15:26













              0












              0








              0







              Step 1: Create registration.php and module.xml file the for custom module.



              Step 2: Assign Observers to Events




              /app/code/Namespace/Mymodule/etc/events.xml




               <?xml version="1.0"?>
              <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
              <event name="catalog_product_load_after">
              <observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
              </event>
              </config>


              Step 3: Create the Observers




              app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php




              <?php
              namespace NamespaceMymoduleObserver;

              use MagentoFrameworkEventObserverInterface;
              use MagentoFrameworkAppRequestInterface;

              class SetAdditionalOptions implements ObserverInterface

              /**
              * @var RequestInterface
              */
              protected $_request;

              /**
              * @param RequestInterface $request
              */
              public function __construct(
              RequestInterface $request
              )
              $this->_request = $request;


              /**
              * @param MagentoFrameworkEventObserver $observer
              */
              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
              $product = $observer->getProduct();
              $additionalOptions = [];
              $additionalOptions[] = array(
              'label' => "Some Label",
              'value' => "Your Information",
              );
              $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));





              run below command after



              rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*

              php bin/magento setup:upgrade

              php bin/magento setup:di:compile

              php bin/magento cache:clean

              php bin/magento cache:flush

              php bin/magento indexer:reindex


              reference links



              https://www.cloudways.com/blog/add-additional-options-in-magento-2/



              https://webkul.com/blog/additional-options-cart-item-magento2/






              share|improve this answer















              Step 1: Create registration.php and module.xml file the for custom module.



              Step 2: Assign Observers to Events




              /app/code/Namespace/Mymodule/etc/events.xml




               <?xml version="1.0"?>
              <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
              <event name="catalog_product_load_after">
              <observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
              </event>
              </config>


              Step 3: Create the Observers




              app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php




              <?php
              namespace NamespaceMymoduleObserver;

              use MagentoFrameworkEventObserverInterface;
              use MagentoFrameworkAppRequestInterface;

              class SetAdditionalOptions implements ObserverInterface

              /**
              * @var RequestInterface
              */
              protected $_request;

              /**
              * @param RequestInterface $request
              */
              public function __construct(
              RequestInterface $request
              )
              $this->_request = $request;


              /**
              * @param MagentoFrameworkEventObserver $observer
              */
              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
              $product = $observer->getProduct();
              $additionalOptions = [];
              $additionalOptions[] = array(
              'label' => "Some Label",
              'value' => "Your Information",
              );
              $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));





              run below command after



              rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*

              php bin/magento setup:upgrade

              php bin/magento setup:di:compile

              php bin/magento cache:clean

              php bin/magento cache:flush

              php bin/magento indexer:reindex


              reference links



              https://www.cloudways.com/blog/add-additional-options-in-magento-2/



              https://webkul.com/blog/additional-options-cart-item-magento2/







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 2 '18 at 14:42









              Abhishek Panchal

              3,6203929




              3,6203929










              answered Jun 30 '17 at 11:06









              JjoJjo

              840214




              840214












              • Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

                – Samantha Rajasingha
                Jun 30 '17 at 12:12











              • @SamanthaRajasingha - Can you refer the above both links?

                – Jjo
                Jun 30 '17 at 12:14












              • I used those links already, I got the same issue from there.

                – Samantha Rajasingha
                Jun 30 '17 at 12:19











              • how to add additional options from the controller by item id in magento 2

                – Prathap Gunasekaran
                Oct 30 '18 at 15:26

















              • Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

                – Samantha Rajasingha
                Jun 30 '17 at 12:12











              • @SamanthaRajasingha - Can you refer the above both links?

                – Jjo
                Jun 30 '17 at 12:14












              • I used those links already, I got the same issue from there.

                – Samantha Rajasingha
                Jun 30 '17 at 12:19











              • how to add additional options from the controller by item id in magento 2

                – Prathap Gunasekaran
                Oct 30 '18 at 15:26
















              Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

              – Samantha Rajasingha
              Jun 30 '17 at 12:12





              Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.

              – Samantha Rajasingha
              Jun 30 '17 at 12:12













              @SamanthaRajasingha - Can you refer the above both links?

              – Jjo
              Jun 30 '17 at 12:14






              @SamanthaRajasingha - Can you refer the above both links?

              – Jjo
              Jun 30 '17 at 12:14














              I used those links already, I got the same issue from there.

              – Samantha Rajasingha
              Jun 30 '17 at 12:19





              I used those links already, I got the same issue from there.

              – Samantha Rajasingha
              Jun 30 '17 at 12:19













              how to add additional options from the controller by item id in magento 2

              – Prathap Gunasekaran
              Oct 30 '18 at 15:26





              how to add additional options from the controller by item id in magento 2

              – Prathap Gunasekaran
              Oct 30 '18 at 15:26













              0














              After add additional options to cart You need to create another observer for save additional attributes order item from quote item.



              Please add this event observer code in Module's etc/events.xml



              <event name="sales_model_service_quote_submit_before">
              <observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
              </event>


              Please create observer file in your module



              VendorModuleObserverSalesOrderItemAdditionalOptions.php


              And here is Observer code for save Additional Options to Order Item.



              <?php
              namespace VendorModuleObserverSales;
              use MagentoFrameworkEventObserverInterface;
              use MagentoFrameworkAppObjectManager;
              use MagentoFrameworkSerializeSerializerJson;
              class OrderItemAdditionalOptions implements ObserverInterface

              public function __construct(
              Json $serializer = null
              )
              $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);

              /**
              * @param MagentoFrameworkEventObserver $observer
              */
              public function execute(MagentoFrameworkEventObserver $observer)

              try
              $quote = $observer->getQuote();
              $order = $observer->getOrder();
              $quoteItems = [];

              // Map Quote Item with Quote Item Id
              foreach ($quote->getAllVisibleItems() as $quoteItem)
              $quoteItems[$quoteItem->getId()] = $quoteItem;


              foreach ($order->getAllVisibleItems() as $orderItem)
              $quoteItemId = $orderItem->getQuoteItemId();
              $quoteItem = $quoteItems[$quoteItemId];
              $additionalOptions = $quoteItem->getOptionByCode('additional_options');


              if (count($additionalOptions) > 0)
              // Get Order Item's other options
              $options = $orderItem->getProductOptions();
              // Set additional options to Order Item
              $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
              $orderItem->setProductOptions($options);


              catch (Exception $e)
              // catch error if any





              Please check this I think it will help you.






              share|improve this answer



























                0














                After add additional options to cart You need to create another observer for save additional attributes order item from quote item.



                Please add this event observer code in Module's etc/events.xml



                <event name="sales_model_service_quote_submit_before">
                <observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
                </event>


                Please create observer file in your module



                VendorModuleObserverSalesOrderItemAdditionalOptions.php


                And here is Observer code for save Additional Options to Order Item.



                <?php
                namespace VendorModuleObserverSales;
                use MagentoFrameworkEventObserverInterface;
                use MagentoFrameworkAppObjectManager;
                use MagentoFrameworkSerializeSerializerJson;
                class OrderItemAdditionalOptions implements ObserverInterface

                public function __construct(
                Json $serializer = null
                )
                $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);

                /**
                * @param MagentoFrameworkEventObserver $observer
                */
                public function execute(MagentoFrameworkEventObserver $observer)

                try
                $quote = $observer->getQuote();
                $order = $observer->getOrder();
                $quoteItems = [];

                // Map Quote Item with Quote Item Id
                foreach ($quote->getAllVisibleItems() as $quoteItem)
                $quoteItems[$quoteItem->getId()] = $quoteItem;


                foreach ($order->getAllVisibleItems() as $orderItem)
                $quoteItemId = $orderItem->getQuoteItemId();
                $quoteItem = $quoteItems[$quoteItemId];
                $additionalOptions = $quoteItem->getOptionByCode('additional_options');


                if (count($additionalOptions) > 0)
                // Get Order Item's other options
                $options = $orderItem->getProductOptions();
                // Set additional options to Order Item
                $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
                $orderItem->setProductOptions($options);


                catch (Exception $e)
                // catch error if any





                Please check this I think it will help you.






                share|improve this answer

























                  0












                  0








                  0







                  After add additional options to cart You need to create another observer for save additional attributes order item from quote item.



                  Please add this event observer code in Module's etc/events.xml



                  <event name="sales_model_service_quote_submit_before">
                  <observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
                  </event>


                  Please create observer file in your module



                  VendorModuleObserverSalesOrderItemAdditionalOptions.php


                  And here is Observer code for save Additional Options to Order Item.



                  <?php
                  namespace VendorModuleObserverSales;
                  use MagentoFrameworkEventObserverInterface;
                  use MagentoFrameworkAppObjectManager;
                  use MagentoFrameworkSerializeSerializerJson;
                  class OrderItemAdditionalOptions implements ObserverInterface

                  public function __construct(
                  Json $serializer = null
                  )
                  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);

                  /**
                  * @param MagentoFrameworkEventObserver $observer
                  */
                  public function execute(MagentoFrameworkEventObserver $observer)

                  try
                  $quote = $observer->getQuote();
                  $order = $observer->getOrder();
                  $quoteItems = [];

                  // Map Quote Item with Quote Item Id
                  foreach ($quote->getAllVisibleItems() as $quoteItem)
                  $quoteItems[$quoteItem->getId()] = $quoteItem;


                  foreach ($order->getAllVisibleItems() as $orderItem)
                  $quoteItemId = $orderItem->getQuoteItemId();
                  $quoteItem = $quoteItems[$quoteItemId];
                  $additionalOptions = $quoteItem->getOptionByCode('additional_options');


                  if (count($additionalOptions) > 0)
                  // Get Order Item's other options
                  $options = $orderItem->getProductOptions();
                  // Set additional options to Order Item
                  $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
                  $orderItem->setProductOptions($options);


                  catch (Exception $e)
                  // catch error if any





                  Please check this I think it will help you.






                  share|improve this answer













                  After add additional options to cart You need to create another observer for save additional attributes order item from quote item.



                  Please add this event observer code in Module's etc/events.xml



                  <event name="sales_model_service_quote_submit_before">
                  <observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
                  </event>


                  Please create observer file in your module



                  VendorModuleObserverSalesOrderItemAdditionalOptions.php


                  And here is Observer code for save Additional Options to Order Item.



                  <?php
                  namespace VendorModuleObserverSales;
                  use MagentoFrameworkEventObserverInterface;
                  use MagentoFrameworkAppObjectManager;
                  use MagentoFrameworkSerializeSerializerJson;
                  class OrderItemAdditionalOptions implements ObserverInterface

                  public function __construct(
                  Json $serializer = null
                  )
                  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);

                  /**
                  * @param MagentoFrameworkEventObserver $observer
                  */
                  public function execute(MagentoFrameworkEventObserver $observer)

                  try
                  $quote = $observer->getQuote();
                  $order = $observer->getOrder();
                  $quoteItems = [];

                  // Map Quote Item with Quote Item Id
                  foreach ($quote->getAllVisibleItems() as $quoteItem)
                  $quoteItems[$quoteItem->getId()] = $quoteItem;


                  foreach ($order->getAllVisibleItems() as $orderItem)
                  $quoteItemId = $orderItem->getQuoteItemId();
                  $quoteItem = $quoteItems[$quoteItemId];
                  $additionalOptions = $quoteItem->getOptionByCode('additional_options');


                  if (count($additionalOptions) > 0)
                  // Get Order Item's other options
                  $options = $orderItem->getProductOptions();
                  // Set additional options to Order Item
                  $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
                  $orderItem->setProductOptions($options);


                  catch (Exception $e)
                  // catch error if any





                  Please check this I think it will help you.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 8 '18 at 10:45









                  Kishor ThummarKishor Thummar

                  1156




                  1156





















                      0














                      after $item->addOption(...) try calling $item->saveItemOptions().

                      Also a side note, don't add the options as serialized using serialize($additionalOptions), use json_encode or better yet the method MagentoFrameworkSerializeSerializerJson::serialize.






                      share|improve this answer



























                        0














                        after $item->addOption(...) try calling $item->saveItemOptions().

                        Also a side note, don't add the options as serialized using serialize($additionalOptions), use json_encode or better yet the method MagentoFrameworkSerializeSerializerJson::serialize.






                        share|improve this answer

























                          0












                          0








                          0







                          after $item->addOption(...) try calling $item->saveItemOptions().

                          Also a side note, don't add the options as serialized using serialize($additionalOptions), use json_encode or better yet the method MagentoFrameworkSerializeSerializerJson::serialize.






                          share|improve this answer













                          after $item->addOption(...) try calling $item->saveItemOptions().

                          Also a side note, don't add the options as serialized using serialize($additionalOptions), use json_encode or better yet the method MagentoFrameworkSerializeSerializerJson::serialize.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 17 '18 at 8:35









                          MariusMarius

                          168k28324693




                          168k28324693



























                              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%2f181480%2fhow-to-add-additional-options-in-magento-2-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

                              Sum ergo cogito? 1 nng

                              419 nièngy_Soadمي 19bal1.5o_g

                              Queiggey Chernihivv 9NnOo i Zw X QqKk LpB