Hello Magento Folks,
Hope you are all doing well, I am back with another exciting article on Magento 2 How to Series. Last time we learned about Magento 2: How to Add New Column In The Customer Grid. So today, We will learn how to display programmatically created custom options in admin order details Magento 2.
Kindly, check this article for How to add Custom Options to the Product Programmatically in Magento 2. In case your thinking to learn about how to display programmatically created custom options in admin order details in Magento 2. Then this is the right spot for you to learn that. In Magento 2, the custom option feature is an inherent feature available; this option will grant permission to customers to customize the product according to their need within the given different custom options. If you want to display programmatically created custom options in admin order details programmatically, you have to follow the instructions provided below.
Step 1: Create VENDOR\EXTENSION\etc\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="quote_to_order" instance="VENDOR\EXTENSION\Observer\OrderItemAdditionalOptions" /> </event> </config>
Step 2: Create an observer for it.
Create VENDOR\EXTENSION\Observer\OrderItemAdditionalOptions.php
<?php namespace VENDOR\EXTENSION\Observer; use Magento\Framework\Event\ObserverInterface; use \Magento\Framework\Unserialize\Unserialize; use \Magento\Framework\Serialize\Serializer\Json; class OrderItemAdditionalOptions implements ObserverInterface { /** * @param \Magento\Framework\Event\Observer $observer */ protected $Unserialize; protected $Serializer; public function __construct( Unserialize $Unserialize, Json $Json ){ $this->Unserialize = $Unserialize; $this->Serializer = $Json; } 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; //130 } foreach ($order->getAllVisibleItems() as $orderItem) { $quoteItemId = $orderItem->getQuoteItemId(); $quoteItem = $quoteItems[$quoteItemId]; $additionalOptions = $quoteItem->getOptionByCode('additional_options'); // Get Order Item's other options $options = $orderItem->getProductOptions(); // Set additional options to Order Item if($this->isSerialized($additionalOptions->getValue())){ $options['options'] = $this->Unserialize->unserialize($additionalOptions->getValue()); }else{ $options['options'] = $this->Serializer->unserialize($additionalOptions->getValue()); } $orderItem->setProductOptions($options); } } catch (\Exception $e) { // catch error if any $e->getMessage(); } } private function isSerialized($value) { return (boolean) preg_match('/^((s|i|d|b|a|O|C):|N;)/', $value); } } ?>
That’s it for today! Now, you can easily display programmatically created custom options in admin order details by just following the above given easy steps.
Last but not least, if you found the blog interesting, don’t forget to share with your Magento pals. Feel free to Contact Us if you find any difficulty in applying the above steps.
Happy Coding!?
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
View Comments
I used this code. It is working fine for adding string custom option, but getting error as "Type Error occurred when creating object: Magento\Framework\Filter\TruncateFilter\Result, Argument 1 passed to Magento\Framework\Filter\TruncateFilter\Result::__construct() must be of the type string, array given" to adding images (type: image)
Where you are facing this issue?
For that you can contact us on support@magecomp.com
i used this code, but it not proper working for configurable product.i am placing two product.it display custom option for first product not showing for second product.
For that you can contact us on support@magecomp.com
How to get the additional options value for one attribute in last ordered items
You need to fetch that additional_options data by loading the order by order Id or something.