5

I am successfully save custom data in cart using checkout_cart_product_add_after event in "quote_item_option" table Now i want to save this data in order and display in order section in admin panel.

enter image description here

  • Share your code which you have use for save data – Rakesh Varma Feb 23 '19 at 6:21
0

Use below code to save additional option to order item.

in etc/events.xml add following code.

<event name="sales_model_service_quote_submit_before"> <observer name="vendor_module_sales_model_service_quote_submit_before" instance="Vendor\ModuleName\Observer\SalesModelServiceQuoteSubmitBeforeObserver" /> </event>

and in SalesModelServiceQuoteSubmitBeforeObserver.php file add below code.

public function execute(EventObserver $observer)
    {
        $this->quote = $observer->getQuote();
        $this->order = $observer->getOrder();
        foreach($this->order->getItems() as $orderItem)
        {
            if(!$orderItem->getParentItemId() && $orderItem->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
            {

                if($quoteItem = $this->getQuoteItemById($orderItem->getQuoteItemId())){
                    if ($additionalOptionsQuote = $quoteItem->getOptionByCode('additional_options'))
                    {
                        if($additionalOptionsOrder = $orderItem->getProductOptionByCode('additional_options'))
                        {
                            $additionalOptions = array_merge($additionalOptionsQuote, $additionalOptionsOrder);
                        }
                        else
                        {
                            $additionalOptions = $additionalOptionsQuote;
                        }
                        if(count($additionalOptions) > 0)
                        {
                            $options = $orderItem->getProductOptions();
                            $options['additional_options'] = $this->serialize->unserialize($additionalOptions->getValue());
                            $orderItem->setProductOptions($options);
                        }

                    }
                }
            }
        }
    }
    private function getQuoteItemById($id)
    {
        if(empty($this->quoteItems))
        {
            foreach($this->quote->getItems() as $item)
            {
                if(!$item->getParentItemId() && $item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
                {
                    $this->quoteItems[$item->getId()] = $item;
                }
            }
        }
        if(array_key_exists($id, $this->quoteItems))
        {
            return $this->quoteItems[$id];
        }
        return null;
    }

for more details, you can check these links

https://magecomp.com/blog/add-additional-options-magento-2/

https://webkul.com/blog/additional-options-order-item-magento2/

  • 1
    I will try and let you know...! – Sanjay Gohil Feb 23 '19 at 6:45

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.