3

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

  • invoice
  • shipment
  • and On Cancel order.
3

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;
  }
}
  • Hi, I need to display the admins name also in here -> prnt.sc/n891za Please help me @Muhammad Hasham – Ask Bytes Apr 6 at 8:27
  • Hi, i achieved the above by extending the Sales\Model\Order addStatusHistoryComment() function. – Ask Bytes Apr 6 at 9:46
  • @AskBytes check here ansarhusain.in/2019/04/… i have made what you have required – Ansar Husain May 2 at 8:56
  • @AnsarHusain That's great. However i found the solution already. thanks for sharing. :) – Ask Bytes May 2 at 15:23

Your Answer

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