Show Notes for this Order Comments on sales order grid
-
15-04-2021 - |
题
Trying to show order comments on sales order grid. I have the column but can't seem to show the data. I can show data with eg city or phone number but not the order comments.
Comments.php
namespace Fitzcode\AdminEdits\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 Comment 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"]);
$comment = $order->getStatusComment();
$item[$this->getData('name')] = $comment;
}
}
return $dataSource;
}
}
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="telephone" class="Fitzcode\AdminEdits\Ui\Component\Listing\Column\Telephone">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Phone Number</item>
<item name="sortOrder" xsi:type="number">6</item>
</item>
</argument>
</column>
<column name="comment" class="Fitzcode\AdminEdits\Ui\Component\Listing\Column\Comment">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Comment</item>
<item name="sortOrder" xsi:type="number">7</item>
</item>
</argument>
</column>
</columns>
</listing>
解决方案
Update your code with $order = $this->_orderRepository->get($item["entity_id"]);
$statusHistoryItem = $order->getStatusHistoryCollection()->getFirstItem();
$status = $statusHistoryItem->getStatusLabel();
$comment = $statusHistoryItem->getComment();
//exit;
$item[$this->getData('name')] = $comment;
其他提示
Did you try printing $dataSource before returning with exit()? Check if $dataSource actually contains the comments data.
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$order = $this->_orderRepository->get($item["entity_id"]);
$comment = $order->getStatusComment();
$item[$this->getData('name')] = $comment;
}
}
echo "<pre>";print_r($dataSource);exit;
return $dataSource;
}