Shopping cart

How to Convert Custom Field From Quote Item to Order Item in Magento 2

Today, we will learn about the way to convert custom field from quote item to order item in Magento 2, through the below instructions.

Overview of converting a custom feild from quote item to order item in Magento 2

Step 1: Add a di.xml file

In the module app/code/Mageplaza/HelloWorld/etc, please add the di.xml file.

Step 2: Identify a code, a plugin

In the di.xml file, let’s identify a code:

<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="HelloWorld_to_order_item" type="Mageplaza\HelloWorld\Model\Plugin\Quote\HelloWorldToOrderItem"/>
</type>

Then, identify a plugin Mageplaza\HelloWorld\Model\Plugin\Quote\HelloWorldToOrderItem” of class “Magento\Quote\Model\Quote\Item\ToOrderItem

Step 3: Add a class to your module, then identify a function

  • In the module, add the class Mageplaza\HelloWorld\Model\Plugin\Quote\HelloWorldToOrderItem, and identify the function aroundConvert which will convert the custom data from quote item to order item at the same time.
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->setHelloWorldCustomData($item->getHelloWorldCustomData());
        return $orderItem;
    }
  • You will achieve the result of function convert in class Magento\Quote\Model\Quote\Item\ToOrderITem from the first line of this function $orderItem = $proceed($item, $additional);.

  • Next, create the value of HelloWorldCustomData for the result of function convert class Magento\Quote\Model\Quote\Item\ToOrderItem.

  • Then, return an object $orderItem to recover result of the function convert in the class Magento\Quote\Model\Quote\Item\ToOrderItem.

That is all requirements every Magento 2 store owners should apply to copy custom data from quote item to order item





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
copy
custom-data
quote-item
order-item

Sam Thomas
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

Comments for How to Convert Custom Field From Quote Item to Order Item in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

People also searched for

  • magento 2 how to copy custom data from quote item to order item
  • magento 2 convert quote item to order item
  • convert quote to order magento 2
  • magento 2 add custom attribute to order item
  • magento 2 convert quote to order
  • magento 2 convert quote to order programmatically
  • 2.1.x, 2.2.x, 2.3.x