SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH

How to add comment history to order in magento 2

You can add Status History Comment for the given order programmatically in Magento 2.

You must get the Order Id to add comments and load the order object. Here is the code to add a comment to the order with the comment we set as default. With this tutorial we add observer comments

Let’s create the events.xml file in the module under the path Magepow/HistoryOrderComment/etc.

<event name="checkout_submit_all_after">
        <observer name="sales_products_observer_checkout_submit_all_after" instance="Magepow\HistoryOrderComment\Observer\OrderComment" />
</event>

then add the file OrderComment.php called in the previous events.xml file

<?php
/**
 * Copyright © 2020 Codazon, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magepow\HistoryOrderComment\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderComment implements ObserverInterface
{
    /**
     * @var \Magento\Sales\Model\Order\Status\HistoryFactory
     */
    protected $historyFactory;

    /**
     * @var \Magento\Sales\Model\OrderFactory
     */
    protected $orderFactory;

    /**
     * @var \Magento\Framework\Json\Helper\Data
     */
    protected $_jsonHelper;

    /**
     * @var \Magento\Framework\Filter\FilterManager
     */
    protected $_filterManager;
    
    
    public function __construct(
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Magento\Framework\Filter\FilterManager $filterManager,
        \Magento\Sales\Model\Order\Status\HistoryFactory $historyFactory,
        \Magento\Sales\Model\OrderFactory $orderFactory,
    ) {
        $this->_jsonHelper = $jsonHelper;
        $this->_filterManager = $filterManager;
        $this->historyFactory = $historyFactory;
        $this->orderFactory = $orderFactory;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {   
        $comment = __('Order Comment: order comment');
       
        $orderId = $observer->getOrder()->getId();
        if ($orderId && (!empty($comment))) {
            $order = $observer->getOrder();
            if ($order->getEntityId()) {
                $status = $order->getStatus();
                $history = $this->historyFactory->create();
                $history->setComment($comment);
                $history->setParentId($orderId);
                $history->setIsVisibleOnFront(1);
                $history->setIsCustomerNotified(0);
                $history->setEntityName('order');
                $history->setStatus($status);
                $history->save();
            }
        }
        
    }
}

This is the result after successful payment

Good luck .