I need to show stock qty column on order create grid in order to place an order easily when generating an order on admin side.
TO DOs are :
1) Add Stock quantity column (Done)
2) Render the value of stock quantity
Could you help me out how to do this?
app/[Vendor]/[Module-Name]/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-Name]\Block\Adminhtml\Grid" />
</config>
app\code\ [Vendor][Module-Name]\Block\Adminhtml
<?php
/**
* Created by
* Date: 13/05/2015
* Time: 5:02 PM
*/
namespace [Vendor]\[Module-Name]\Block\Adminhtml;
use Magento\Framework\View\Element\Template;
class Grid extends \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid
{
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(
'remain_qty',
[
'filter' => false,
'sortable' => false,
'header' => __('Quantity'),
'renderer' => '[Vendor]\[Module-Name]\Block\Adminhtml\Grid\Renderer\Remain',
//'values' => 3,
'name' => 'remain_qty',
'inline_css' => 'remain_qty',
'type' => 'text',
'validate_class' => 'validate-number',
'index' => 3
]
);
/*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();
}
}
At this moment, using my above codes, i can see empty custom column at order create grid like this :
To render stock information, i make a block inherited from stock repository. But i don't know how to do.
app\code[Vendor][module-Name]\Block\Adminhtml\Grid\Renderer\Remian.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace [Vendor]\[module-Name]\Block\Adminhtml\Grid\Renderer;
/**
* Renderer for Remain Qty field in sales create new order search grid
*
* @author
*/
class Remain extends \Magento\Framework\View\Element\Template,
// \Magento\Backend\Block\Widget\Grid\Column\Renderer\Text
{
/**
* Type config
*
* @var \Magento\Catalog\Model\ProductTypes\ConfigInterface
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
array $data = []
)
{
$this->_stockItemRepository = $stockItemRepository;
parent::__construct($context, $data);
}
public function getStockItem($productId)
{
return $this->_stockItemRepository->get($productId);
}
}
/**
* Returns whether this qty field must be inactive
*
* @param \Magento\Framework\DataObject $row
* @return bool
*/
protected function _isInactive($row)
{
return $this->typeConfig->isProductSet($row->getTypeId());
}
/**
* Render product qty field
*
* @param \Magento\Framework\DataObject $row
* @return string
*/
public function render(\Magento\Framework\DataObject $row)
{
// what should i insert that?
}
}
Best Answer
You should try this