magento2
magento2 copied to clipboard
MC-13180: Fetching invoice items with \Magento\Sales\Model\Order\Invoice getItems function sometimes returns a collection
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)
- 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)
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.
@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.
@lbajsarowicz hello. Could you suggest any possible solution for that? How to do it appropriately?
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 thanks for your influence. Let me try to understand and try to apply your thought.
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.
Hi, is there any plan to continue working on this and merge it?
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?