Show Tier Price in Product Grid of Sales Order creation form Magento 2
-
13-04-2021 - |
Question
I'm trying to show the Tier Price
in the Product Grid
of the sales order creation page, but can't show.
I tried the below code.
added the column to _prepareColumns()
method from Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid.php
class
$this->addColumn(
'action', array(
'header' => __('Price Breaks'),
'width' => '100',
'type' => 'action',
'actions' => array(
array(
'caption' => __('Tier Price'),
//'url' => array('base' => '*/*/*'),
'field' => 'entity_id',
'target'=>'_blank',
'id' => 'price_break',
'class' => 'price_break',
// 'onclick' => "window.open('http://www.google.com')"
)
),
'index' => 'entity_id',
));
but it's not displaying becz it's not a field.
Solution
Finally, I did by using the below code.
Grid.php
protected function _prepareColumns()
{
$this->addColumn(
'available_qty',
[
'filter' => false,
'sortable' => false,
'header' => __('Price Break'),
'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Availableqty::class,
'name' => 'available_qty',
'inline_css' => 'available_qty',
'type' => 'input',
//'validate_class' => 'validate-number',
'index' => 'available_qty',
'sortOrder' => 500
]
);
return parent::_prepareColumns();
}
Availableqty.php
<?php
namespace Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer;
use Magento\Catalog\Model\ProductRepository;
class Availableqty extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Text
{
/**
* @var \Magento\CatalogInventory\Api\StockStateInterface
*/
private $stockItem;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $_product;
/**
* @var ProductRepository
*/
protected $_productRepository;
/**
* @var \Magento\Backend\Model\Session\Quote
*/
protected $sessionQuote;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var \Magento\Customer\Model\CustomerFactory
*/
protected $_customer;
/**
* Availableqty constructor.
*
* @param \Magento\CatalogInventory\Api\StockStateInterface $stockItem
* @param \Magento\Catalog\Model\Product $product
* @param ProductRepository $productRepository
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
* @param \Magento\Framework\ObjectManagerInterface $_objectManager
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
*/
public function __construct(
\Magento\CatalogInventory\Api\StockStateInterface $stockItem,
\Magento\Catalog\Model\Product $product,
ProductRepository $productRepository,
\Magento\Backend\Model\Session\Quote $sessionQuote,
\Magento\Framework\ObjectManagerInterface $_objectManager,
\Magento\Customer\Model\CustomerFactory $customerFactory
)
{
$this->stockItem = $stockItem;
$this->_product = $product;
$this->_productRepository = $productRepository;
$this->sessionQuote = $sessionQuote;
$this->_objectManager = $_objectManager;
$this->_customer = $customerFactory;
}
/**
* Retrieve session object
*
* @return \Magento\Backend\Model\Session\Quote
*/
protected function _getSession()
{
return $this->_objectManager->get(\Magento\Backend\Model\Session\Quote::class);
}
/**
* @param \Magento\Framework\DataObject $row
* @return float|string
*/
public function render(\Magento\Framework\DataObject $row)
{
//$_product = $this->_product->load($row->getId());
$customerId = $this->_getSession()->getCustomerId();
$customerGId = 0;
if($customerId != null){
$customer = $this->_customer->create()->load($customerId);
$customerGId = $customer->getGroupId();
}
// $customer = $this->sessionQuote->getOrder()->getCustomer();
$_product = $this->_productRepository->getById($row->getId());
$tierPriceBreak = "";
$i = 1;
$checkArray = array(
"Pack",
"Each",
"Case",
"Bundle",
"Box"
);
$content = "";
if($_product->getTypeId() == "simple"){
$tierPrice = $_product->getTierPrices();
$stockMessage = $_product->getWebPackageBreak();
if(count($checkArray) > 0 && $stockMessage != null){
foreach($checkArray as $needle){
if(strpos( $stockMessage, $needle ) !== false ) {
$content = $needle;
$isStockMessagesExists = true;
break;
}
}
}
if($content == ""){
$content = "Each";
}
if(count($tierPrice) > 0){
foreach($tierPrice as $pirces){
if($pirces->getCustomerGroupId() == $customerGId){
$price = number_format($pirces->getValue(), 2, '.', '');
$qty = number_format($pirces->getQty(), 0, '.', '');
$tierPriceBreak .= " $i => Buy " . $qty." for $" . $price . "/$content ";
$i++;
}
}
}
}else{
$tierPriceBreak = "";
}
//return $this->stockItem->getStockQty($row->getId(), $row->getStore()->getWebsiteId());
return $tierPriceBreak;
}
}
It will show you a column with tier price.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange