Magento 1.9.3 – How to Get Last Order Comment After Event sales_order_status_history_save_after

historymagento-1.9orders

I have created my own function which is trigged for the event sales_order_status_history_save_after. What I wanted to do is, when a new comment is added, the system will get the latest order comment, and send it to customer.

I have tried using the following code,

    public function CommentSaveAfter()
    {
            $history = $order->getStatusHistoryCollection()->getFirstItem();
            return $history->getComment();

    }

However, this code only get me the last comment before the new one is added.

See screenshot, the code only get the last comment 4444, not 5555 when I click on "Submit Comment"
enter image description here

How can I get the latest comment when click on Submit Comment with event sales_order_status_history_save_after ?


This question is not duplicated with Magento event for adding order comment

My problem is I can not get the comment I just added to the order comment, but only the latest comment from status_history. I have already managed to add my own function to the event sales_order_status_history_save_after. So it's different from the other question.

Best Answer

Event to use when order comment is post

You can use sales_order_status_history_save_before or sales_order_status_history_save_afterand you will get the comment you are saving as an object using

 $observer->getEvent()->getStatusHistory()
Related Topic