Load specific order status comment by the order status comment ID in Magento 2 to retrieve status history data.
A helpful method is public function get($commentId) from the OrderStatusHistoryRepository Interface of the Sales Module.
Be sure you have a valid comment id to get order status comment data,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php namespace Jesadiya\LoadOrderStatus\Model; use Magento\Sales\Api\Data\OrderStatusHistoryInterface; use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface; use Psr\Log\LoggerInterface; class LoadOrderStatus { /** * @var OrderStatusHistoryRepositoryInterface */ private $historyRepository; /** * @var LoggerInterface */ private $logger; public function __construct( OrderStatusHistoryRepositoryInterface $historyRepository, LoggerInterface $logger ) { $this->historyRepository = $historyRepository; $this->logger = $logger; } /** * load sales order status history by comment id * * @param int $commentId * @return OrderStatusHistoryInterface|null */ public function loadComment(int $commentId): ?OrderStatusHistoryInterface { $historyCommment = null; try { $historyCommment = $this->historyRepository->get($commentId); } catch (\Exception $exception) { $this->logger->critical($exception->getMessage()); } } } |
Call method with argument comment id of the given order status history.
1 2 | $commentId = 1; $orderStatusComment = $this->loadComment($commentId); |
Output: Order status history interface methods.
\Magento\Sales\Api\Data\OrderStatusHistoryInterface