{"data":{"id":"magento2-40428","source":"github-pr","sourceRef":"magento/magento2#40428","title":"Invoice addComment can throw error when Invoice getComments is called first","pr":{"number":40428,"url":"https://github.com/magento/magento2/pull/40428","author":"yaroslav-kozar","mergedAt":"2026-03-05T14:41:21Z","mergeCommit":"3e216bf61018c715bbc1337d1ab2cdb27ad85a98","headCommit":"0c745525557cffc17da9959a4656ab965272c08f","baseRef":"2.4-develop","diffSha256":"3dc331ef83384f27813875af91e7728152784168d8da80e60e6058b2e299b6ed"},"issues":[{"number":39884,"url":"https://github.com/magento/magento2/issues/39884","title":"Invoice addComment can throw error when Invoice getComments is called first","labels":["Area: Order","Component: Sales","Issue: Confirmed","Priority: P3","Progress: PR Created","Progress: done","Reported on 2.4.8","Reproduced on 2.4.x","Triage: Dev.Experience"],"kind":"human"}],"fixedIn":"2.4.9","containingTags":["2.4.9"],"reportedOn":"2.4.8","codeMatch":{"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.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.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.9":"conflict"},"affectedVersions":["2.4.6","2.4.6-p1","2.4.6-p2","2.4.6-p3","2.4.6-p4","2.4.6-p5","2.4.6-p6","2.4.6-p7","2.4.6-p8","2.4.6-p9","2.4.6-p10","2.4.6-p11","2.4.6-p12","2.4.6-p13","2.4.6-p14","2.4.6-p15","2.4.7","2.4.7-p1","2.4.7-p2","2.4.7-p3","2.4.7-p4","2.4.7-p5","2.4.7-p6","2.4.7-p7","2.4.7-p8","2.4.7-p9","2.4.7-p10","2.4.8","2.4.8-p1","2.4.8-p2","2.4.8-p3","2.4.8-p4","2.4.8-p5"],"components":["magento/module-sales"],"files":[{"path":"app/code/Magento/Sales/Model/Order/Invoice.php","change":"modified","package":"magento/module-sales"}],"stripped":{"tests":["app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceTest.php"],"docs":[],"outsideCode":[]},"linesChanged":48,"mergeBatched":false,"excluded":null,"sections":{"description":"`Error: Call to a member function addItem() on array in /var/www/html/vendor/magento/module-sales/Model/Order/Invoice.php:716`","stepsToReproduce":"Implement the following sequence in a custom module:\n```\n$invoice = $this->invoiceRepository->get($invoiceId);\n\n$commentsCollection = $invoice->getComments();    \n// will set the invoice object data key InvoiceInterface::COMMENTS with an array value\n\n$invoice->addComment('whatever', false, false);\n// will further call getCommentsCollection() which will return the array from data key InvoiceInterface::COMMENTS\n//   on which ->addItem() will be called, causing the error\n```","expectedResult":"Comment is added to the invoice","actualResult":"`Error: Call to a member function addItem() on array in /var/www/html/vendor/magento/module-sales/Model/Order/Invoice.php:716`","source":"issue"},"signatures":["So a call to `$invoice->addComment()` is causing `Error: Call to a member function addItem() on array in `.","`Error: Call to a member function addItem() on array in `"],"labels":{"area":["Order"],"component":["Sales"],"priority":"P3","severity":null,"reportedOn":["2.4.8"]},"categories":["Order/Checkout"],"triage":{"model":"@cf/cloudflare/clef","requestHash":"9762bd37d98e28857233d3ee5a8044778bdb0e1ea168b6f32a92267095d6da4b","isBugfix":0.9793,"changeKind":{"choice":"bugfix","probabilities":{"bugfix":0.9683,"feature":0.0041,"refactor":0.0142,"tests_only":0.0059,"docs_only":0.0042,"dependency":0.0033},"confidence":0.9254},"scope":{"score":1.0536,"probabilities":{"0":0.1728,"1":0.6008,"2":0.2264},"confidence":0.1631},"risk":{"score":0.4937,"probabilities":{"0":0.6041,"1":0.2981,"2":0.0978},"confidence":0.195},"area":{"choice":"checkout","probabilities":{"catalog":0.0286,"checkout":0.6449,"customer":0.0367,"admin":0.1223,"graphql_api":0.0343,"framework":0.0417,"frontend":0.0228,"other":0.0687},"confidence":0.3614},"securityRelevant":0.0069,"reportedVersion":{"choice":"2.4.8","probabilities":{"2.4.0":0.0023,"2.4.0-p1":0.0023,"2.4.1":0.0022,"2.4.1-p1":0.002,"2.4.2":0.0024,"2.4.2-p1":0.0023,"2.4.2-p2":0.0028,"2.4.3":0.0028,"2.4.3-p1":0.0032,"2.4.3-p2":0.0038,"2.4.3-p3":0.0029,"2.4.4":0.0031,"2.4.4-p1":0.0027,"2.4.4-p10":0.0028,"2.4.4-p11":0.0032,"2.4.4-p12":0.0027,"2.4.4-p13":0.0028,"2.4.4-p14":0.0032,"2.4.4-p15":0.0029,"2.4.4-p16":0.0031,"2.4.4-p17":0.0029,"2.4.4-p18":0.003,"2.4.4-p2":0.002,"2.4.4-p3":0.0021,"2.4.4-p4":0.0026,"2.4.4-p5":0.0027,"2.4.4-p6":0.003,"2.4.4-p7":0.003,"2.4.4-p8":0.0039,"2.4.4-p9":0.0025,"2.4.5":0.0039,"2.4.5-p1":0.0026,"2.4.5-p10":0.0024,"2.4.5-p11":0.0033,"2.4.5-p12":0.0035,"2.4.5-p13":0.0032,"2.4.5-p14":0.0033,"2.4.5-p15":0.0025,"2.4.5-p16":0.0031,"2.4.5-p17":0.0027,"2.4.5-p2":0.0022,"2.4.5-p3":0.0022,"2.4.5-p4":0.0025,"2.4.5-p5":0.003,"2.4.5-p6":0.0032,"2.4.5-p7":0.003,"2.4.5-p8":0.0036,"2.4.5-p9":0.0022,"2.4.6":0.0069,"2.4.6-p1":0.0057,"2.4.6-p10":0.0058,"2.4.6-p11":0.0067,"2.4.6-p12":0.0062,"2.4.6-p13":0.0055,"2.4.6-p14":0.0054,"2.4.6-p15":0.0045,"2.4.6-p2":0.0025,"2.4.6-p3":0.0038,"2.4.6-p4":0.0051,"2.4.6-p5":0.0044,"2.4.6-p6":0.0051,"2.4.6-p7":0.0048,"2.4.6-p8":0.0053,"2.4.6-p9":0.0027,"2.4.7":0.0461,"2.4.7-p1":0.0095,"2.4.7-p10":0.0058,"2.4.7-p2":0.0037,"2.4.7-p3":0.0047,"2.4.7-p4":0.0057,"2.4.7-p5":0.0052,"2.4.7-p6":0.0065,"2.4.7-p7":0.0057,"2.4.7-p8":0.0072,"2.4.7-p9":0.0042,"2.4.8":0.4504,"2.4.8-p1":0.0436,"2.4.8-p2":0.0549,"2.4.8-p3":0.0251,"2.4.8-p4":0.0217,"2.4.8-p5":0.019,"2.4.9":0.0072,"unspecified":0.0578},"confidence":0.2063},"backportWorthy":{"score":1.5931,"probabilities":{"0":0.0865,"1":0.234,"2":0.6795},"confidence":0.286}},"curated":{"title":"Fixes the fatal error when invoice getComments() runs before addComment()","description":"Calling getComments() before addComment() on an invoice stores an array in the comments data, so addComment() throws Error: Call to a member function addItem() on array. In Magento\\Sales\\Model\\Order\\Invoice, getCommentsCollection() now creates a new collection when the stored data is not a Collection instance, and getComments() returns an array even if a Collection is stored.","categories":["Order/Checkout"],"author":"claude-code/opus-5.5","date":"2026-10-06","reviewedBy":null},"tests":{"2.4.8-p5":{"before":"not-runnable","after":"error","adapted":false,"runAt":"2026-10-06T09:20:16.686Z","releaseCommit":"870a22c63d9d9b68fa3297962e2d5d5841115814","suites":{"unit":{"before":"not-runnable","after":"error","runAt":"2026-10-06T09:20:16.686Z"}}},"2.4.7-p10":{"before":"not-runnable","after":"error","adapted":false,"runAt":"2026-10-06T09:29:30.261Z","releaseCommit":"72561bf80652f57cc642a03e2c9a51d74a285b14","suites":{"unit":{"before":"not-runnable","after":"error","runAt":"2026-10-06T09:29:30.261Z"}}}}},"_documentation":"https://magento.watch/api","_description":"Upstream fix magento2-40428 details"}