Back to Top

Add custom comment programmatically to order in Magento 2

Updated 1 September 2018

In this Article, I am going to explain how to add the custom comment in Order.
Some time, we have a need to add note to order so that we can not miss any thing for the particular order.
So this is the simplest solution to add note for order in the form of comments.

class AddCommentToOrder extends [AbstractController] {
        * @var \Magento\Sales\Model\OrderFactory

        protected $_orderFactory;

        // custom comment text
        const ORDER_ID = '1';
        const ORDER_CUSTOM_COMMENT_TEXT = 'This is a custom Comment';

        public function __construct(
            \Magento\Sales\Model\OrderFactory $orderFactory,
            $this->_orderFactory                       = $orderFactory;
        public function execute()  {
           $orderId = self::ORDER_ID;

           if (isset($orderId)) {

             $order = $this->_orderFactory->create()->load($orderId);
             $history = $order->addStatusHistoryComment(self::ORDER_CUSTOM_COMMENT_TEXT);


Note : self::ORDER_CUSTOM_COMMENT_TEXT, self::ORDER_ID,  I have used these constant variables but you can pass the dynamic value for both orderId and comment message.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

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

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home