Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2.3.1: Unable to place order for bundle when child quantity > 1 #22743

Closed
mystix opened this issue May 6, 2019 · 20 comments
Closed

M2.3.1: Unable to place order for bundle when child quantity > 1 #22743

mystix opened this issue May 6, 2019 · 20 comments
Assignees
Labels
Component: Bundle Component: Paypal Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@mystix
Copy link

mystix commented May 6, 2019

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.

  • PayPal Express Checkout + Payment Action = Sale
  • Zero Subtotal Checkout + Automatically Invoice All Items = Yes

Preconditions (*)

  1. Magento 2.3.1

  2. Test bundle product:
    -- General configuration
    Screenshot 2019-05-06 at 12 10 39 PM
    -- Child product (ensure Ship Bundle Items is set to "Together")
    Screenshot 2019-05-05 at 2 31 25 PM

  3. 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

  4. Admin > Stores > Configuration > Sales > Shipping Methods configuration:
    -- Free Shipping
    Enabled: Yes


Steps to reproduce (as customer) (*)

  1. Add Test bundle to the shopping cart, making sure to set the child quantity to a value > 1:
    Screenshot 2019-05-06 at 12 26 32 PM
  2. Proceed to Checkout
  3. Select PayPal Express Checkout as the payment method
  4. Click Continue to PayPal
  5. Make payment on PayPal

Expected result (*)

  1. Customer is redirected back to Magento's Order Success page
    Screenshot 2019-05-06 at 12 39 03 PM

Actual result (*)

  1. Error message We found an invalid quantity to invoice item "%1". is displayed:
    Screenshot 2019-05-06 at 12 41 58 PM

Steps to reproduce (as admin) (*)

  1. Create a new order from Admin > Sales > Orders
  2. Add the Test bundle to the order, making sure the child quantity is set to a value greater than 1:
    Screenshot 2019-05-06 at 12 46 11 PM
  3. Set a custom price of 0 for the Test bundle
    Screenshot 2019-05-06 at 12 47 42 PM
  4. Under Shipping Method, select Free Shipping
  5. Payment Method section should now display "No Payment Information Required", and the order total should be 0:
    Screenshot 2019-05-06 at 12 56 14 PM
  6. Click Submit Order

Expected result (*)

  1. New zero subtotal order is successfully created
  2. Invoice is automatically created for this zero subtotal order

Actual result (*)

  1. Error message We found an invalid quantity to invoice item "Test". is displayed:
    Screenshot 2019-05-06 at 1 06 53 PM
@m2-assistant
Copy link

m2-assistant bot commented May 6, 2019

Hi @mystix. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

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?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label May 6, 2019
@mystix
Copy link
Author

mystix commented May 6, 2019

@magento-engcom-team give me 2.3-develop instance

@magento-engcom-team
Copy link
Contributor

Hi @mystix. Thank you for your request. I'm working on Magento 2.3-develop instance for you

@magento-engcom-team
Copy link
Contributor

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
Copy link
Author

mystix commented May 6, 2019

@mystix do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@mystix
Copy link
Author

mystix commented May 6, 2019

This bug was introduced in commit 2fb6b3b 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

  • PayPal Express Checkout + payment action = "Sale"
  • Zero Subtotal Checkout + automatically invoice all items = "Yes"

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'];
            }
        }
    }

@AlexWorking AlexWorking self-assigned this May 6, 2019
@m2-assistant
Copy link

m2-assistant bot commented May 6, 2019

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label 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 branch

    Details- 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.

@AlexWorking AlexWorking added Component: Bundle Component: Paypal Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels May 6, 2019
@ghost ghost unassigned AlexWorking May 6, 2019
@magento-engcom-team
Copy link
Contributor

✅ 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.

@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label May 6, 2019
@davidwindell
Copy link
Contributor

@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.

@GovindaSharma GovindaSharma self-assigned this May 10, 2019
@m2-assistant
Copy link

m2-assistant bot commented May 10, 2019

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:

  • 1. Add/Edit 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 branch

    Details- 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.

@lmstephenson
Copy link

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'];
            }
        }
    }

@ernesthernandez ernesthernandez mentioned this issue May 20, 2019
4 tasks
@DanielRiezebos
Copy link

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.

@magento-engcom-team magento-engcom-team added Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch and removed Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Aug 6, 2019
@magento-engcom-team
Copy link
Contributor

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.

@bscbodyscience
Copy link

I am getting this issue in magento 2.3.2. I assume magento still didn't fix this

@mystix
Copy link
Author

mystix commented Aug 15, 2019

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?
#22743 (comment)

@bscbodyscience
Copy link

@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.

@csdougliss
Copy link
Contributor

We are also having this problem on a live site, when will it be fixed? Magento 2.3.2

@bscbodyscience
Copy link

@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.

@develth
Copy link

develth commented Oct 16, 2019

Fixed it for me, too & does not seems to be fixed in 2.3.3

@cits74
Copy link

cits74 commented Nov 12, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Bundle Component: Paypal Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests