0

How to adding senders name to order comments in the magento2 admin panel. I don't know how to implement this task. Can you please help me? The below screenshot is my required output screenshot.https://prnt.sc/jtx9di

  • You mean customer name? – Sukumar Gorai Jun 12 '18 at 8:18
  • Hi @ Sukumar Gorai, add by admin username. Can you please help me? – Dileep Kumar Jun 12 '18 at 8:30
  • Logged in admin username? Or the name of admin who comment there? Also this is only for admin or on frontend as well? – Sukumar Gorai Jun 12 '18 at 8:38
  • Hi @ Sukumar Gorai, I want to display name of admin who comeent there. – Dileep Kumar Jun 12 '18 at 8:41
  • Hi @ Sukumar Gorai, please find the link. atwix.com/magento/adding-senders-name-order-comments .This is the magento1 and I want to display these in magento2 – Dileep Kumar Jun 12 '18 at 8:43
1

You need to override the following file:

vendor/magento/module-sales/Controller/Adminhtml/Order/AddComment.php

After that inject \Magento\Backend\Model\Auth\Session like below:

public function __construct(
    ...
    \Magento\Backend\Model\Auth\Session $authSession
    ...
){
    ...
    $this->authSession = $authSession;
    ...
}

Under execute function add the below line:

$username = $this->authSession->getUser()->getUsername();
$append = " (by ".$username.")";
$history = $order->addStatusHistoryComment($data['comment'].$append, $data['status']);

In place of

$history = $order->addStatusHistoryComment($data['comment'], $data['status']);

All done. After that you can edit anything from admin will show the admin username to the comments.

If you want to edit the core file AddComment.php then you can add the following code to your AddComment.php. But this is not a good practice to modify core files directly. Best way is to override that file and work accordingly. Code for AddComment.php:

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Controller\Adminhtml\Order;

use Magento\Backend\App\Action;
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;

use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
use Psr\Log\LoggerInterface;

class AddComment extends \Magento\Sales\Controller\Adminhtml\Order
{
    /**
     * Authorization level of a basic admin session
     *
     * @see _isAllowed()
     */
    const ADMIN_RESOURCE = 'Magento_Sales::comment';

    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $_fileFactory;

    /**
     * @var \Magento\Framework\Translate\InlineInterface
     */
    protected $_translateInline;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var \Magento\Framework\Controller\Result\JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * @var \Magento\Framework\View\Result\LayoutFactory
     */
    protected $resultLayoutFactory;

    /**
     * @var \Magento\Framework\Controller\Result\RawFactory
     */
    protected $resultRawFactory;

    /**
     * @var OrderManagementInterface
     */
    protected $orderManagement;

    /**
     * @var OrderRepositoryInterface
     */
    protected $orderRepository;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    protected $authSession;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Translate\InlineInterface $translateInline,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
        \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
        OrderManagementInterface $orderManagement,
        OrderRepositoryInterface $orderRepository,
        LoggerInterface $logger,
        \Magento\Backend\Model\Auth\Session $authSession
    ) {
        $this->authSession = $authSession;
        parent::__construct($context, $coreRegistry,$fileFactory,$translateInline,$resultPageFactory,$resultJsonFactory,$resultLayoutFactory,$resultRawFactory,$orderManagement,$orderRepository,$logger);
    }

    /**
     * Add order comment action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $order = $this->_initOrder();
        if ($order) {
            try {
                $data = $this->getRequest()->getPost('history');
                if (empty($data['comment']) && $data['status'] == $order->getDataByKey('status')) {
                    throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a comment.'));
                }

                $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
                $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;

                $username = $this->authSession->getUser()->getUsername();
                $append = " (by ".$username.")";

                $history = $order->addStatusHistoryComment($data['comment'].$append, $data['status']);
                $history->setIsVisibleOnFront($visible);
                $history->setIsCustomerNotified($notify);
                $history->save();

                $comment = trim(strip_tags($data['comment']));

                $order->save();
                /** @var OrderCommentSender $orderCommentSender */
                $orderCommentSender = $this->_objectManager
                    ->create(\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender::class);

                $orderCommentSender->send($order, $notify, $comment);

                return $this->resultPageFactory->create();
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $response = ['error' => true, 'message' => $e->getMessage()];
            } catch (\Exception $e) {
                $response = ['error' => true, 'message' => __('We cannot add order history.')];
            }
            if (is_array($response)) {
                $resultJson = $this->resultJsonFactory->create();
                $resultJson->setData($response);
                return $resultJson;
            }
        }
        return $this->resultRedirectFactory->create()->setPath('sales/*/');
    }
}

After the changes run the below commands:

bin/magento setup:di:compile

bin/magento setup:static-content:deploy -f

(if needed)(-f if developer mode and default mode enabled)

Hope this helps!

| improve this answer | |
  • Hi @ Sukumar Gorai, how to overriding the file I don't know. Can you please help me this task? – Dileep Kumar Jun 12 '18 at 10:28
  • Hi @ Sukumar Gorai, I have changed to file path vendor/magento/module-sales/Controller/Adminhtml/Order/AddComment.php In this file I have added your code but Comments was not creating . – Dileep Kumar Jun 12 '18 at 10:29
  • If you are adding the same code to core file then you need to modify the AddComment.php. I am adding AddComment.php file to my answer. – Sukumar Gorai Jun 12 '18 at 10:43
  • Hi @ Sukumar Gorai, Good morning. It is not working. Can you please help me? – Dileep Kumar Jun 13 '18 at 4:31
  • Did you try to post new comments? – Sukumar Gorai Jun 13 '18 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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