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

Learn how you can add and copy the custom data from the quote item in Magento 2 platform with just a few items.

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.
Build your Amazon-like multi-vendor marketplace website @ $249/- now!!!
           Get your free live demo now: Click here

How to copy custom data from the quote 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 the module to set up custom data

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 in Magento 2.

Contact us if you want to develop a Magento store with its latest platform version.

Custom data in Magento 2


Posted

in

, ,

by

Comments

Leave a Reply

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