0

I have add a custom column in a quote_item_option table, Now I want to update a record based on a quote item id and I use bellow code :

 public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\Session\SessionManagerInterface $session,
        \Magento\Quote\Model\Quote $quoteFactory,
        \Magento\Quote\Model\ResourceModel\Quote\Item\Option\CollectionFactory $optioncollection,
        \Magento\Quote\Model\QuoteRepository $quoteRepo,
        \Magento\Quote\Model\Quote\Item\Option $itemoption

    ) {
        $this->checkoutSession = $checkoutSession;
        $this->messageManager = $messageManager;
        $this->_session = $session;
        $this->_quoteFactory = $quoteFactory;
        $this->_optioncollection = $optioncollection;
        $this->_quoteRepo = $quoteRepo;
        $this->_itemoption = $itemoption;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $quote = $this->checkoutSession->loadCustomerQuote();
            $result = $quote->getData();            
            $quoteData = $this->_quoteFactory->load($result['quote_id_1']);
            $items = $quoteData->getAllItems();
            $optioncollection = $this->_optioncollection->create();
            foreach ($items as $value) {
                $optioncollection->addFieldToSelect('*')->addItemFilter($value['item_id'])->getData();
                if(!empty($optioncollection)){
                    foreach ($optioncollection as $v) {
                         $itemoption = $this->_itemoption->load($v['option_id']);
                         $itemoption->setCustomracking(json_encode($this->getValue()));
                         $itemoption->save();
                    }
                }
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('Load customer quote error'));
        }
    }

    public function getValue(){
        $this->session->start();
        return $this->session->getRackingData();
    }

    public function unSetValue(){
        $this->session->start();
        return $this->session->getRackingData();
    }

can any one help me with my code ?

where i am wrong ?

2
  • When are you updating this value? Jan 23, 2020 at 7:19
  • using this checkout_cart_save_after Jan 23, 2020 at 8:15

1 Answer 1

1

i got my solution as below changes in my code :

    try {
        $quote = $this->checkoutSession->loadCustomerQuote();
        $result = $quote->getData();            
        $quoteData = $this->_quoteFactory->load($result['quote_id_1']);
        $items = $quoteData->getAllItems();
        $optioncollection = $this->_optioncollection->create();
        foreach ($items as $value) {
            $optioncollection->addFieldToSelect('*')->addItemFilter($value['item_id'])->getData();
            if(!empty($optioncollection)){
                foreach ($optioncollection as $v) {
                     $itemoption = $this->_itemoption->load($v['option_id']);
                     $itemoption->setCustomracking(json_encode($this->getValue()));
                     $itemoption->save();
                }
            }

        }
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        $this->messageManager->addError($e->getMessage());
    } catch (\Exception $e) {
        $this->messageManager->addException($e, __($e->getMessage().'Load customer quote error'));
    }

Your Answer

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

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