How To

How to Convert the Custom Field from Quote item to Order item in Magento

In an Ecommerce store, every product has different shipping costs as well as other additional costs charged by your store owner. So, if the customer adds a product in his/her shopping cart the whole cart summary can be view in the shopping cart as well as while checkout. Also, Magento stores every cart is known as a persistent cart in the backend with all added products bound in the one single quote item field. But if such cart is restored, it will only restore selected products, not its extra charges or shipping. So, every time your customers need to reselect every option when they came back for checkout.
So to resolve such problem you need to create custom additional quote field in the backend which holds such values and reapplied charges on cart restoration. To do the same, simply follow below two steps to convert a custom field from quote item to order item in Magento
First, create “di.xml” file at the following path.
app\code \Vendor \Extension\etc\

<pre class="lang:default decode:true">
<?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_item_to_order_item"    type="Vendor\Extension\Plugin\Quote\Convertquoteitemtoorder"/>
</type>
 </config>
</pre>

Now, we need to create one more file “Convertquoteitemtoorder.php” to convert quote item at this path.
app\code\Vendor\Extension\Plugin\Quote\

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Plugin\Quote;
class Convertquoteitemtoorder{
 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->setCustomField1($item->getCustomField1());
        $orderItem->setCustomField2($item->getCustomField2());
        $orderItem->setCustomField3($item->getCustomField3());
     return $orderItem;
 }
}
</pre>

That’s it, by following those 2 simple steps you can transfer all the Additional custom fields in the Quote field to the customer’s final order list. You are free add manipulate above code.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.

Happy Coding!

Click to rate this post!
[Total: 12 Average: 4.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago