How to Convert Custom Field From Quote Item to Order Item in Magento 2
- Step 1: Add a di.xml file
- Step 2: Identify a code, a plugin
- Step 3: Add a class to your module, then identify a function
Step 1: Add a di.xml file
In the module app/code/Tony/HelloWorld/etc, please add the di.xml file.
Step 2: Identify a code, a plugin
<?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="TonyHelloworld_to_order_item" type="Tony\Helloworld\Model\Plugin\Quote\TonyHelloworldToOrderItem"/>
</type>
</config>
Then, identify a plugin Tony\HelloWorld\Model\Plugin\Quote\TonyHelloworldToOrderItem of class Magento\Quote\Model\Quote\Item\ToOrderItem
Step 3: Add a class to your module, then identify a function
<?php
/**
* Tony
*
* Author : Tony Liu
* Blog: https://www.tonyblog.cn
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @package Tony_Helloworld
*/
namespace Tony\Helloworld\Model\Plugin\Quote;
class TonyHelloworldToOrderItem
{
public function aroundConvert(
\Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
\Closure $proceed,
\Magento\Quote\Model\Quote\Item\AbstractItem $item,
$additional = []
) {
/** @var $orderItem Item */
$orderItem = $proceed($item, $additional);
$orderItem->setNotes($item->getNotes());
return $orderItem;
}
}
Finally, the result of fuction as follow :
If you want to know more information, please contact me.