I have product with a custom option.
On the event checkout_cart_product_add_after, I want to assign to this option some value programmatically. If the option has been set by the customer in product page, I manage to do it using :
$this->_cart->getquote()->getItemById($item->getItemId())->getOptionByCode('option_'.$OptionID)->setValue($foo)->save();
But in the case the option has not been set by customer in product page, the cart item does not have this option. I then need to create it, but I do not manage to do it.
NB: I can retreive the product options using :
$ProductId=$item->getProduct()->getId();
$Product=$this->_productRepository->getById($ProductId);
$Options=$Product->getOptions();
Thank you for your help