0

I have products with custom options. One of these products options must be updated just after product has been added to cart.

I then created an event launched just after checkout_cart_add_product_complete.

My observer is well called :

public function execute(\Magento\Framework\Event\Observer $observer) {

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
    $items = $cart->getQuote()->getAllItems();

    foreach ($items as $item)
    {
        $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
        $customOptions = $options['options'];
        if (!empty($customOptions)) 
        {
            foreach ($customOptions as $key=>$option) 
            {
                if ($option['label']=="foo") 
                {
                    $customOptions[$key]['value']="bar";
                }
            }

            $item->setProductOptions($customOptions);
            $item->save();
        }
    }
}

But when Cart is displayed, the product option has not been changed.

Thank you for your help,

1
  • nobody as an idea?
    – Alexglvr
    Jul 26, 2018 at 7:12

1 Answer 1

0
        public function __construct(
                ...
                \Magento\Checkout\Model\Cart $cart
                ) 
        {
            ...
            $this->_cart=$cart;
        }

    public function execute(\Magento\Framework\Event\Observer $observer) {

        $items = $this->_cart->getquote()->getAllItems();

        foreach ($items as $item)
        {
            $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
            $customOptions = $options['options'];
            if (!empty($customOptions)) 
            {
                foreach ($customOptions as $key=>$option) 
                {
                    if ($option['label']=="foo") 
                    {
                        $OptionID=$option['option_id'];
                        $this->_cart->getquote()->getItemById($item->getItemId())->getOptionByCode('option_'.$OptionID)->setValue("bar");
                        break;
                    }
                }
                $buyreqTab=$this->serializer->unserialize($this->_cart->getquote()->getItemById($item->getItemId())->getOptionByCode('info_buyRequest')->getValue());
                $buyreqTab['options'][$OptionID]="bar";
                $this->_cart->getquote()->getItemById($item->getItemId())->getOptionByCode('info_buyRequest')->setValue($this->serializer->serialize($buyreqTab));
            }
            $this->_cart->save();

        }
    }

i tried it but it does not reflects in quote_item_options table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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