magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.45k stars 9.29k forks source link

Rest cart item update throws `The requested qty is not available` if quantity in cart and quantity to update to exceeds stock #38949

Open sanderjongsma opened 1 month ago

sanderjongsma commented 1 month ago

Preconditions and environment

Steps to reproduce

  1. Create guest cart with rest api: Post to https://11bfd466f8c258a1113feb50fd641584.instances-prod.magento-community.engineering/rest/V1/guest-carts/ and use that masked id.

  2. Get the guest cart to determine quote id: Get to https://11bfd466f8c258a1113feb50fd641584.instances-prod.magento-community.engineering/rest/V1/guest-carts/{{masked_id}}

  3. Add just 1 configurable to cart: Post to: https://11bfd466f8c258a1113feb50fd641584.instances-prod.magento-community.engineering/rest/V1/guest-carts/{{masked_id}}/items with body:

    
    {
    "cartItem": {
    "sku": "MH02",
    "qty": 1,
    "quote_id": "{{quote_id}}",
    "product_option": {
      "extension_attributes": {
        "configurable_item_options": [
          {
            "option_id": "93",
            "option_value": 49
          },
          {
            "option_id": "142",
            "option_value": 166
          }
        ]
      }
    },
    "extension_attributes": {}
    }
    }

4. Update the qty to 100 (default stock for a simple of a configurable product)
![image](https://github.com/user-attachments/assets/4c4a63bc-3fe9-4494-bfc9-84421d280ec6)

Note: Use the item_id of the configurable type (not the simple)

PUT to `https://11bfd466f8c258a1113feb50fd641584.instances-prod.magento-community.engineering/rest/V1/guest-carts/{{masked_id}}/items/{{item_id}}` with body:

{ "cartItem": { "sku": "MH02", "qty": 100, "quote_id": "{{quote_id}}", "product_option": { "extension_attributes": { "configurable_item_options": [ { "option_id": "93", "option_value": 49 }, { "option_id": "142", "option_value": 166 } ] } }, "extension_attributes": {} } }


Now the response of the call is:  

{ "message": "The requested qty is not available" }


### Expected result

When doing step 4 the requested qty of 100 should be in the cart after the post request instead of an exception

### Actual result

This exception is thrown in step 4:

{ "message": "The requested qty is not available" }



### Additional information

while the actual stock is 100, so this should work. The error is thrown from this file: `vendor/magento/module-inventory-sales/Model/IsProductSalableForRequestedQtyCondition/IsSalableWithReservationsCondition.php` and after debugging it is because in this validator the $requestedQty is 101 instead of 100, so the requested quantity from the api call is added to the current quantity in the cart.

In \Magento\Quote\Model\Quote::updateItem on line 1805 there is a `$buyRequest->setResetCount(true);` to reset the actual counter, but when the `reset_count` is red in the request on line 94 here: `\Magento\Quote\Model\Quote\Item\Processor::prepare` there is another check: `$item->getId() == $request->getId()` which will be never true because the request does not have the id of the item.

### Release note

_No response_

### Triage and priority

- [ ] Severity: **S0** _- Affects critical data or functionality and leaves users without workaround._
- [ ] Severity: **S1** _- Affects critical data or functionality and forces users to employ a workaround._
- [ ] Severity: **S2** _- Affects non-critical data or functionality and forces users to employ a workaround._
- [ ] Severity: **S3** _- Affects non-critical data or functionality and does not force users to employ a workaround._
- [ ] Severity: **S4** _- Affects aesthetics, professional look and feel, “quality” or “usability”._
m2-assistant[bot] commented 1 month ago

Hi @sanderjongsma. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 1 month ago

Hi @engcom-Bravo. 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:

engcom-Bravo commented 1 month ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 1 month ago

Hi @engcom-Bravo. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 1 month ago

Hi @engcom-Bravo, here is your Magento Instance: https://af74504e6ae991dcc1cb1e6019789fb1.instances-prod.magento-community.engineering Admin access: https://af74504e6ae991dcc1cb1e6019789fb1.instances-prod.magento-community.engineering/admin_e8a4 Login: ce6f05eb Password: 4f1aaf440e68

engcom-Bravo commented 1 month ago

Hi @sanderjongsma,

Thanks for your reporting and collaboration.

We have verified the issue in Latest 2.4-develop instance and it seems to be an expected behaviour of magento.

Screenshot 2024-07-24 at 14 48 30

Since already the product is in cart with the QTY 1 and again we are adding more than available QTY it will shows an error The requested qty is not available which seems to be an expected behaviour.

Kindly let us know if we are missing anything.

Thanks.

sanderjongsma commented 1 month ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 1 month ago

Hi @sanderjongsma. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 1 month ago

Hi @sanderjongsma, here is your Magento Instance: https://af74504e6ae991dcc1cb1e6019789fb1.instances-prod.magento-community.engineering Admin access: https://af74504e6ae991dcc1cb1e6019789fb1.instances-prod.magento-community.engineering/admin_6de1 Login: 6a0b133d Password: 942edd90d96e

sanderjongsma commented 1 month ago

Since already the product is in cart with the QTY 1 and again we are adding more than available QTY it will shows an error The requested qty is not available which seems to be an expected behavior.

That can't be right. Because if I add 10 to the cart with the rest, I have 10 in the cart. If I then use the update rest call with qty 20, it is 20. And what you imply with your comment it should actually be 30 (10 with add plus 20 with update). But that is not happening. An update rest call with the qty is for updating that cart item to the quantity in the body of the rest call.

engcom-Bravo commented 1 month ago

Hi @sanderjongsma,

Thanks for your update.

As per this comment https://github.com/magento/magento2/issues/38949#issuecomment-2247611961 we are confirming the issue.

Thanks.

github-jira-sync-bot commented 1 month ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-12641 is successfully created for this GitHub issue.

m2-assistant[bot] commented 1 month ago

:white_check_mark: Confirmed by @engcom-Bravo. Thank you for verifying the issue.
Issue Available: @engcom-Bravo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.