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!