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