2

In admin panel Sales->Orders section, while viewing the order, It show order information like below enter image description here

I need to fetch the comment details from core table (sale_order_status_history).and want to show in frontend. Please provide a solution to get comment field details and what are the files needed to achieve this.

For reference the image is below, enter image description here

I have created Block file but it returns empty page

<?php

namespace XXX\SalesOrder\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Model\OrderFactory as OrderFactory;

class Hello extends Template
{

    protected $orderFactory;

    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context, OrderFactory $orderFactory
    )
    {
        $this->orderFactory = $orderFactory;
        parent::__construct($context);
    }

    public function getCollection()
    {

        $order = $this->orderFactory->create()->load('1');


        $orderCommentHistory = $order->getStatusHistoryCollection();

      echo '<pre>';
      print_r($orderCommentHistory);
    }

}

1 Answer 1

8

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

7
  • try to print $orderCommentHistory->getData()
    – Keyur Shah
    Nov 24, 2017 at 9:31
  • Thanks for your response .I had given $orderCommentHostory=$order->getData(); It shows all the order details of id 1 . But it is failed to fetch getStatusHistoryCollection().
    – Jaisa
    Nov 24, 2017 at 9:46
  • Has this order any comments? Please check on backend @Jayashree
    – Keyur Shah
    Nov 24, 2017 at 9:52
  • Yes. Now it works fine. Thnak you soo much. But i need only the comment field. How do i get it?
    – Jaisa
    Nov 24, 2017 at 9:59
  • See my edit, @Jayashree
    – Keyur Shah
    Nov 24, 2017 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.