6

There is custom field in quote_item table same as sales_order_item both column name and type are same.

I want to convert quote_item table to sales_order_item table. so I have used fieldset.xml to convert those data,

my fieldset.xml file is,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
    <fieldset id="quote_convert_item">
        <field name="customcolumn1">
            <aspect name="to_order_item" />
        </field>
        <field name="customcolumn2">
            <aspect name="to_order_item" />
        </field>
    </fieldset>
</scope>
</config>

but its not working so strange

5
  • Please clear all cache & rebuild all indexing. after try again. :)
    – Abdul
    Feb 8, 2016 at 6:51
  • Have you investigated log files, any error messages? Feb 8, 2016 at 12:37
  • yes i checked log files no error message
    – Newbie
    Feb 8, 2016 at 12:39
  • That configuration should be enough try to clean all var/generation and var/di and cache. and as @David Verholen said, debug your data there. Feb 10, 2016 at 6:30
  • I think you have to create plugins for the quoteItem:getExtentionAttributes and quoteItem:set ExtentionAttribute, the same for the orderItem, so that getDataFromFieldset to work. Dec 23, 2021 at 17:43

5 Answers 5

21

In Magento 2 without using fieldset you can also copy custom data from quote item to order item by using plugin.

create di.xml in your module etc folder.

app/code/Vender/Yourmodule/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="cedapi_quote_to_order_item" type="Vender\Yourmodule\Model\Plugin\Quote\QuoteToOrderItem"/>
    </type>
</config>

Create a class to your module and define a function. app/code/Vender/Yourmodule/Model/Plugin/Quote
Create QuoteToOrderItem.php file

<?php
namespace Vender\Yourmodule\Model\Plugin\Quote;

use Closure;

class QuoteToOrderItem
{
    /**
     * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject
     * @param callable $proceed
     * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
     * @param array $additional
     * @return \Magento\Sales\Model\Order\Item
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
  public function aroundConvert(
        \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
        Closure $proceed,
        \Magento\Quote\Model\Quote\Item\AbstractItem $item,
        $additional = []
    ) {
        /** @var $orderItem \Magento\Sales\Model\Order\Item */
        $orderItem = $proceed($item, $additional);//result of function 'convert' in class 'Magento\Quote\Model\Quote\Item\ToOrderItem' 
        $orderItem->setCustomDesign($item->getCustomDesign());//set your required
        return $orderItem;// return an object '$orderItem' which will replace result of function 'convert' in class 'Magento\Quote\Model\Quote\Item\ToOrderItem'
    }

}
3
  • 1
    +1, This is working fine for quote to sales_order_item. Could you please show how to convert sales_order_item to sales_invoice_item? Sep 21, 2016 at 12:31
  • 1
    I tried with some more data. It is not working. Sep 7, 2018 at 6:50
  • Thanks, It's working fine. But in multiple address checkout it's not working. do we've to follow any other pattern to update the quote item value in order item table during the multiple address checkout ?
    – senthil
    Jul 7, 2022 at 8:24
1

On your fieldset, try this:

<fieldset id="sales_convert_quote_item">

instead of what you currently have:

<fieldset id="convert_quote_item">
1

I think the converting is done here: https://github.com/magento/magento2/blob/2.0/app/code/Magento/Quote/Model/Quote/Item/ToOrderItem.php#L61

you may want to debug here and see what happens

UPDATE

had the same problem today. It seems to be a bug since the converting itself seems to work, but the populeateWithArray then strips the new attribute.

I solved it with an around plugin on the ToOrderItem class like described here

1

In Magento\Quote\Model\Quote\Item\ToOrderItem::convert() After

    $orderItemData = $this->objectCopyService->getDataFromFieldset(
        'quote_convert_item',
        'to_order_item',
        $item
    );

The $orderItemData contains the data of the custom field that you declare in fieldset.xml

But after that Magento create order item by Magento\Sales\Api\Data\OrderItemInterfaceFactory and set data of $orderItemData to it

    $orderItem = $this->orderItemFactory->create();
    $this->dataObjectHelper->populateWithArray(
        $orderItem,
        array_merge($orderItemData, $data),
        \Magento\Sales\Api\Data\OrderItemInterface::class
    );

They will ignore custom fields that didn't define in OrderItemInterface

0

Try to create observer of sales_model_service_quote_submit_before and there get the object of order using $order=$observer->getOrder(); and set your values on that order object. it will solve your problem.

==== Update======
create events.xml and put the following code

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_before">
        <observer name="event_name" instance="Companyname\Packagename\Observer\Observername" />
    </event>
</config>


now create observer file Observername.php add this code

public function execute(\Magento\Framework\Event\Observer $observer)
    {  
        $quote=$observer->getQuote();
        $order=$observer->getOrder();
        $order->setCustomcolumn1($quote->getCustomcolumn1());
        $order->setCustomcolumn2($quote->getCustomcolumn2());
    }
3
  • 1
    Here we are talking about quoteItem to orderItem not quote to order
    – MagePsycho
    Oct 12, 2017 at 9:31
  • I have added comment field after payment methods and before apply discount. I have added the custom column to quote and sales_order. I am using this event observer code to get comment data but not getting comment field data. please help me.
    – Magecode
    Jun 14, 2018 at 11:49
  • In quote I am not getting data of custom field which i have added in Review & payment step after Payment Method
    – Magecode
    Jun 14, 2018 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.