5

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

4
  • Please clear all cache & rebuild all indexing. after try again. :) – Abdul Feb 8 '16 at 6:51
  • Have you investigated log files, any error messages? – Arkadii Chyzhov Feb 8 '16 at 12:37
  • yes i checked log files no error message – Newbie Feb 8 '16 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. – MauroNigrele Feb 10 '16 at 6:30
19

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'
    }

}
2
  • 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? – Rakesh Jesadiya Sep 21 '16 at 12:31
  • 1
    I tried with some more data. It is not working. – Saravanan DS Sep 7 '18 at 6:50
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

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
  • Here we are talking about quoteItem to orderItem not quote to order – MagePsycho Oct 12 '17 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 '18 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 '18 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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