Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

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

Vinh Jacker | 12-18-2024

Convert Custom Field From Quote Item to Order Item

Usually, when customers add an item to the cart, it will be stored into the quote object. Sometimes, you will want to pass the cart value in the quote as an order object for easier access to the value. To do that, you need to convert your custom field from the quote to the order.

Therefore, in today’s post, we will learn about the way to convert custom field from quote item to order item in Magento 2. Follow the instruction below to successfully get the work done.

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.

Final words

That is all requirements every Magento 2 store owners should apply to copy custom data from quote item to order item. If you have any issues while following this tutorial, don’t hesitate to contact us. Thanks for reading!

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services