How to copy custom data from quote item to order item in Magento 2?

The accompanying advances tell you the best way to copy custom information from quote item to order item in Magento 2:

  1. Create an XML file into the etc folder.
  2. Describe a code, plug-in.
  3. Build a class to the module and represent then, I build the function.
Having trouble in your Magento store? Try upgrading it today!!!

How to copy custom data from quote item to order item in Magento 2?

In Magento 2, you can’t use the fieldset to copy custom data from quote item to order item, so you must do some other action to do it.

Now I will show you how to a used plug-in to do it

1. Create a dt.XML file to an etc folder of my module

app/code/Webnexs/TipAndTrick/etc”

2. Describe the code into this file, the following code:

<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">

        <plugin name="tipandtrick_to_order_item" type="Webnexs\TipAndTrick\Model\Plugin\Quote\TipAndTrickToOrderItem"/>

    </type>

3. In this code, I describe a plug-in

“Webnexs\TipAndTrick\Model\Plugin\Quote\TipAndTrickToOrderItem of class Magento\Quote\Model\Quote\Item\ToOrderItem

  1. Then, I built a class

Webnexs\TipAndTrick\Model\Plugin\Quote\TipAndTrickToOrderItem

In to my module and describe the function” around convert” which will copy custom data from quote item to order item.

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->setTipAndTrickCustomData($item->getTipAndTrickCustomData());

        return $orderItem;

    }
  • In the first line of the function $orderItem = $proceed($item, $additional);this is the output of the function “convert” in class

Magento\Quote\Model\Quote\Item\ToOrderItem

  • In the second line, set a value TipAndTrickCustomDatathis is the output of the function “convert” in class “Magento\Quote\Model\Quote\Item\ToOrderItem”.
  • Then return an object $orderItemwhich will restore the result of the function “convert” in class “Magento\Quote\Model\Quote\Item\ToOrderItem”.

I hope these above steps will be useful to you to copy the custom data from quote item to order item.

How to copy custom data from quote item to order item

Last Update: February 17, 2021  

February 5, 2018   2622   Nandini Ramachandran    Magento, Magento 2 Tutorial, Orders    
Total 3 Votes:
2

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn