Adding Admin Usernames and Action Names via Sales Order Grid Actions in magento2

 Steps to Add Admin User name and Action name in Order Comment Section in Magento 2:

Step 1:  First, we need to create an “events.xml“ file inside our extension at the following path


app\code\Vendor\Extension\etc\adminhtml\


Then add the code as follows.


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="sales_order_save_after">

        <observer name="sales_order_save_after_comment_history_add" 

         instance="\Vendor\Extension\Observer\OrderSaveAfter" />

    </event>

</config>

Step 2:  After that, we need to create an “OrderSaveAfter.php” file inside our extension at the following path


app\code\Vendor\Extension\Observer\


And embed the code as given below.


<?php 

namespace Vendor\Extension\Observer;

 

use Magento\Framework\Event\ObserverInterface;

use Magento\Framework\App\RequestInterface;

use Magento\Backend\Model\Auth\Session;

use Magento\Framework\Exception\NoSuchEntityException;

use Magento\Sales\Api\OrderRepositoryInterface;

use Magento\Sales\Api\Data\OrderStatusHistoryInterface;

use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;

 

class OrderSaveAfter implements ObserverInterface

{

    protected $_request;

    protected $authSession;

    protected $orderStatusRepository;

    protected $orderRepository;

    

    public function __construct(

        RequestInterface  $request,

        Session $authSession,

        OrderStatusHistoryRepositoryInterface $orderStatusRepository,

        OrderRepositoryInterface $orderRepository

    )

    {

     $this->authSession = $authSession;

        $this->_request = $request;

        $this->orderStatusRepository = $orderStatusRepository;

        $this->orderRepository = $orderRepository;

    }

 

    public function execute(\Magento\Framework\Event\Observer $observer)

    {

         if($this->_request->getParam('selected'))

         {

          $selectedids = $this->_request->getParam('selected');

          foreach($selectedids as $orderids)

             {

                 $orderid = $orderids;

                 $this->addCommentToOrder($orderid);

          }

         }

         return $this;

    }

    public function addCommentToOrder($orderId)

    {

        $order = null;

        try

        {

            $order = $this->orderRepository->get($orderId);

        }

        catch (NoSuchEntityException $exception)

        {

            return false;

        }

        $orderHistory = null;

        if ($order)

        {

            $username = $this->authSession->getUser()->getUsername();

            $comment = $order->addStatusHistoryComment(

                      '<b>Admin User :</b> '.$username. " <b>Perform :</b> ".$order->getStatusLabel()

            );

            try

            {

                $orderHistory = $this->orderStatusRepository->save($comment);

            }

            catch (\Exception $exception)

            {

                return false;

            }

        }

        return $orderHistory;

    }

}

Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2