---
title: "How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2?"
url: "https://magecomp.com/blog/add-comments-to-order-programmatically-after-place-order-from-frontend-magento-2/"
date: "2023-08-14T18:16:15+00:00"
modified: "2025-02-13T13:01:57+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 450
reading_time: "3 min read"
summary: "Hello Magento Friends,"
description: "To add comments to an order programmatically after placing an order from the front end in Magento 2, check out the guide"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Best Magento 2 Instagram Feed Extensions"
    url: "https://magecomp.com/blog/best-magento-2-instagram-feed-extensions/"
  - title: "Magento 2: How Resize Image of Specific Products Based on Admin Configuration?"
    url: "https://magecomp.com/blog/magento-2-resize-image-of-specific-products-based-on-admin-configuration/"
  - title: "How to Programmatically Add Option Values to a Dropdown Attribute in Magento 2?"
    url: "https://magecomp.com/blog/programmatically-add-option-values-to-dropdown-attribute-magento-2/"
---

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

_Published: August 14, 2023_  
_Author: Dhiren Vasoya_  

![How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2](https://magecomp.com/blog/wp-content/uploads/2023/08/How-to-Add-Comments-to-Order-Programmatically-After-Place-Order-from-Frontend-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

Today, I will explain How to Add Comments to Order Programmatically After Place Order from Frontend in Magento 2.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

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="VendorExtensionObserverOrderObserver"/>
    </event>
</config>
```

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2023/04/Magento-Malware-Removal-CTA-1.webp)](https://magecomp.com/magento-malware-removal.html)

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

namespace VendorExtensionObserver;

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoSalesApiOrderRepositoryInterface;
use MagentoSalesModelOrderStatusHistoryFactory;

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(MagentoSalesModelOrder::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 page, which 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](https://magecomp.com/blog/wp-content/uploads/2023/08/Add-order-comments.png)

## 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**](https://magecomp.com/magento-2-order-comments.html) to allow customers to add a comment at the checkout page and manage it accordingly from the backend.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

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


---

_View the original post at: [https://magecomp.com/blog/add-comments-to-order-programmatically-after-place-order-from-frontend-magento-2/](https://magecomp.com/blog/add-comments-to-order-programmatically-after-place-order-from-frontend-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-05-22 05:03:04 UTC_  
