0

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!

3 Answers 3

3

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.

1

I think the event you are observing does not exist.
Try this one instead controller_action_predispatch_adminhtml_sales_order_addcomment.
The comment is submitted to this url: MAGENTO_ROOT/index.php/admin/sales_order/addComment/order_id/ORDER_ID_HERE/

3
  • I may not have explained the question properly. All I need to do is add the name of the Admin that changed an order status... Let's say it was changed to "Hold" I need the admin name to show where it shows the status in the order comments... Commented Aug 1, 2013 at 19:35
  • I understood that you wanted to save the admin name that added a comment
    – Marius
    Commented Aug 2, 2013 at 5:59
  • nah my module already does that... I using an Observer to add the admin name to all areas where comments are added but I need to also add the name for order_status changes too... Commented Aug 2, 2013 at 15:36
1

I actually did do this a couple of years ago:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Company_OrderComment>
            <version>0.0.1</version>
        </Company_OrderComment>
    </modules>
    <adminhtml>
        <events>
            <controller_action_predispatch_adminhtml_sales_order_addComment>
                <observers>
                    <Company_OrderComment>
                        <class>Company_OrderComment_Model_Observer</class>
                        <method>controllerActionPredispatchAdminhtmlSalesOrderAddComment</method>
                    </Company_OrderComment>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_addComment>

            <controller_action_predispatch_adminhtml_sales_order_create_addComment>
                <observers>
                    <Company_OrderComment>
                        <class>Company_OrderComment_Model_Observer</class>
                        <method>controllerActionPredispatchAdminhtmlSalesOrderCreateAddComment</method>
                    </Company_OrderComment>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_create_addComment>   

            <controller_action_predispatch_adminhtml_sales_order_creditmemo_save>
                <observers>
                    <Company_OrderComment>
                        <class>Tamedo_OrderComment_Model_Observer</class>
                        <method>controllerActionPredispatchAdminhtmlSalesOrderCreditmemoSave</method>
                    </Company_OrderComment>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_creditmemo_save>

            <controller_action_predispatch_adminhtml_sales_order_creditmemo_addComment>
                <observers>
                    <Company_OrderComment>
                        <class>Tamedo_OrderComment_Model_Observer</class>
                        <method>controllerActionPredispatchAdminhtmlSalesOrderCreditmemoAddComment</method>
                    </Company_OrderComment>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_creditmemo_addComment>
            <controller_action_predispatch_adminhtml_sales_order_creditmemo_addComment>
                <observers>
                    <Company_OrderComment>
                        <class>Company_OrderComment_Model_Observer</class>
                        <method>controllerActionPredispatchAdminhtmlProductReturnEdit</method>
                    </Company_OrderComment>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_creditmemo_addComment>
        </events>
    </adminhtml>   
</config>`

Observer.php

<?php
    class Company_OrderComment_Model_Observer {
    function controllerActionPredispatchAdminhtmlSalesOrderAddComment($observer)
    {
        $history = Mage::app()->getRequest()->getPost('history');
        if ($history && isset($history['comment'])) {
            $history['comment'] .= $this->_getAppend();
            Mage::app()->getRequest()->setPost('history', $history);
        }
    }
    function controllerActionPredispatchAdminhtmlSalesOrderCreateAddComment($observer)
    {
        $history = Mage::app()->getRequest()->getPost('history');
        if ($history && isset($history['comment'])) {
            $history['comment'] .= $this->_getAppend();
            Mage::app()->getRequest()->setPost('history', $history);
        }
    }
    function controllerActionPredispatchAdminhtmlSalesOrderCreditmemoSave($observer)
    {
        $post = Mage::app()->getRequest()->getPost('creditmemo');
        if ($post && isset($post['comment_text'])) {
            $post['comment_text'] .= $this->_getAppend();
            Mage::app()->getRequest()->setPost('creditmemo', $post);
        }
    }
    function controllerActionPredispatchAdminhtmlSalesOrderCreditmemoAddComment($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 "<br/><br/> From: " . $username;
    }
    }

You can get the full module here Admin order comment

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.