I need to extra column to Sales Order Grid that displays product name or product SKU.
-
@Prince Patel It is n't working with ver. 2.2.3. I replaced Vendor/Modules and Test\Test to Hello/MyModule and setup:upgrade then deployed static content but no changes affected on the grid. Do I need to make any more changes ?– Salman KvSep 13, 2018 at 9:33
-
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review– PiyushSep 13, 2018 at 10:08
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review– Rama Chandran MSep 13, 2018 at 10:16
-
This is working, but now if i am exporting csv the sales orders, product column is empty there whereas the product column has values in the grid, how to resolve this for exporting csv?– DebuggerJun 11, 2019 at 6:44
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review– fmsthirdJun 11, 2019 at 7:17
3 Answers
Create sales_order_grid.xml
at
app/code/Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="products" class="Vendor\Module\Ui\Component\Listing\Column\Products">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="visible" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Products</item>
</item>
</argument>
</column>
</columns>
</listing>
Create Products.php
at
app/code/Vendor/Module/Ui/Component/Listing/Column/Products.php
<?php
namespace Test\Test\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 Products 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 &$items) {
$productArr = [];
$order = $this->_orderRepository->get($items["entity_id"]);
foreach ($order->getAllVisibleItems() as $item) {
$productArr[] = $item->getSku();
//$productArr[] = $item->getName(); //Get product name
}
$items['products'] = implode(' - ', $productArr);
unset($productArr);
}
}
return $dataSource;
}
}
Now remove var/ and your output will look like:
OUTPUT:
-
Thank you @prince-patel. You solution worked but with a minor glitch. The product array $productArr was not reseting properly and hence showing previous products which resulted in long strings as the orders grew. So I made some change line 38: $productArr = array(); line 48: unset($productArr); Feb 5, 2018 at 10:38
-
@AbidMalik Yes, you are right. You should reset array. I updated my answer. Feb 7, 2018 at 5:15
-
I am yet getting repeating product details on the order grid after using above code. Can you help me for the same @PrincePatel May 28, 2018 at 6:46
-
@Sumilsanghavi you need to move
$productArr
in foreach loop Check updated answer May 28, 2018 at 9:09 -
1@SagarPPanchal Get product id from
$item
object and use productRepository to get product attribute data. Oct 9, 2020 at 6:19
The issue is was $productArr = []; is defined above the foreach loop of the data items. i have added below the main foreach loop after that working cool. Thanks.
<?php
namespace Test\Test\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 Products 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 &$items) {
$productArr = [];
$order = $this->_orderRepository->get($items["entity_id"]);
foreach ($order->getAllVisibleItems() as $item) {
$productArr[] = $item->getSku();
}
$items['products'] = implode(' - ', $productArr);
}
unset($productArr);
}
return $dataSource;
}
Hello How to make this product sku searchable in order grid, because they are not directly associated with order table