2

I am sending additional custom options in the cart when add to cart a product.

I have used this link . Using this link I have added custom options in the cart as well on checkout page and minicart. But custom options are not showing in admin.

I have tried link2, Link3, link4 but still getting error on checkout page: {"message":"An error occurred on the server. Please try to place the order again."}

How can I show additional custom options on the order page in admin?

0

I found the issue reason and fixed the error in Magento 2.3

I changed the below code from the original code.

if (count($additionalOptions) > 0) { to if ($additionalOptions) {

and unserialize to $this->serializer->unserialize

Please check my code carefully.

in events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_before">
        <observer name="unique_name" instance="Vendor\ModuleName\Observer\OrderItemAdditionalOptions" />
    </event>
</config>

in Observer Class.

<?php
namespace Vendor\ModuleName\Observer;
 
use Magento\Framework\Event\ObserverInterface;
 
class OrderItemAdditionalOptions implements ObserverInterface
{

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\LayoutInterface $layout,
        \Magento\Framework\App\RequestInterface $request,
        \Psr\Log\LoggerInterface $logger,
        Json $serializer = null
    )
    {
        $this->_layout = $layout;
        $this->_storeManager = $storeManager;
        $this->_request = $request;
        $this->logger = $logger;
        $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Magento\Framework\Serialize\Serializer\Json::class);
    }
    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $quote = $observer->getQuote();
            $order = $observer->getOrder();
            $quoteItems = [];

            // Map Quote Item with Quote Item Id
            foreach ($quote->getAllVisibleItems() as $quoteItem) {
                $quoteItems[$quoteItem->getId()] = $quoteItem;
            }

            foreach ($order->getAllVisibleItems() as $orderItem) {
                $quoteItemId = $orderItem->getQuoteItemId();
                $quoteItem = $quoteItems[$quoteItemId];
                $additionalOptions = $quoteItem->getOptionByCode('additional_options');

                
                if ($additionalOptions) {
                    // Get Order Item's other options
                    $options = $orderItem->getProductOptions();
                    // Set additional options to Order Item
                    $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
                    $orderItem->setProductOptions($options);
                }
            }
        } catch (\Exception $e) {
            // catch error if any
        }
    }
}

This code works fine in my project.

You can find the additional option result in sales_order_item table.

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.