Magento 2 – Get Order Comments from Order ID

Posted on March 29, 2021 by jamie

6

If you have $order object then you can easily get the order comment history by,

$order->getStatusHistoryCollection()

If you don’t have $order object then you can use like this,

use Magento\Sales\Model\OrderFactory as OrderFactory;

protected $orderFactory;


public function __construct(
    ...
    OrderFactory $orderFactory  
    ....
) {
    ....
    $this->orderFactory = $orderFactory;    
    ....
}


$order = $this->orderFactory->create()->load('YOUR_ORDER_ID');
$orderCommentHostory=$order->getStatusHistoryCollection();

EDIT If you need order comment then please try this,

$orderComment = [];
foreach ($order->getStatusHistoryCollection() as $status) {
    if ($status->getComment()) {
        $orderComment[] = $status->getComment();
    }
}

At the end, you have all order comments in $orderComment