Adding sender’s name to order comments in Magento admin

Adding sender's name to order comments in Magento admin

If there is more than one person who serves Magento orders you may want to be able to put sender’s name next to the comment. As you know, order’s comments are already implemented in Magento, so we are going to change it a little to have name of commentator inserted and display it automatically.
The final result will look like this:
Magento admin order view - comments history
Moreover, the idea is very simple. We will append sender’s name to the body of the comment using special markup [name](by name)[/name].
So, let’s start with rewriting orders controller. Pay attention, here we append username to the comment:

//file: app/code/community/Atwix/OrderComment/controllers/Adminhtml/Sales/OrderController.php
public function addCommentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $response = false;
                $data = $this->getRequest()->getPost('history');
                $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
                $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;
                
                //getting username
                $session = Mage::getSingleton('admin/session');
                $username = $session->getUser()->getUsername();
                $append = " [name](by ".$username.")[/name]";
                
                //appending username with markup to comment
                $order->addStatusHistoryComment($data['comment'].$append, $data['status'])
                    ->setIsVisibleOnFront($visible)
                    ->setIsCustomerNotified($notify);

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

                $order->save();
                $order->sendOrderUpdateEmail($notify, $comment);

                $this->loadLayout('empty');
                $this->renderLayout();
            }
            catch (Mage_Core_Exception $e) {
                $response = array(
                    'error'     => true,
                    'message'   => $e->getMessage(),
                );
            }
            catch (Exception $e) {
                $response = array(
                    'error'     => true,
                    'message'   => $this->__('Cannot add order history.')
                );
            }
            if (is_array($response)) {
                $response = Mage::helper('core')->jsonEncode($response);
                $this->getResponse()->setBody($response);
            }
        }
    }

As the second action, we modify template file to display the name of the commentator. File app/design/adminhtml/default/default/template/sales/order/view/history.phtml, replaces the following line:

<br/><?php echo $this->escapeHtml($_item->getComment(), array('b','br','strong','i','u')) ?>

with

<br/><?php echo preg_replace("/\[name\](.*?)\[\/name\]/", "<span class='comment-sender'>$1</span>",$this->escapeHtml($_item->getComment(), array('b','br','strong','i','u', 'span')) )  ?>

As well, one more template is needed to be updated for Comments History section. File app/design/adminhtml/default/default/template/sales/order/view/tab/history.phtml
replaces the line:

<br/><?php echo $_comment ?>

with

<br/><?php echo preg_replace("/\[name\](.*?)\[\/name\]/", "<span class='comment-sender'>$1</span>", $_comment) ?>

Magento admin - Comments History Section
That is enough for our feature to function. However, you can add some CSS styles:

.comment-sender{
  color: #ff4500;
 }

Note, that it is a basic example, so improving this approach is welcome. The code can be found here. Hope you will find this simple feature helpful. Stay tuned.

Read more:

Clients

Smart Brands Choose Us.

From Mark Cuban and Sir Richard Branson backed startups, to Inc 500 US Fastest Growing Companies and Global Brands choose us to deliver and support unparalleled eСommerce experience for their customers.