UPSTREAM FIX

magento2-40428: Fatal error when invoice getComments() runs before addComment()

Community fix magento2-40428 merged into magento/magento2 on 2026-03-05, released in 2.4.9; applies cleanly to 33 releases from 2.4.6 to 2.4.8-p5.

Fixes the fatal error when invoice getComments() runs before addComment() edited

Pull request title
Invoice addComment can throw error when Invoice getComments is called first
Pull request
magento/magento2#40428
Issues
#39884 human
Author
@yaroslav-kozar
Merged
2026-03-05
Fixed in
2.4.9
Reported on
2.4.8
Categories
Order/Checkout
Components
magento/module-sales

Labels

Area
Order
Component
Sales
Priority
P3
Severity
—
Reported on (labels)
2.4.8

Issue

Title and steps come from the upstream issue and pull request.

Description

Error: Call to a member function addItem() on array in /var/www/html/vendor/magento/module-sales/Model/Order/Invoice.php:716

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

Taken from the upstream issue.

Error signatures

Code match per tag

Each tag was checked with git apply --check against that tag's files. A clean match means the change applies; it is not a test result. Tags that already contain the fix are marked.

LineCode match per tagTests
2.4.6
2.4.6 clean 2.4.6-p1 clean 2.4.6-p2 clean 2.4.6-p3 clean 2.4.6-p4 clean 2.4.6-p5 clean 2.4.6-p6 clean 2.4.6-p7 clean 2.4.6-p8 clean 2.4.6-p9 clean 2.4.6-p10 clean 2.4.6-p11 clean 2.4.6-p12 clean 2.4.6-p13 clean 2.4.6-p14 clean 2.4.6-p15 clean
2.4.6: no test data 2.4.6-p1: no test data 2.4.6-p2: no test data 2.4.6-p3: no test data 2.4.6-p4: no test data 2.4.6-p5: no test data 2.4.6-p6: no test data 2.4.6-p7: no test data 2.4.6-p8: no test data 2.4.6-p9: no test data 2.4.6-p10: no test data 2.4.6-p11: no test data 2.4.6-p12: no test data 2.4.6-p13: no test data 2.4.6-p14: no test data 2.4.6-p15: no test data
2.4.7
2.4.7 clean 2.4.7-p1 clean 2.4.7-p2 clean 2.4.7-p3 clean 2.4.7-p4 clean 2.4.7-p5 clean 2.4.7-p6 clean 2.4.7-p7 clean 2.4.7-p8 clean 2.4.7-p9 clean 2.4.7-p10 clean
2.4.7: no test data 2.4.7-p1: no test data 2.4.7-p2: no test data 2.4.7-p3: no test data 2.4.7-p4: no test data 2.4.7-p5: no test data 2.4.7-p6: no test data 2.4.7-p7: no test data 2.4.7-p8: no test data 2.4.7-p9: no test data 2.4.7-p10: test files do not apply to this releaseunit: could not run before, could not run after
2.4.8
2.4.8 clean 2.4.8-p1 clean 2.4.8-p2 clean 2.4.8-p3 clean 2.4.8-p4 clean 2.4.8-p5 clean
2.4.8: no test data 2.4.8-p1: no test data 2.4.8-p2: no test data 2.4.8-p3: no test data 2.4.8-p4: no test data 2.4.8-p5: test files do not apply to this releaseunit: could not run before, could not run after
2.4.9
2.4.9 conflictcontains the fix
2.4.9: no test data

Triage

Model @cf/cloudflare/clef. Probability this is a bug fix: 97.9%. Probability it is security relevant: 0.7%.

Show the model's answers and probabilities
QuestionAnswerProbabilitiesConfidence
Change kindbugfixbugfix 96.8%, refactor 1.4%, tests_only 0.6%, docs_only 0.4%, feature 0.4%, dependency 0.3%92.5%
Areacheckoutcheckout 64.5%, admin 12.2%, other 6.9%36.1%
Reported version2.4.82.4.8 45.0%, unspecified 5.8%, 2.4.8-p2 5.5%20.6%
Scope1.05 of 21 60.1%, 2 22.6%, 0 17.3%16.3%
Risk0.49 of 20 60.4%, 1 29.8%, 2 9.8%19.5%
Worth backporting1.59 of 22 68.0%, 1 23.4%, 0 8.6%28.6%

Download

For cweagans/composer-patches, choose a version below and download the bundle. Copy its magento2-40428/ folder into patches/composer/, merge composer.patches.json into composer.json, then run composer install. Test files are always removed; paths are relative to each package root, using the default -p1 level.

Packages (1): magento/module-sales
Bundle README (what the ZIP ships)
# magento2-40428

Community fix merged upstream into magento/magento2, adapted by magento.watch.
This is not a patch published by Adobe.

Pull request: https://github.com/magento/magento2/pull/40428
Issue: https://github.com/magento/magento2/issues/39884
Author: @yaroslav-kozar
Source commit: 0c745525557cffc17da9959a4656ab965272c08f
Modifications: test files and documentation removed, paths rewritten relative to each Composer package.
Licence: OSL-3.0 / AFL-3.0, as the original Magento Open Source code.
Maintainer: Łukasz Bajsarowicz (@lbajsarowicz)

Licence: Magento Open Source code under OSL-3.0 and AFL-3.0. The bundle carries the original author, source commit and the list of modifications.

Sources

Łukasz Bajsarowicz
Built by

Łukasz Bajsarowicz, e-commerce architect

Magento and Adobe Commerce architecture, upgrades, performance and audits for merchants and agencies since 2015; magento.watch is the tooling I use on those projects.

Open source, maintained on weekends.