When placing an order for a bundle product with child product quantity set to a value greater than 1, the following error message is thrown:
We found an invalid quantity to invoice item "%1".
This happens for any payment method where invoices are automatically created during order creation e.g.
Test bundle product:
-- _General configuration_

-- _Child product_ (ensure Ship Bundle Items is set to "Together")

Admin > Stores > Configuration > Sales > Payment Methods configuration:
-- _PayPal Express Checkout (note: a sandbox account will work as well)_
Enable this solution: Yes
Payment Action: Sale
-- _Zero Subtotal Checkout_
Enabled: Yes
New Order Status: Processing
Automatically Invoice All Items: Yes
Admin > Stores > Configuration > Sales > Shipping Methods configuration:
-- _Free Shipping_
Enabled: Yes


We found an invalid quantity to invoice item "%1". is displayed:



We found an invalid quantity to invoice item "Test". is displayed:
Hi @mystix. Thank you for your report.
To help us process this issue please make sure that you provided the following information:
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
@magento-engcom-team give me 2.3-develop instance - upcoming 2.3.x release
For more details, please, review the Magento Contributor Assistant documentation.
@mystix do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
@magento-engcom-team give me 2.3-develop instance
Hi @mystix. Thank you for your request. I'm working on Magento 2.3-develop instance for you
Hi @mystix, here is your Magento instance.
Admin access: https://i-22743-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.
@mystix do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
- [x] yes
- [ ] no
This bug was introduced in commit 2fb6b3b81ada22e1a172272a7eb5454a50a71a2d in the following 2 private methods from app/code/Magento/Sales/Model/Service/InvoiceService.php:
/**
* Prepare qty to invoice for parent and child products if theirs qty is not specified in initial request.
*
* @param Order $order
* @param array $qtys
* @return array
*/
private function prepareItemsQty(Order $order, array $qtys = [])
{
foreach ($order->getAllItems() as $orderItem) {
if (empty($qtys[$orderItem->getId()])) {
if ($orderItem->getProductType() == Type::TYPE_BUNDLE && !$orderItem->isShipSeparately()) { // herein lies the problem -- order item id is assumed to be > 0
$qtys[$orderItem->getId()] = $orderItem->getQtyOrdered() - $orderItem->getQtyInvoiced();
} else {
continue;
}
}
$this->prepareItemQty($orderItem, $qtys);
}
return $qtys;
}
/**
* Prepare qty to invoice for bundle products
*
* @param \Magento\Sales\Api\Data\OrderItemInterface $orderItem
* @param array $qtys
*/
private function prepareBundleQty(\Magento\Sales\Api\Data\OrderItemInterface $orderItem, &$qtys)
{
if ($orderItem->getProductType() == Type::TYPE_BUNDLE && !$orderItem->isShipSeparately()) { // herein lies the problem -- order item id is assumed to be > 0
foreach ($orderItem->getChildrenItems() as $childItem) {
$bundleSelectionAttributes = $this->serializer->unserialize(
$childItem->getProductOptionByCode('bundle_selection_attributes')
);
$qtys[$childItem->getId()] = $qtys[$orderItem->getId()] * $bundleSelectionAttributes['qty'];
}
}
}
The bug is triggered when the private method prepareItemsQty() is called by the public method prepareInvoice().
ALL existing test cases which call the prepareInvoice() method assume an order has already been successfully created (and hence, all order items belonging to the order have an item id > 0). This assumption is invalid when invoices are created _together_ with orders, as is the case with
To resolve this, the 2 private methods above should also check for a non-zero bundle item id:
/**
* Prepare qty to invoice for parent and child products if theirs qty is not specified in initial request.
*
* @param Order $order
* @param array $qtys
* @return array
*/
private function prepareItemsQty(Order $order, array $qtys = [])
{
foreach ($order->getAllItems() as $orderItem) {
if (empty($qtys[$orderItem->getId()])) {
if ($orderItem->getId() && $orderItem->getProductType() == Type::TYPE_BUNDLE && !$orderItem->isShipSeparately()) { // bugfix
$qtys[$orderItem->getId()] = $orderItem->getQtyOrdered() - $orderItem->getQtyInvoiced();
} else {
continue;
}
}
$this->prepareItemQty($orderItem, $qtys);
}
return $qtys;
}
/**
* Prepare qty to invoice for bundle products
*
* @param \Magento\Sales\Api\Data\OrderItemInterface $orderItem
* @param array $qtys
*/
private function prepareBundleQty(\Magento\Sales\Api\Data\OrderItemInterface $orderItem, &$qtys)
{
if ($orderItem->getId() && $orderItem->getProductType() == Type::TYPE_BUNDLE && !$orderItem->isShipSeparately()) { // bugfix
foreach ($orderItem->getChildrenItems() as $childItem) {
$bundleSelectionAttributes = $this->serializer->unserialize(
$childItem->getProductOptionByCode('bundle_selection_attributes')
);
$qtys[$childItem->getId()] = $qtys[$orderItem->getId()] * $bundleSelectionAttributes['qty'];
}
}
}
Hi @AlexWorking. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.
[ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Verify that the issue is reproducible on 2.2-develop branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x
[ ] 6. Add label Issue: Confirmed once verification is complete.
[ ] 7. Make sure that automatic system confirms that report has been added to the backlog.
:white_check_mark: Confirmed by @AlexWorking
Thank you for verifying the issue. Based on the provided information internal tickets MAGETWO-99568 were created
Issue Available: @AlexWorking, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
@mystix just wanted to say thank you for the detailed report and suggested fix - we've hit this exact issue on a live site (recently upgraded to 2.3.1) with Stripe also.
Hi @GovindaSharma. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. Verify that the issue is reproducible on 2.2-develop branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x
[ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.
I encountered this same issue using Braintree gateway with "_Enable PayPal through Braintree_" which I believe is a PayPal Flow transaction, slightly different than Express Checkout but still broken without @mystix fix.
@mystix thank you for posting the fix. I implemented on and M2.3.1 instance this morning. The InvoiceService.php source code was a bit different than what you posted, however, adding $orderItem->getId() && to the beginning of the if statements worked the same.
I encountered this same issue with "Enable PayPal through Braintree" which I believe is a PayPal Flow transaction, slightly different that Express Checkout but still broken without @mystix fix.
@file : vendor/magento/module-sales/Model/Service/InvoiceService.php
@lines: 179-206
@code : code added @line 190 $orderItem->getId() &&
/**
* Prepare qty to invoice for parent and child products if theirs qty is not specified in initial request.
*
* @param Order $order
* @param array $qtys
* @return array
*/
private function prepareItemsQty(Order $order, array $qtys = [])
{
foreach ($order->getAllItems() as $orderItem) {
if (empty($qtys[$orderItem->getId()])) {
if ($orderItem->getId() && $orderItem->getProductType() == Type::TYPE_BUNDLE && !$orderItem->isShipSeparately()) {
$qtys[$orderItem->getId()] = $orderItem->getQtyOrdered() - $orderItem->getQtyInvoiced();
} else {
$parentItem = $orderItem->getParentItem();
$parentItemId = $parentItem ? $parentItem->getId() : null;
if ($parentItemId && isset($qtys[$parentItemId])) {
$qtys[$orderItem->getId()] = $qtys[$parentItemId];
}
continue;
}
}
$this->prepareItemQty($orderItem, $qtys);
}
return $qtys;
}
@file : vendor/magento/module-sales/Model/Service/InvoiceService.php
@lines: 240-258
@code : code added @line 249 $orderItem->getId() &&
/**
* Prepare qty to invoice for bundle products
*
* @param \Magento\Sales\Api\Data\OrderItemInterface $orderItem
* @param array $qtys
*/
private function prepareBundleQty(\Magento\Sales\Api\Data\OrderItemInterface $orderItem, &$qtys)
{
if ($orderItem->getId() && $orderItem->getProductType() == Type::TYPE_BUNDLE && !$orderItem->isShipSeparately()) {
foreach ($orderItem->getChildrenItems() as $childItem) {
$bundleSelectionAttributes = $childItem->getProductOptionByCode('bundle_selection_attributes');
if (is_string($bundleSelectionAttributes)) {
$bundleSelectionAttributes = $this->serializer->unserialize($bundleSelectionAttributes);
}
$qtys[$childItem->getId()] = $qtys[$orderItem->getId()] * $bundleSelectionAttributes['qty'];
}
}
}
I would like to thank @mystix for providing the fix I needed to solve this issue! Hopefully Magento can come out with a patch or update soon to properly resolve this problem.
Hi @mystix, @GovindaSharma.
Thank you for your report and collaboration!
The related internal Jira ticket MAGETWO-99568 was closed as non-reproducible in 2.3-develop.
It means that Magento team either unable to reproduce this issue using provided _Steps to Reproduce_ from the _Description section_ on clean Magento instance or the issue has been already fixed in the scope of other tasks.
But if you still run into this problem please update or provide additional information/steps/preconditions in the _Description section_ and reopen this issue.
I am getting this issue in magento 2.3.2. I assume magento still didn't fix this
I am getting this issue in magento 2.3.2. I assume magento still didn't fix this
I ran both my test cases, and double-checked my existing codebase just to make sure I'm running the code from M2.3.2 -- I can confirm I no longer see this problem in M2.3.2.
Try monkey-patching your codebase with the fix I suggested to see if it helps resolve your issue?
https://github.com/magento/magento2/issues/22743#issuecomment-489509956
@mystix Thanks for the reply mate. I am getting a lot of complaints from customers so, definitely, I am getting the error :(
I will try to add the fix you have mentioned mate.
We are also having this problem on a live site, when will it be fixed? Magento 2.3.2
@mystix I have added your fix and it seems working now :) . thanks for the fix mate. you are a lifesaver.
@craigcarnell Try @mystix fix. it worked for me.
Fixed it for me, too & does not seems to be fixed in 2.3.3
same issue here, magento 2.3.2 (cannot upgrade right now because it is production env). patched as @mystix suggested, now works. I hope it will be fixed in future versions, I don't want to keep custom patches in the code.
Most helpful comment
This bug was introduced in commit 2fb6b3b81ada22e1a172272a7eb5454a50a71a2d in the following 2 private methods from app/code/Magento/Sales/Model/Service/InvoiceService.php:
The bug is triggered when the private method
prepareItemsQty()is called by the public methodprepareInvoice().ALL existing test cases which call the
prepareInvoice()method assume an order has already been successfully created (and hence, all order items belonging to the order have an item id > 0). This assumption is invalid when invoices are created _together_ with orders, as is the case withTo resolve this, the 2 private methods above should also check for a non-zero bundle item id: