0

I am using Magento 2.3.0

enter image description here

You can see above grid when you try to add products while order creation in the admin.

I want to add a new column called "Stock" in the above grid and I want to display the product's quantity in stock in this column.

How can I do this?

2 Answers 2

2

Add a new column called Stock while creating the order in the admin need to override the class \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid and add column qty_in_stock

Follow below step to create custom module to add custom column.

Step 1) magento/app/code/Venodr/SalesordercreategridStock/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_SalesordercreategridStock',
    __DIR__
);

Step 2) magento/app/code/Vendor/SalesordercreategridStock/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_SalesordercreategridStock" setup_version="1.0.0">
    </module>
</config>

Step 3) magento/app/code/Vendor/SalesordercreategridStock/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\SalesordercreategridStock\Block\Adminhtml\Order\Create\Search\Grid" />
</config>

Step 4) magento/app/code/Vendor/SalesordercreategridStock/Block/Adminhtml/Order/Create/Search/Grid.php

<?php
namespace Vendor\SalesordercreategridStock\Block\Adminhtml\Order\Create\Search;

class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid
{
    /**
     * @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(
            'qty_in_stock',
            'cataloginventory_stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1 AND {{table}}.website_id=0',
            'left'
        );

        $this->setCollection($collection);

        $parent = get_parent_class($this);
        $parentclass = get_parent_class($parent);
        return $parentclass::_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(
            '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'
            ]
        );

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

        $parent = get_parent_class($this);
        $parentclass = get_parent_class($parent);
        return $parentclass::_prepareColumns();
    }
}

Hope it help!

3
  • I am having multistore setup. Two websites one for the US and one for Canada. So for the multi-website is there any change in the above code? I asked because you used website id for joining. Oct 1, 2019 at 4:33
  • Just check in your two website it is working or not. If not working then replace just that line to below code. Replace with: '{{table}}.stock_id=1 AND {{table}}.website_id=0', to '{{table}}.stock_id=1', Oct 1, 2019 at 4:58
  • worked fine, thanks Jan 14, 2022 at 6:23
0

Another approach to this is to create an after plugin to Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection

<type name="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection">
    <plugin name="some_module_collection"
            type="Vendor\SomeModule\Plugin\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollectionPlugin"/>
</type>

Create the plugin class

class ProductCollectionPlugin
{
    public function afterGetCollectionForStore(
        \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection $subject,
        $collection,
        \Magento\Store\Model\Store $store
    ) {
        $collection->joinField(
            'qty_in_stock',
            'cataloginventory_stock_item'),
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1 AND {{table}}.website_id=0',
            'left'
        );
        return $collection;
    }
}

You can proceed with overriding the Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid

Then, you should only override the following method

protected function _prepareColumns()
    {
        parent::_prepareColumns();
        $this->addColumn(
            'stock',
            [
                'header' => __('Stock'),
                'sortable' => true,
                'type' => 'number',
                'index' => 'stock',
                'position' => 2
            ]
        );
        $parent = get_parent_class($this);
        $superParent = get_parent_class($parent);
        return $superParent::_prepareColumns();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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