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

[BUG] Adding another project to cart fails if there's one in the cart already with qty = 1 #9

Open
kolaente opened this issue Nov 8, 2021 · 12 comments

Comments

@kolaente
Copy link

kolaente commented Nov 8, 2021

Adding a product (Let's call it "product a") to cart with qty = 1 works fine. But when I then want to add another product ("product B") to the cart with some other qty, adding that second product fails with an error message "The requested qty is not available".

This is the quantity settings of product A:
image

Product B has some some quantity > 1. Adding it individually (without Product A already in cart) works fine.

I dug into it a bit and it looks like this happens because this module saves the cart. That tries to set the quantity again for product A which won't work because it does not have enough quantity available at this point. Not quite sure if this is a core issue or only related to this module.

@kolaente
Copy link
Author

kolaente commented Nov 8, 2021

The method called here saves the quote. That's where it then fails.

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

I'm not quite sure what would be a good way to fix this. Maybe plugging a different method than getQuote?

@amenk
Copy link

amenk commented Nov 9, 2021

Wondering why it still fails to add product B, even the exception is caught ...

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

Maybe because the exception rewinds the transaction?

@amenk
Copy link

amenk commented Nov 9, 2021

You mean the exception rewineded the transaction before getting caught -- possible.
You could also try to comment-out
$quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage());
There is a setHasError() maybe that rewinds something

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

No, the actual exception gets re-thrown a couple of times. I think it originally happens here and gets rolled back: https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php#L433

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

Uncommenting the $quote->addErrorInfo( doesn't work either

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

I'm actually not quite sure why magento tries to add all existing products to cart when saving the cart (at vendor/magento/module-quote/Model/Quote.php:1804)

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

I think if we could manage to set reset_count to the item's buy request we should be able to trigger resetting the qty here. That would need to happen here.

EDIT: Okay upon second look it seems like this is already fixed in the newest magento version? The last part looks like this in the source on github:

if ($currentItem->getQty() !== $buyRequestData->getQty()) {
    $item = $quote->updateItem($itemId, $buyRequestData);
}

and in the version we're using it does not have that if.

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

Okay it does work when I edit the source at that place here to contain the if.

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

This is the commit that fixes it: magento/magento2@5478c03

@kolaente
Copy link
Author

kolaente commented Nov 9, 2021

It looks like adding magento/magento2@5478c03 via composer patches fixes this issue.

@schmengler @lbuchholz since there's no magento version yet with the patch it might make sense to add a note to the readme about a patch being necessary? Or patch it in the module directly?

There's probably other places as well where this bug is triggered but it looks a lot like an issue with this module (even though it is not, but we'd never noticed that bug without using this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants