1

I need to add input field similar to "Qty" while selecting the products for order in admin Magento 2

enter image description here

I added input field as shown in the create form but i am not getting how to save it: enter image description here

Please help me for this Thanks in advance Reshma

0

You 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.

10
  • Thank you Himmat But not working for me – Reshma Mar 10 '17 at 8:04
  • Are you getting proper value for $item->getQtyInKgs() in plugin? – Himmat Paliwal Mar 10 '17 at 8:15
  • But where to add input field – Reshma Mar 10 '17 at 8:20
  • In your above post you said: "I added input field as shown in the create form but i am not getting how to save it." Didn't you add that? – Himmat Paliwal Mar 10 '17 at 9:24
  • Ya Himmat I added that. – Reshma Mar 10 '17 at 9:37

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.