2

I can update existing cart values:

$item = $quote->getItemById($itemId);
$item->getOptionByCode('option_code')->setValue($value);
$item->saveItemOptions();

But this only works if the option already exists, e.g. an unchecked checkmark custom option will not be included in the item options when adding the product to cart. I tried adding the options with $item->addOption() but that doesn't work because the option is not included in the $item->getProduct()->getCustomOptions(), and trying to add the custom option there also modifies the actual product instead of just the cart item product.

Is there a way to add an existing custom option to an existing cart item without removing and re-adding the item?

2

Spent 2 days trying to figure this out and of course I find the answer an hour after asking a question:

        $buyRequest = $this->serializer->unserialize($item->getOptionByCode('info_buyRequest')->getValue());
        $buyRequest['options'][$code] = $value;
        $item->getOptionByCode('info_buyRequest')->setValue($this->serializer->serialize($buyRequest));
        $item->saveItemOptions();
        $quote->updateItem($itemId, $buyRequest);
        $this->quoteRepository->save($quote);

Using the $quote->updateItem() method with an updated info_buyRequest pushes the update to the cart product as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.