0

I have created custom column in sales_order_item table, I am trying to save product custom field data to sales_order_item custom filed. But I am unable to save.

tried the way to create field as below:

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="sales_order_item" resource="sales" engine="innodb" comment="Sales Order Item">
        <column xsi:type="varchar" name="custom_message"   nullable="true" comment="Custom Message"/>
    </table>
</schema>

Any suggestions to do?

2 Answers 2

1

Solved my self by adding below code.

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="vlc_quote_to_order_item" type="ABCSolutions\SalesOrder\Model\Plugin\Quote\QuoteToOrderItem"/>
    </type>

</config>

and save the custom field in the plugin information like below.

<?php
namespace ABCSolutions\SalesOrder\Model\Plugin\Quote;

use Closure;
use Magento\Framework\App\ObjectManager;

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);
        //$orderItem->setStockMessage('$item->getStockMessage()');

        $objectManager = ObjectManager::getInstance();
        $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProduct()->getId());

        if($_product->getStockMessage() != "In Stock" && $_product->getStockMessage() != "Out of Stock" && $_product->getStockMessage() != "" && $_product->getStockMessage() != null)
            $orderItem->setStockMessage($_product->getStockMessage());

        return $orderItem;
    }

}

and finally convert the quote to order item data using fieldset.xml.

<?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="stock_message">
                <aspect name="to_order_item" />
            </field>
        </fieldset>
    </scope>
</config>
0

Just add the ->setCustomMessage($your_value) in your sales order item save method ?

You might need to use a preference on the class where the save is located and add the set call in the inside.

1
  • Can we have brief ?
    – Bojjaiah
    Sep 23, 2020 at 15:23

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.