How to Convert Custom Field From Quote Item to Order Item in Magento 2
Hello Magento peeps! 💻
I am back with another technical solution to help you convert custom field from quote item to order item in Magento 2.
The quote item table in Magento 2 represents the shopping cart and stores all the information about the product, quantity, prices, etc. On the successful placement of the order, all the fields from the quote item are converted to the order item table by default in Magento 2.
However, the custom fields in the quote item are not passed to the order object. This blog post is the solution to convert a custom field in Magento 2 quote item to order item. This is helpful when you have created custom columns in the quote item table and want to convert them into order items in Magento 2.
Let’s see how you can convert custom fields in the quote to order programmatically in Magento 2.
Steps to Programmatically Convert Custom Field From Quote Item to Order Item in Magento 2
In case you have a custom field in the quote item table and want to pass that to the order item table in Magento 2, you can follow the simple and easy steps mentioned below.
Step 1: Create a di.xml file at the app/code/Vendor/Extension/etc/ directory and add the following code:
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Quote\Model\Quote\Item\ToOrderItem"> <plugin name="quote_to_order_item_conv" type="Vendor\Extension\Plugin\Quote\ConvertToOrderItem"/> </type> </config> |
Step 2: Create ConvertToOrderItem.php file at the app/code/Vendor/Extension/Plugin/Quote directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace Vendor\Extension\Plugin\Quote; class ConvertToOrderItem { public function aroundConvert( \Magento\Quote\Model\Quote\Item\ToOrderItem $subject, \Closure $proceed, \Magento\Quote\Model\Quote\Item\AbstractItem $item, $additional = [] ) { $orderItem = $proceed($item, $additional); $orderItem->setCustomField($item->getCustomField()); return $orderItem; } } |
That’s it!
This is how easily you can pass the custom field from the quote item to the order item table in Magento 2. 😃
Have any doubts? Feel free to comment. I will be happy to help you. 😇
Also, do not forget to share this solution with your developer friends via social media.
Thanks for reading. 🍀
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
2 Comments
thank a lot
Hello,
We’re glad that the above solution helpful is for you
Thank you