Skip to content

Invoice addComment can throw error when Invoice getComments is called first #39884

Open
@mihaimatei

Description

@mihaimatei

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 both getComments() and getCommentsCollection() are using the same InvoiceInterface::COMMENTS data key to get/set data, but using different data types (array vs collection) to populate it.
Example to replicate:

$invoice = $this->invoiceRepository->get($invoiceId);
$commentsCollection = $invoice->getComments();
$invoice->addComment('whatever', false, false);

So a call to $invoice->addComment() is causing Error: 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 the addComment call, will make it work, because due to not having the comments key will make getCommentsCollection() 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:

$invoice = $this->invoiceRepository->get($invoiceId);

$commentsCollection = $invoice->getComments();    
// will set the invoice object data key InvoiceInterface::COMMENTS with an array value

$invoice->addComment('whatever', false, false);
// will further call getCommentsCollection() which will return the array from data key InvoiceInterface::COMMENTS
//   on which ->addItem() will be called, causing the error

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

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: OrderComponent: SalesIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.Reported on 2.4.8Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    Status

    Ready for Development

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions