Invoice comments can be added by the store admin from the backend for the invoice query. Admin can notify about the comments to the customers by email. It can also be displayed in the frontend invoice section.
Magento\Sales\Api\InvoiceManagementInterface is the class that is responsible to fetch a list of comments from the invoice.
<?php namespace MageDelight\Comments\Model; use Exception; use Magento\Sales\Api\InvoiceManagementInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\InvoiceCommentSearchResultInterface; class Comments { /** * @var InvoiceManagementInterface */ private $invoiceService; public function __construct( InvoiceManagementInterface $invoiceService ) { $this->invoiceService = $invoiceService; } /** * @param int $invoiceId * @return InvoiceCommentSearchResultInterface */ public function getComments($invoiceId): ?InvoiceCommentSearchResultInterface { $invoiceComments = null; try { $invoiceComments = $this->invoiceService->getCommentsList($invoiceId); } catch (NoSuchEntityException $exception) { throw new Exception($exception->getMessage()); } return $invoiceComments; } }
Call the method with a required parameter. Don’t forget to assign an Invoice Id, not the order id. Now verify if the Invoice has Comments available or not by the getTotalCount().
$invoiceId = 1; // Invoice id $invoiceComments = $this->getComments($invoiceId); if ($invoiceComments->getTotalCount()) { foreach($invoiceComment as $comment) { echo "<pre>";print_r($comment->debug()); } }
And it is done.
Also read: How to Create Invoice Programmatically in Magento 2?
Hope we covered everything related to invoicing comments list programmatically in Magento 2. If we missed out anything or you get stuck, feel free to reach us out.