After lots of digging into Magento's core code, I found a solution to my question. Instead of adding a column to the grid via the database, I created a UI component sales_order_grid.xml under [COMPANY]/[MODULE]/view/adminhtml/ui_component/sales_order_grid.xml
Then created the UI class under [COMPANY]/[MODULE]/Ui/Component/Listing/Column/Status.php
<?php
namespace [COMPANY]\[MODULE]\Ui\Component\Listing\Column;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
class Status extends Column
{
protected $_orderRepository;
protected $_searchCriteria;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $criteria,
array $components = [],
array $data = []
) {
$this->_orderRepository = $orderRepository;
$this->_searchCriteria = $criteria;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$order = $this->_orderRepository->get($item["entity_id"]);
$status = $order->getData("export_status");
switch ($status) {
case "0":
$export_status = "No";
break;
case "1";
$export_status = "Yes";
break;
default:
$export_status = "Failed";
break;
}
// $this->getData('name') returns the name of the column so in this case it would return export_status
$item[$this->getData('name')] = $export_status;
}
}
return $dataSource;
}
}
Best Answer
You can add column like this
Using this code you can add column in
quote_item
table