magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

MC-13180: Fetching invoice items with \Magento\Sales\Model\Order\Invoice getItems function sometimes returns a collection

Open AleksLi opened this issue 4 years ago • 8 comments

Description (*)

Created an example of the solution. I think something like this would fix the issue since it is using a lot in the process of adding Item.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes magento/magento2#13180: Fetching invoice items with \Magento\Sales\Model\Order\Invoice getItems function sometimes returns a collection

Manual testing scenarios (*)

Well explained in the description of #13180

Questions or comments

Contribution checklist (*)

  • [ ] Pull request has a meaningful description of its purpose
  • [ ] All commits are accompanied by meaningful commit messages
  • [ ] All new or changed code is covered with unit/integration tests (if applicable)
  • [ ] All automated tests passed successfully (all builds are green)

AleksLi avatar Mar 18 '20 20:03 AleksLi

Hi @AleksLi. Thank you for your contribution Here is some useful tips how you can test your changes using Magento test environment. Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

m2-assistant[bot] avatar Mar 18 '20 20:03 m2-assistant[bot]

@lbajsarowicz I was trying to resolve this another way but the logic here was constructed to return Magento\Sales\Model\ResourceModel\Order\Invoice\Item\Collection instead of getting InvoiceItemInterface as it says as the return type in the getItems() method.

Simply returning an array as that's written in the code won't make it work. For example, we will get an error trying to addItem on \Magento\Sales\Model\Order\Invoice::getItemsCollection line 553.

I debugged the issue for several hours trying to resolve it in an easy way.

Of course, method getItems() should return InvoiceItemInterface[] as it expected, but we should change everything connected to that. As you know that would affect other modules I guess and affect 3rd party modules.

Your suggestion?

This example below won't work, just for example. m2git  ~:Public:projects:m2git  -  :app:code:Magento:Sales:Model:Order:Invoice php 2020-03-21 21-26-53

AleksLi avatar Mar 21 '20 20:03 AleksLi

@lbajsarowicz hello. Could you suggest any possible solution for that? How to do it appropriately?

AleksLi avatar Apr 04 '20 18:04 AleksLi

Changing getItems() to return hashmap and getItemCollection() to return collection instead of getItems() would solve your concern. It looks like getItemCollection() is the only client that depends on getItems() returning Collection. Everyone else seems to depend on iterable and countable.

This will, of course, be a breaking change, but only for people who depended on the unpublished interface (hacked).

Anything else I missed?

antonkril avatar Apr 04 '20 22:04 antonkril

@antonkril thanks for your influence. Let me try to understand and try to apply your thought.

AleksLi avatar Apr 07 '20 19:04 AleksLi

Hi @AleksLi I have applied the latest changes in this PR and notice an issue when creating an invoice from at least the backend:

1 exception(s): Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in vendor/magento/module-tax/Helper/Data.php on line 731

Apparently this change is breaking this code: foreach ($salesItem->getItems() as $item) {

This is because $this->getId in the method getItems() returns null at this point: public function getItems() { if ($this->getData(InvoiceInterface::ITEMS) === null && $this->getId()) {

A workaround could be calling getAllItems or getItemsCollection, but I'm uncertain sure of the impact. Also which other areas might also be affected by this? This all feels a bit like a can of worms.

igorwulff avatar Sep 01 '20 08:09 igorwulff

Hi, is there any plan to continue working on this and merge it?

lfolco avatar Apr 26 '22 18:04 lfolco

Hi, I also applied the change in this PR on my Magento 2.4.3-p1 instance. I discovered that after I create a new invoice successfully, there is no invoice item exists. Is anyone have any idea about how to fix it?

kenit avatar Aug 11 '22 07:08 kenit