How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2

How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2

Hello Magento Friends,

Today, I will explain How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2.

In Magento 2, it might be really helpful sometimes, especially after placing an order, to add comments or some notes for further instructions or clarifications or even for communication between some people internally.

Let’s see How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2.

Steps to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2:

Step 1: Create a file at app\code\Vendor\Extension\etc\events.xml and add the below code.

<?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="checkout_onepage_controller_success_action">
        <observer name="order_observer" instance="\Vendor\Extension\Observer\OrderObserver"/>
    </event>
</config>

Step 2: Create a file at app\code\Vendor\Extension\Observer\OrderObserver.php and add the below code.

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Status\HistoryFactory;

class OrderObserver implements ObserverInterface
{
     protected $orderRepository;

     protected $orderStatusHistoryFactory;
    
    public function __construct(
        OrderRepositoryInterface $orderRepository,
        HistoryFactory $orderStatusHistoryFactory
    )
    {
        $this->orderRepository = $orderRepository;
        $this->orderStatusHistoryFactory = $orderStatusHistoryFactory;
    }

    public function execute(Observer $observer)
    {
       $order = $observer->getEvent()->getOrder();
        $comment = "Your comment goes here.";
        $this->addCommentToOrder($order->getId(), $comment);
        return $this;
    }

    public function addCommentToOrder($orderId, $comment)
    {
        try {
        $order = $this->orderRepository->get($orderId);
        $statusHistory = $this->orderStatusHistoryFactory->create();
        $statusHistory->setComment(
                       __('Comment: %1.', $comment)
                   );
        $statusHistory->setEntityName(\Magento\Sales\Model\Order::ENTITY);
        $statusHistory->setStatus($order->getStatus());
        $statusHistory->setIsCustomerNotified(false)->setIsVisibleOnFront(false);
        $order->addStatusHistory($statusHistory);
        $this->orderRepository->save($order);
        } catch (\Exception $e) {
            throw new LocalizedException(__("Failed to add the comment to the order: %1", $e->getMessage()));
        }
    } 
}

Output:

Visit the front end’s order view pagewhich should now feature a comment form. Add a comment there and then click the Submit Comment button. The comment should be automatically added to the status history of that order

Add order comments

Conclusion:

By following these steps, you can enhance your store‘s communication and organization by activating the feature of adding a comment or note to the order. This functionality can really be helpful in keeping a record of special instructions, updates, or any other pertinent information associated with each order. You can also use the Magento 2 Order Comments Extension to allow customers to add a comment at the checkout page and manage it accordingly from the backend.

Forward this tutorial to your friends and keep in touch with us for Magento solutions.

Happy Coding!

Previous Article

What is Shopify Plus and How Much Does it Cost?

Next Article

How to Add Product Variants in Shopify?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨