How to copy custom data from quote item to order item

The following steps show you how to copy custom data from quote item to order item:

  1. Createxml file in to etc folder.
  2. Describe a code, plug-in.
  3. Build a class to the module and describe then, I build the function.

In magento 2 ,you can ’t use field set 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 use plug-in to do it

  1. Create dt.xml file to etc folder of my module
app/code/Webnexs/TipAndTrick/etc”
  1. describe the code in to 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 a 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 outcome of the function “convert” in class

Magento\Quote\Model\Quote\Item\ToOrderItem

  • In the second line, set a value TipAndTrickCustomDatathis is the result 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 steps will be useful to you to copy the custom data from quote item to order item

Last Update: April 9, 2018  

February 5, 2018   243   Nandini R    Orders    
Total 1 Votes:
0

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
Google+
http://www.webnexs.com/blog/kb/copy-custom-data-quote-item-order-item
Twitter
LinkedIn