I ran into a Magento 2.4.8 core issue where calling getComments() on an invoice before addComment() can trigger a fatal error.
Update: I reported it as GitHub issue #39884 on May 2, 2025, and Magento later merged a fix for it on March 5, 2026.
The problem
This sequence can fail:
$invoice = $this->invoiceRepository->get($invoiceId);
$comments = $invoice->getComments();
$invoice->addComment('whatever', false, false);Instead of adding the invoice comment, Magento can throw Error: Call to a member function addItem() on array
The issue was reported against Magento 2.4.8 and reproduced on 2.4.x. In my original report I also noted the same code issue on 2.4.7.
Why it happens
The root cause is inconsistent handling of the invoice comments data.
getComments() and getCommentsCollection() both use the same InvoiceInterface::COMMENTS data key, but they may populate it with different types. After getComments() runs, the invoice object may hold an array under that key. Later, addComment() eventually works with that same key as if it were a collection, and this leads to the fatal call on addItem().
Temporary workaround
If you need a safe workaround before upgrading, this avoids the problem:
$invoice = $this->invoiceRepository->get($invoiceId);
$comments = $invoice->getComments();
$invoice->unsetData(\Magento\Sales\Api\Data\InvoiceInterface::COMMENTS);
$invoice->addComment('whatever', false, false);Unsetting the COMMENTS data forces Magento to rebuild the comments collection properly before adding the new comment.
Update: Upstream fix
Fix for this issue was implemented in pull request #40428. The change does a few important things:
- updates getCommentsCollection() to handle existing array data correctly
- refactors getComments() to consistently return an array
- adds reload=true in addComment() to keep comment data consistent
- adds a unit test to cover the scenario and reduce regression risk
The fix was merged into 2.4-develop on March 5, 2026.




