7

How can I add new column in product grid when you create new order from admin panel ? see this screenshot for more information. enter image description here

i need product qty in new column?

6

Thansk to @Dhrumin.

My working code here:

app/code/[Vendor]/[Module]/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor]_[Module]',
    __DIR__
);

app/code/[Vendor]/[Module]/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="[Vendor]_[Module]" setup_version="0.0.1"/>
</config>

app/code/[Vendor]/[Module]/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">
    <preference for="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid" type="[Vendor]\[Module]\Block\Adminhtml\Order\Create\Search\Grid" />
</config>

app/code/[Vendor]/[Module]/Block/Adminhtml/Order/Create/Search/Grid.php

<?php

namespace [Vendor]\[Module]\Block\Adminhtml\Order\Create\Search;

class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid
{
    /**
     * Grid constructor.
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param \Magento\Catalog\Model\Config $catalogConfig
     * @param \Magento\Backend\Model\Session\Quote $sessionQuote
     * @param \Magento\Sales\Model\Config $salesConfig
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\Config $catalogConfig,
        \Magento\Backend\Model\Session\Quote $sessionQuote,
        \Magento\Sales\Model\Config $salesConfig,
        array $data = []
    )
    {
        $this->_productFactory = $productFactory;
        $this->_catalogConfig = $catalogConfig;
        $this->_sessionQuote = $sessionQuote;
        $this->_salesConfig = $salesConfig;
        parent::__construct($context, $backendHelper, $productFactory, $catalogConfig, $sessionQuote, $salesConfig, $data);
    }

    /**
     * Prepare collection to be displayed in the grid
     *
     * @return $this
     */
    protected function _prepareCollection()
    {
        $attributes = $this->_catalogConfig->getProductAttributes();
        /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
        $collection = $this->_productFactory->create()->getCollection();
        $collection->setStore(
            $this->getStore()
        )->addAttributeToSelect(
            $attributes
        )->addAttributeToSelect(
            'sku'
        )->addStoreFilter()->addAttributeToFilter(
            'type_id',
            $this->_salesConfig->getAvailableProductTypes()
        )->addAttributeToSelect(
            'gift_message_available'
        );

        $collection->joinField(
            'barcode_qty',
            'cataloginventory_stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1 AND {{table}}.website_id=0',
            'left'
        );

        $this->setCollection($collection);

        $p = get_parent_class($this);
        $pp = get_parent_class($p);
        return $pp::_prepareCollection();
    }

    /**
     * Prepare columns
     *
     * @return $this
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            [
                'header' => __('ID'),
                'sortable' => true,
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
                'index' => 'entity_id'
            ]
        );

        $this->addColumn(
            'barcode_qty',
            [
                'header' => __('Quantity'),
                'type' => 'number',
                'index' => 'barcode_qty'
            ]
        );

        $this->addColumn(
            'name',
            [
                'header' => __('Product'),
                'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Product::class,
                'index' => 'name'
            ]
        );
        $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
        $this->addColumn(
            'price',
            [
                'header' => __('Price'),
                'column_css_class' => 'price',
                'type' => 'currency',
                'currency_code' => $this->getStore()->getCurrentCurrencyCode(),
                'rate' => $this->getStore()->getBaseCurrency()->getRate($this->getStore()->getCurrentCurrencyCode()),
                'index' => 'price',
                'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Price::class
            ]
        );

        $this->addColumn(
            'in_products',
            [
                'header' => __('Select'),
                'type' => 'checkbox',
                'name' => 'in_products',
                'values' => $this->_getSelectedProducts(),
                'index' => 'entity_id',
                'sortable' => false,
                'header_css_class' => 'col-select',
                'column_css_class' => 'col-select'
            ]
        );

        $this->addColumn(
            'qty',
            [
                'filter' => false,
                'sortable' => false,
                'header' => __('Quantity'),
                'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty::class,
                'name' => 'qty',
                'inline_css' => 'qty',
                'type' => 'input',
                'validate_class' => 'validate-number',
                'index' => 'qty'
            ]
        );

        $p = get_parent_class($this);
        $pp = get_parent_class($p);
        return $pp::_prepareColumns();
    }

}

Hope it helps.

  • 1
    YOU ARE MY HERO THANK YOU GOOOOD Btw i don't get that Magento team have never thought about this function. I think this function is the one of the functions which many Magento users need practically. Your suggestion was awesome. It was the common mistake which doesn't chain complete object with original object. – 3CE Nov 14 '17 at 16:08
  • A big thanks goes to PHP docs get_parent_class :D – hayatbiralem Nov 14 '17 at 16:09
  • By the way, do you have any idea how to sort the custom field on where i want to put. I was about to overwrite view/adminhtml/ui_component but i couldn't find about it. Thank you – 3CE Nov 14 '17 at 16:39
  • I've updated the _prepareColumns function. We took all the control over :D So you don't need to override any ui_component here. – hayatbiralem Nov 14 '17 at 17:42
5

Yes i find the solution for my question, open below file for path and override in your o custom module.

vendor/magento/module-sales/Block/Adminhtml/Order/Create/Search/Grid.php

And add below code in _prepareCollection() function,

$collection->joinField(
                'qty_in_stock',
                'cataloginventory_stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left'
        );

Now add below code in _prepareColumns function.

$this->addColumn(
                'qty_in_stock',
                [
                'header' => __('Quantity In Stock'),
                'type' => 'number',
                'index' => 'qty_in_stock'
                ]
        );

Hope it's help you.

  • It doesn't work at M2.1.9 – 3CE Nov 14 '17 at 15:03
  • Thanks a lot! It works on M2.2.0. @3CE Sorry. My previous suggestion is not make any sense. I think you must check your code very carefully because I made a lot of mistakes. For example, I extended the wrong class at first. – hayatbiralem Nov 14 '17 at 15:43
  • @hayatbiralem Yes i think you are right. I really wonder why my module doesn't work right now. Could you have a look for a second? I'll update my code. – 3CE Nov 14 '17 at 15:49
  • @hayatbiralem I just updated my code at this moment. Could you have a look at it. Thank you! – 3CE Nov 14 '17 at 15:52
  • Yeap, I looked at it and I saw the problem because I had the same problem. Check my answer :) – hayatbiralem Nov 14 '17 at 16:05
0

We can add a new custom column in admin product grid by using product_listing.xml.

Let's create a file view\adminhtml\ui_component\product_listing.xml with the following content:

<?xml version="1.0"?>

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
       <column name="custom_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Custom Field</item>
                    <item name="sortOrder" xsi:type="number">75</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Finally, clear cache and check results.

0

EDIT

Look At @hayatbiralem Answer.Thank you


@hayatbiralem

My di.xml file was good because the empty comlumn was created well. But the thing is it couldn't get any data from joined field.

enter image description here

<?php
/**
 * Created by -
 * Date: 13/05/2015
 * Time: 5:02 PM
 */
namespace Kbethos\Ordercreateqty\Block\Adminhtml;
use Magento\Framework\View\Element\Template;
class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid
{
    protected function _prepareCollection()
    {
        $attributes = $this->_catalogConfig->getProductAttributes();
        /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
        $collection = $this->_productFactory->create()->getCollection();



        $collection
        ->setStore(
            $this->getStore()
        )->addAttributeToSelect(
            $attributes
        )->addAttributeToSelect(
            'sku'
        )->addStoreFilter()->addAttributeToFilter(
            'type_id',
            $this->_salesConfig->getAvailableProductTypes()
        )->addAttributeToSelect(
            'gift_message_available'
        )->joinField(
                'qty_in_stock',
                'cataloginventory_stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left'
        );

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }


    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            [
                'header' => __('ID'),
                'sortable' => true,
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
                'index' => 'entity_id'
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('Product'),
                'renderer' => 'Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Product',
                'index' => 'name'
            ]
        );
        $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
        $this->addColumn(
            'price',
            [
                'header' => __('Price'),
                'column_css_class' => 'price',
                'type' => 'currency',
                'currency_code' => $this->getStore()->getCurrentCurrencyCode(),
                'rate' => $this->getStore()->getBaseCurrency()->getRate($this->getStore()->getCurrentCurrencyCode()),
                'index' => 'price',
                'renderer' => 'Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Price'
            ]
        );

        $this->addColumn(
            'in_products',
            [
                'header' => __('Select'),
                'type' => 'checkbox',
                'name' => 'in_products',
                'values' => $this->_getSelectedProducts(),
                'index' => 'entity_id',
                'sortable' => false,
                'header_css_class' => 'col-select',
                'column_css_class' => 'col-select'
            ]
        );

        /*20171031 add show qty */
        $this->addColumn(
            'qty_in_stock',
            [
                'header' => __('Quantity In Stock'),
                'type' => 'number',
                'index' => 'qty_in_stock'
            ]
        );
         /*20171031 add show qty */

        $this->addColumn(
            'qty',
            [
                'filter' => false,
                'sortable' => false,
                'header' => __('Qty to Order'),
                'renderer' => 'Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty',
                'name' => 'qty',
                'inline_css' => 'qty',
                'type' => 'input',
                'validate_class' => 'validate-number',
                'index' => 'qty'
            ]
        );

        return parent::_prepareColumns();
    }
}
  • Hi @3CE, look at my answer. The tricky part is return parent::_prepareCollection(); You must use the lines starts with $p = get_parent_class($this); – hayatbiralem Nov 14 '17 at 16:02

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.