Monday, 8 April 2019

Display admin user name who made order comment Magento2

1.Create file registration.php in app/code/Order/CommentsUser/registration.php

<?php
/**
 * Ansar Husain
 * Order_CommentsUser
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Order_CommentsUser',
    __DIR__
);



2. Create file module.xml app/code/Order/CommentsUser/etc/module.xml

<?xml version="1.0"?>
<!--
/**
* Ansar Husain
* Order_CommentsUser
**/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Order_CommentsUser" setup_version="1.0.0" />
</config>

2. Create file di.xml app/code/Order/CommentsUser/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Ansar Husain
 * Order_CommentsUser
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Controller\Adminhtml\Order\AddComment" type="Order\CommentsUser\Controller\Adminhtml\Order\AddComment" />
</config>

3. Create file AddComment.xml Order\CommentsUser\Controller\Adminhtml\Order\AddComment.php

<?php

/**
 * Ansar Husain
 * Order_CommentsUser
 */

namespace Order\CommentUser\Controller\Adminhtml\Order;


class AddComment extends \Magento\Sales\Controller\Adminhtml\Order\AddComment
{


    public function execute()
    {
         $user = $this->_auth->getUser();
         $append = "<b>(by ".$user->getUsername().")</b>";
         $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;

                $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/*/');
    }
}


No comments:

Post a Comment