4

I need to add admin username in order comment history whenever I create

  • invoice
  • shipment
  • and On Cancel order.

1 Answer 1

4

I recently did this work through observers

You have to add these event in your module event file Vendor\Yourmodulename\etc\events.xml

For Invoice

<event name="sales_order_invoice_register">
   <observer name="vendor_yourmodulename_observer_addcommentonaction" instance="Vendor\Yourmodulename\Observer\Addcommentonaction" />
</event>

For Cancel

<event name="order_cancel_after">
   <observer name="vendor_yourmodulename_observer_addcommentoncancelaction" instance="Vendor\Yourmodulename\Observer\Addcommentoncancelaction" />
</event>

For Shipment

<event name="sales_order_shipment_save_after">
   <observer name="vendor_yourmodulename_observer_addcommentonshipmentaction" instance="Vendor\Yourmodulename\Observer\Addcommentonshipmentaction" />
</event>

Observer files

For Invoice Add file in your module Vendor\Yourmodulename\Observer\Addcommentonaction.php

<?php
namespace Vendor\Yourmodulename\Observer;

class Addcommentonaction implements \Magento\Framework\Event\ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
  {

    $order= $observer->getData('order');
     //$order->doSomething();
    $authsession = \Magento\Framework\App\ObjectManager::getInstance()->create(\Magento\Backend\Model\Auth\Session::class);
    $username = $authsession->getUser()->getUsername();


    $notify = false;
    $visible = false;
    $history = $order->addStatusHistoryComment("Invoice generated"." (".$username.")", $order->getStatus());
    $history->setIsVisibleOnFront($visible);
    $history->setIsCustomerNotified($notify);
    $history->save();

     return $this;
  }
}

For Cancel Add file in your module Vendor\Yourmodulename\Observer\Addcommentoncancelaction.php

    <?php
namespace Vendor\Yourmodulename\Observer;

class Addcommentoncancelaction implements \Magento\Framework\Event\ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
  {

    $order= $observer->getData('order');
     //$order->doSomething();
    $authsession = \Magento\Framework\App\ObjectManager::getInstance()->create(\Magento\Backend\Model\Auth\Session::class);
    $username = $authsession->getUser()->getUsername();


    $notify = false;
    $visible = false;
    $history = $order->addStatusHistoryComment("Order canceled"." (".$username.")", $order->getStatus());
    $history->setIsVisibleOnFront($visible);
    $history->setIsCustomerNotified($notify);
    $history->save();

     return $this;
  }
}

For Shipment Add file in your module Vendor\Yourmodulename\Observer\Addcommentonshipmentaction.php

 <?php
namespace Vendor\Yourmodulename\Observer;

class Addcommentonshipmentaction implements \Magento\Framework\Event\ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
  {

    $shipment = $observer->getEvent()->getShipment();
    $order = $shipment->getOrder();
    $authsession = \Magento\Framework\App\ObjectManager::getInstance()->create(\Magento\Backend\Model\Auth\Session::class);
    $username = $authsession->getUser()->getUsername();


    $notify = false;
    $visible = false;
    $history = $order->addStatusHistoryComment("Shipment generated"." (".$username.")", $order->getStatus());
    $history->setIsVisibleOnFront($visible);
    $history->setIsCustomerNotified($notify);
    $history->save();

     return $this;
  }
}
5
  • Hi, I need to display the admins name also in here -> prnt.sc/n891za Please help me @Muhammad Hasham
    – Ask Xah
    Apr 6, 2019 at 8:27
  • Hi, i achieved the above by extending the Sales\Model\Order addStatusHistoryComment() function.
    – Ask Xah
    Apr 6, 2019 at 9:46
  • @AskBytes check here ansarhusain.in/2019/04/… i have made what you have required May 2, 2019 at 8:56
  • @AnsarHusain That's great. However i found the solution already. thanks for sharing. :)
    – Ask Xah
    May 2, 2019 at 15:23
  • bro, its useful for me, i just want the observer and event for 'on-hold', 'hold' ,'cod-confirm', 'cc-paid' can you please guide me? Sep 24, 2020 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy