I need to add input field similar to "Qty" while selecting the products for order in admin Magento 2
I added input field as shown in the create form but i am not getting how to save it:
Please help me for this Thanks in advance Reshma
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou need to create new field you wish to save the value(qty_in_kgs) in sales_order_item table first. Than by using the plugin, you can save the value in table as below
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="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
<plugin name="your_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->setQtyInKgs($item->getQtyInKgs());//set your custom value.
return $orderItem;// return an object '$orderItem' which will replace result of function 'convert' in class 'Magento\Quote\Model\Quote\Item\ToOrderItem'
}
}
I didn't give try to this but it should work. Once the value saved in order item table, you can show it wherever you want and proceed further.