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: 11 Average: 4.6]
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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago