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

  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  3. <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
  4. <plugin name="TonyHelloworld_to_order_item" type="Tony\Helloworld\Model\Plugin\Quote\TonyHelloworldToOrderItem"/>
  5. </type>
  6. </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

  1. <?php
  2. /**
  3. * Tony
  4. *
  5. * Author : Tony Liu
  6. * Blog: https://www.tonyblog.cn
  7. *
  8. * DISCLAIMER
  9. *
  10. * Do not edit or add to this file if you wish to upgrade this extension to newer
  11. * version in the future.
  12. *
  13. * @package Tony_Helloworld
  14. */
  15. namespace Tony\Helloworld\Model\Plugin\Quote;
  16. class TonyHelloworldToOrderItem
  17. {
  18. public function aroundConvert(
  19. \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
  20. \Closure $proceed,
  21. \Magento\Quote\Model\Quote\Item\AbstractItem $item,
  22. $additional = []
  23. ) {
  24. /** @var $orderItem Item */
  25. $orderItem = $proceed($item, $additional);
  26. $orderItem->setNotes($item->getNotes());
  27. return $orderItem;
  28. }
  29. }

Finally, the result of fuction as follow :

1. Quote notes :
Tony's blog image

2. Quote to order
Tony's blog image

3. After create an order
Tony's blog image

If you want to know more information, please contact me.