Invoice addComment can throw error when Invoice getComments is called first #39884
Open
1 of 5 tasks
Labels
Issue: ready for confirmation
Reported on 2.4.8
Indicates original Magento version for the Issue report.
Triage: Dev.Experience
Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Preconditions and environment
The Invoice model
module-sales/Model/Order/Invoice.php
can return both a collection or an array. If the invoice object has the 'comments' data key, it will just return it, regardless of it being sometimes an array, like when$invoice->getComments()
was called before attempting to add a comment. One reason for this would be that bothgetComments()
andgetCommentsCollection()
are using the sameInvoiceInterface::COMMENTS
data key to get/set data, but using different data types (array vs collection) to populate it.Example to replicate:
So a call to
$invoice->addComment()
is causingError: Call to a member function addItem() on array in /var/www/html/vendor/magento/module-sales/Model/Order/Invoice.php:716
.Comparing
getCommentsCollection()
method between Invoice and CreditMemo models, reveals the different implementation.For the above example, adding
$invoice->unsetData(InvoiceInterface::COMMENTS);
right before theaddComment
call, will make it work, because due to not having the comments key will makegetCommentsCollection()
return a proper collection.Affected versions: 2.4.7, 2.4.8 (I've only checked and confirmed the code issue on these versions)
Steps to reproduce
Implement the following sequence in a custom module:
Expected result
Comment is added to the invoice
Actual result
Error: Call to a member function addItem() on array in /var/www/html/vendor/magento/module-sales/Model/Order/Invoice.php:716
Additional information
No response
Release note
No response
Triage and priority
The text was updated successfully, but these errors were encountered: