0

I am trying to show admin order comments in a new column I created in sales order grid. Anyone know how I would do this?

Admin Order Comments

2
  • For a single order multiple comment will be there. which comment are you show in order grid? Oct 28, 2020 at 1:42
  • The most recent comment would be preferred.
    – Shaun
    Oct 28, 2020 at 4:01

2 Answers 2

0

[This link will help you to solve your issue ] https://www.youtube.com/watch?v=X5fc0rsCrdw

0

**

Create File At: 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">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">
                    Vendor_Module/js/grid/listing
                </item>
            </item>
        </argument>
        <column name="letestcomment" class="Dolphin\Ordercomment\Ui\Component\Listing\Column\Letestcomment">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="bodyTmpl" xsi:type="string">
                        ui/grid/cells/html
                    </item>
                    <item name="visible" xsi:type="boolean">
                        true
                    </item>
                    <item name="label" xsi:type="string" translate="true">
                        Letest Comment
                    </item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

**

Than Create Another File At: Vendor\Module\Ui\Component\Listing\Column\Letestcomment.php

**

<?php
namespace Vendor\Module\Ui\Component\Listing\Column;

use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;

class Letestcomment extends Column
{
    protected $historyCollectionFactory;

    public function __construct(
        ContextInterface $context,
        CollectionFactory $historyCollectionFactory,
        UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ) {
        $this->historyCollectionFactory = $historyCollectionFactory;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$items) {
                $history = $this->historyCollectionFactory->create();
                $history->addFieldToFilter('parent_id', $items["entity_id"]);

                //uncomment code as your requirements to set filter
                // $history->addFieldToFilter('comment', array('neq' => 'NULL'));
                // $history->addFieldToFilter('entity_name', 'order');
                // $history->setOrder('created_at', 'DESC');
                $comment = $history->getFirstItem()->getComment();
                $items['letestcomment'] = $comment;
            }
        }
        return $dataSource;
    }
}

Uncomment created_at DESC Filter If You Want Most Recent Comment. & Run command php -f bin/magento setup:upgrade.

And Done :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.