Magento 2 – Add Comments Programmatically for Order in Admin Panel

magento2.2PHPsales-order

In admin panel Sales->Orders section, while viewing the order, It show order information like below,How to add comment programatically…. And it needs to save (sale_order_status_history) table Suggest a solution..
enter image description here

Best Answer

$orderId = $orderId;
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
    $order->addStatusHistoryComment('This comment is programatically added to last order in this Magento setup');
    $order->save();

you can also user magento repo object as well.

Related Topic