Add option to product in the cart progrmatically in magento 2Magento model extension experiment, return: “class does not exist”Set custom price of product when adding to cart code not workingMagento 2.1 Create a filter in the product grid by new attributeMagento 2: Add a product to the cart programmaticallyAdd to cart button freezes and nothing is put in basketMagento 2: After custom cookie is created all pages default to home pageMagento 2: I Want to add multiple product using checkboxI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom OptionsHow to update product's attribute value in Magento 2?

Why is the Searing Smite spell not listed in the Roll20 spell list?

Applying for mortgage when living together but only one will be on the mortgage

Why would an invisible personal shield be necessary?

NULL value causes blank row in SELECT results for text concatenation

Should students have access to past exams or an exam bank?

Why are prop blades not shaped like household fan blades?

My employer is refusing to give me the pay that was advertised after an internal job move

What to expect in a jazz audition

Adding a (stair/baby) gate without facing walls

Should I put my name first or last in the team members list?

Russian pronunciation of /etc (a directory)

"Fewer errors means better products" or fewer errors mean better products."

Music Theory: Facts or Hierarchy of Opinions?

Coworker mumbles to herself when working, how to ask her to stop?

Introduction to the Sicilian

What kind of horizontal stabilizer does a Boeing 737 have?

Patio gate not at right angle to the house

Why don't short runways use ramps for takeoff?

Prepare a user to perform an action before proceeding to the next step

Why are we moving in circles with a tandem kayak?

What does 「ちんちんかいかい」 mean?

Was Donald Trump at ground zero helping out on 9-11?

How did Biff return to 2015 from 1955 without a lightning strike?

GDPR Compliance - notification of data breach



Add option to product in the cart progrmatically in magento 2


Magento model extension experiment, return: “class does not exist”Set custom price of product when adding to cart code not workingMagento 2.1 Create a filter in the product grid by new attributeMagento 2: Add a product to the cart programmaticallyAdd to cart button freezes and nothing is put in basketMagento 2: After custom cookie is created all pages default to home pageMagento 2: I Want to add multiple product using checkboxI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom OptionsHow to update product's attribute value in Magento 2?






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








0















<?php
namespace NeologixBopisObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoCheckoutModelSession as CheckoutSession;

class SetAdditionalOptions implements ObserverInterface


protected $_request;

public function __construct(
RequestInterface $request,CheckoutSession $checkoutSession
)
$this->_request = $request;
$this->checkoutSession = $checkoutSession;

public function execute(MagentoFrameworkEventObserver $observer)

$infoDataObject = $observer->getEvent()->getInfo()->toArray();
$cart = $observer->getEvent()->getCart();

foreach ($infoDataObject as $itemId => $itemInfo)

$item = $cart->getQuote()->getItemById($itemId);
$product = $item->getProduct();
**// able to load product but after this code is not working for adding options to the product**
$additionalOptions = array(
array(
'code' => 'my_code',
'label' => 'This text is displayed through additional options2',
'value' => 'ID is '
)
);
$product->addOption(
array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
)
);



?>









share|improve this question


























  • so what issue you are facing ?? can you describe ?

    – Murtuza Zabuawala
    Aug 1 '17 at 10:49











  • When the user is on cart page, I am adding custom options to the product on the click of update cart link but not able to add the option to the product with the above code. I am able to the product

    – anantchopra2007
    Aug 2 '17 at 7:32











  • can you give me event name ??

    – Murtuza Zabuawala
    Aug 2 '17 at 7:34











  • Yes , Event name is :- checkout_cart_update_items_after

    – anantchopra2007
    Aug 2 '17 at 7:37











  • I am able to load product on the click of update cart button with the help of the above observer code. But not able to set or add custom options to the product programatically here.

    – anantchopra2007
    Aug 2 '17 at 7:38


















0















<?php
namespace NeologixBopisObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoCheckoutModelSession as CheckoutSession;

class SetAdditionalOptions implements ObserverInterface


protected $_request;

public function __construct(
RequestInterface $request,CheckoutSession $checkoutSession
)
$this->_request = $request;
$this->checkoutSession = $checkoutSession;

public function execute(MagentoFrameworkEventObserver $observer)

$infoDataObject = $observer->getEvent()->getInfo()->toArray();
$cart = $observer->getEvent()->getCart();

foreach ($infoDataObject as $itemId => $itemInfo)

$item = $cart->getQuote()->getItemById($itemId);
$product = $item->getProduct();
**// able to load product but after this code is not working for adding options to the product**
$additionalOptions = array(
array(
'code' => 'my_code',
'label' => 'This text is displayed through additional options2',
'value' => 'ID is '
)
);
$product->addOption(
array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
)
);



?>









share|improve this question


























  • so what issue you are facing ?? can you describe ?

    – Murtuza Zabuawala
    Aug 1 '17 at 10:49











  • When the user is on cart page, I am adding custom options to the product on the click of update cart link but not able to add the option to the product with the above code. I am able to the product

    – anantchopra2007
    Aug 2 '17 at 7:32











  • can you give me event name ??

    – Murtuza Zabuawala
    Aug 2 '17 at 7:34











  • Yes , Event name is :- checkout_cart_update_items_after

    – anantchopra2007
    Aug 2 '17 at 7:37











  • I am able to load product on the click of update cart button with the help of the above observer code. But not able to set or add custom options to the product programatically here.

    – anantchopra2007
    Aug 2 '17 at 7:38














0












0








0








<?php
namespace NeologixBopisObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoCheckoutModelSession as CheckoutSession;

class SetAdditionalOptions implements ObserverInterface


protected $_request;

public function __construct(
RequestInterface $request,CheckoutSession $checkoutSession
)
$this->_request = $request;
$this->checkoutSession = $checkoutSession;

public function execute(MagentoFrameworkEventObserver $observer)

$infoDataObject = $observer->getEvent()->getInfo()->toArray();
$cart = $observer->getEvent()->getCart();

foreach ($infoDataObject as $itemId => $itemInfo)

$item = $cart->getQuote()->getItemById($itemId);
$product = $item->getProduct();
**// able to load product but after this code is not working for adding options to the product**
$additionalOptions = array(
array(
'code' => 'my_code',
'label' => 'This text is displayed through additional options2',
'value' => 'ID is '
)
);
$product->addOption(
array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
)
);



?>









share|improve this question
















<?php
namespace NeologixBopisObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoCheckoutModelSession as CheckoutSession;

class SetAdditionalOptions implements ObserverInterface


protected $_request;

public function __construct(
RequestInterface $request,CheckoutSession $checkoutSession
)
$this->_request = $request;
$this->checkoutSession = $checkoutSession;

public function execute(MagentoFrameworkEventObserver $observer)

$infoDataObject = $observer->getEvent()->getInfo()->toArray();
$cart = $observer->getEvent()->getCart();

foreach ($infoDataObject as $itemId => $itemInfo)

$item = $cart->getQuote()->getItemById($itemId);
$product = $item->getProduct();
**// able to load product but after this code is not working for adding options to the product**
$additionalOptions = array(
array(
'code' => 'my_code',
'label' => 'This text is displayed through additional options2',
'value' => 'ID is '
)
);
$product->addOption(
array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
)
);



?>






magento2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 '17 at 10:46









Raghu

1,2731 gold badge5 silver badges22 bronze badges




1,2731 gold badge5 silver badges22 bronze badges










asked Aug 1 '17 at 10:31









anantchopra2007anantchopra2007

186 bronze badges




186 bronze badges















  • so what issue you are facing ?? can you describe ?

    – Murtuza Zabuawala
    Aug 1 '17 at 10:49











  • When the user is on cart page, I am adding custom options to the product on the click of update cart link but not able to add the option to the product with the above code. I am able to the product

    – anantchopra2007
    Aug 2 '17 at 7:32











  • can you give me event name ??

    – Murtuza Zabuawala
    Aug 2 '17 at 7:34











  • Yes , Event name is :- checkout_cart_update_items_after

    – anantchopra2007
    Aug 2 '17 at 7:37











  • I am able to load product on the click of update cart button with the help of the above observer code. But not able to set or add custom options to the product programatically here.

    – anantchopra2007
    Aug 2 '17 at 7:38


















  • so what issue you are facing ?? can you describe ?

    – Murtuza Zabuawala
    Aug 1 '17 at 10:49











  • When the user is on cart page, I am adding custom options to the product on the click of update cart link but not able to add the option to the product with the above code. I am able to the product

    – anantchopra2007
    Aug 2 '17 at 7:32











  • can you give me event name ??

    – Murtuza Zabuawala
    Aug 2 '17 at 7:34











  • Yes , Event name is :- checkout_cart_update_items_after

    – anantchopra2007
    Aug 2 '17 at 7:37











  • I am able to load product on the click of update cart button with the help of the above observer code. But not able to set or add custom options to the product programatically here.

    – anantchopra2007
    Aug 2 '17 at 7:38

















so what issue you are facing ?? can you describe ?

– Murtuza Zabuawala
Aug 1 '17 at 10:49





so what issue you are facing ?? can you describe ?

– Murtuza Zabuawala
Aug 1 '17 at 10:49













When the user is on cart page, I am adding custom options to the product on the click of update cart link but not able to add the option to the product with the above code. I am able to the product

– anantchopra2007
Aug 2 '17 at 7:32





When the user is on cart page, I am adding custom options to the product on the click of update cart link but not able to add the option to the product with the above code. I am able to the product

– anantchopra2007
Aug 2 '17 at 7:32













can you give me event name ??

– Murtuza Zabuawala
Aug 2 '17 at 7:34





can you give me event name ??

– Murtuza Zabuawala
Aug 2 '17 at 7:34













Yes , Event name is :- checkout_cart_update_items_after

– anantchopra2007
Aug 2 '17 at 7:37





Yes , Event name is :- checkout_cart_update_items_after

– anantchopra2007
Aug 2 '17 at 7:37













I am able to load product on the click of update cart button with the help of the above observer code. But not able to set or add custom options to the product programatically here.

– anantchopra2007
Aug 2 '17 at 7:38






I am able to load product on the click of update cart button with the help of the above observer code. But not able to set or add custom options to the product programatically here.

– anantchopra2007
Aug 2 '17 at 7:38











2 Answers
2






active

oldest

votes


















0














try below event for your task



checkout_cart_update_items_before


because right now when you adding product option but quote is already updated so if you are setting up the custom option they are not getting saved



So use before event instead of after



you can see more info in this class



https://github.com/magento/magento2/blob/2.0.13/app/code/Magento/Checkout/Model/Cart.php






share|improve this answer

























  • This is not working.

    – anantchopra2007
    Aug 2 '17 at 7:59


















0














Try his code. It will work



 $additionalOptions[] = [
'label' => 'This text is displayed through additional options2',
'value' => 'ID is '
];

if(count($additionalOptions) > 0)
$item->addOption(array(
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));






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%2f187352%2fadd-option-to-product-in-the-cart-progrmatically-in-magento-2%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    try below event for your task



    checkout_cart_update_items_before


    because right now when you adding product option but quote is already updated so if you are setting up the custom option they are not getting saved



    So use before event instead of after



    you can see more info in this class



    https://github.com/magento/magento2/blob/2.0.13/app/code/Magento/Checkout/Model/Cart.php






    share|improve this answer

























    • This is not working.

      – anantchopra2007
      Aug 2 '17 at 7:59















    0














    try below event for your task



    checkout_cart_update_items_before


    because right now when you adding product option but quote is already updated so if you are setting up the custom option they are not getting saved



    So use before event instead of after



    you can see more info in this class



    https://github.com/magento/magento2/blob/2.0.13/app/code/Magento/Checkout/Model/Cart.php






    share|improve this answer

























    • This is not working.

      – anantchopra2007
      Aug 2 '17 at 7:59













    0












    0








    0







    try below event for your task



    checkout_cart_update_items_before


    because right now when you adding product option but quote is already updated so if you are setting up the custom option they are not getting saved



    So use before event instead of after



    you can see more info in this class



    https://github.com/magento/magento2/blob/2.0.13/app/code/Magento/Checkout/Model/Cart.php






    share|improve this answer













    try below event for your task



    checkout_cart_update_items_before


    because right now when you adding product option but quote is already updated so if you are setting up the custom option they are not getting saved



    So use before event instead of after



    you can see more info in this class



    https://github.com/magento/magento2/blob/2.0.13/app/code/Magento/Checkout/Model/Cart.php







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 2 '17 at 7:46









    Murtuza ZabuawalaMurtuza Zabuawala

    12.6k7 gold badges33 silver badges63 bronze badges




    12.6k7 gold badges33 silver badges63 bronze badges















    • This is not working.

      – anantchopra2007
      Aug 2 '17 at 7:59

















    • This is not working.

      – anantchopra2007
      Aug 2 '17 at 7:59
















    This is not working.

    – anantchopra2007
    Aug 2 '17 at 7:59





    This is not working.

    – anantchopra2007
    Aug 2 '17 at 7:59













    0














    Try his code. It will work



     $additionalOptions[] = [
    'label' => 'This text is displayed through additional options2',
    'value' => 'ID is '
    ];

    if(count($additionalOptions) > 0)
    $item->addOption(array(
    'code' => 'additional_options',
    'value' => serialize($additionalOptions)
    ));






    share|improve this answer





























      0














      Try his code. It will work



       $additionalOptions[] = [
      'label' => 'This text is displayed through additional options2',
      'value' => 'ID is '
      ];

      if(count($additionalOptions) > 0)
      $item->addOption(array(
      'code' => 'additional_options',
      'value' => serialize($additionalOptions)
      ));






      share|improve this answer



























        0












        0








        0







        Try his code. It will work



         $additionalOptions[] = [
        'label' => 'This text is displayed through additional options2',
        'value' => 'ID is '
        ];

        if(count($additionalOptions) > 0)
        $item->addOption(array(
        'code' => 'additional_options',
        'value' => serialize($additionalOptions)
        ));






        share|improve this answer













        Try his code. It will work



         $additionalOptions[] = [
        'label' => 'This text is displayed through additional options2',
        'value' => 'ID is '
        ];

        if(count($additionalOptions) > 0)
        $item->addOption(array(
        'code' => 'additional_options',
        'value' => serialize($additionalOptions)
        ));







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 4 '17 at 23:57









        Gurdit SinghGurdit Singh

        11 bronze badge




        11 bronze badge






























            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%2f187352%2fadd-option-to-product-in-the-cart-progrmatically-in-magento-2%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

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

            Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

            Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?