Magento – Save admin name to order_status change using an Observer

event-observermagento-1.7

I am trying to figure out which event to observe in order to save the name of a logged in admin that changes an order state making it visible in the order comments.

I have been able to observe other events in order to append the admin name to order notes, credit memo and invoice creation but I can't seem to find the correct event to use.

I've tried the sales_order_save_before and sales_order_save_after but it won't same the admin name to the comments history.

Can anyone help me out please?

Here's my observer based module code:

config.xml

            <controller_action_predispatch_adminhtml_sales_order_save_before>
                <observers>
                    <module_ordercomment>
                        <class>Thaneuk_OrderComment_Model_Observer</class>
                        <method>controllerActionPredispatchAdminhtmlSalesOrderSaveBefore</method>
                    </module_ordercomment>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_save_before>                         
        </events>

And Observer.php

<?php

class Company_OrderComment_Model_Observer {

function controllerActionPredispatchAdminhtmlSalesOrderSaveBefore($observer)
{
    $post = Mage::app()->getRequest()->getPost('comment');
    if ($post && isset($post['comment'])) {
        $post['comment'] .= $this->_getAppend();
        Mage::app()->getRequest()->setPost('comment', $post);
    }
}

protected function _getAppend()
{
    $user     = Mage::getSingleton('admin/session');
    $username = $user->getUser()->getUsername();
    return " : " . $username;
}

}

A heads up would be appreciated!

Best Answer

EDIT:

You can also do the following, which I would recommend over the previous solution I posted. Mostly because it doesn't use setPost, which I would consider scary.

config.xml:

<adminhtml>
  <events>
    <sales_order_status_history_save_before>
      <observers>
        <module_status_history_save_before>
          <class>module/observer</class>
          <method>commentSaveBefore</method>
        </module_status_history_save_before>
      </observers>
    </sales_order_status_history_save_before>
  </events>
</adminhtml>

Observer.php:

public function commentSaveBefore($obs) {
    $event = $obs->getEvent();
    $status = $event->getDataObject();
    $status->setComment($status->getComment().$this->_getAppend());
}

protected function _getAppend()
{
    $admin = Mage::getSingleton('admin/session')->getUser();
    $username = $admin->getUsername();
    return " : " . $username;
}

This would accomplish the same thing, except you're intercepting the model before it gets saved, allowing you to alter the comment without having to use setPost().

Original Post: You could do the following:

config.xml:

<adminhtml>
    <events>
        <controller_action_predispatch_adminhtml_sales_order_addComment>
            <observers>
                <module_order_save_comment>
                    <class>module/observer</class>
                    <method>adminhtmlSavingOrderComment</method>
                </module_order_save_comment>
            </observers>
        </controller_action_predispatch_adminhtml_sales_order_addComment>
    </events>
</adminhtml>

Observer.php:

public function adminhtmlSavingOrderComment($obs) {
    $event = $obs->getEvent();
    $order = $event->getOrder();
    $data = Mage::app()->getRequest()->getPost('history');
    if ($data && isset($data['comment'])) {
        $data['comment'] .= $this->_getAppend();
        Mage::app()->getRequest()->setPost('history', $data);
    }
}

protected function _getAppend()
{
    $admin = Mage::getSingleton('admin/session')->getUser();
    $username = $admin->getUsername();
    return " : " . $username;
}

This should work for you.

Related Topic