Magento – Magento 2 Update product additional option added in add to cart

magento-2.1magento2PHP

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(\Magento\Framework\Event\Observer $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();
                    }

Best Answer

First of all, let's check the \Magento\Quote\Model\Quote\Item::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.

Related Topic