Skip to content

Unable to execute addSimpleProduct mutation while other items out of stock #26683

Closed
@logaretm

Description

@logaretm

Summary (*)

We are using Magento as a headless backend for our front-facing application, we are using the GraphQL API to build the frontend.

We noticed if we added a product to cart, and that product later became out of stock, any further attempts to add items to cart will fail with Some cart items are out of stock effectively locking up the cart from adding other items or changing other items quantities.

This is the returned GraphQL error:

"message": "Some of the products are out of stock.",

This is a breaking issue for us, as the alternative would result in a very poor experience for our users.

Currently, we have things set up that the frontend is able to detect whenever this exception is thrown and tries to amend it by removing the offending items and attempting to re-play whatever mutations are done to the cart since then.

Needless to say, this is a very flaky solution and it doesn't make sense to have this restriction in the first place since the REST API seems to be fine with this scenario.

Examples (*)

To Reproduce:

  1. Using the GraphQL API: Add an item to cart.
  2. Mark it as out-of-stock from the admin dashboard.
  3. Using the GraphQL API: Try to add other products to cart.

I have successfully reproduced this on a fresh magento environment

Proposed solution

Ideally, the GraphQL API should allow mutating the cart as long as the out-of-stock item is not being the target of the mutation (updating its quantity for example).

A slightly less robust solution is to allow the mutation to go through, but return errors alongside the data prompting the GQL client to update their UI and also be aware of possible problems.

GraphQL requests for reproducing

Create an empty cart

mutation {
  createEmptyCart
}

Add product to cart

mutation {
  addSimpleProductsToCart(
    input: {
      cart_id: "{ CART_ID }"   # <--- cart id from the previous request goes here
      cart_items: [
        {
          data: {
            quantity: 1
            sku: "simple-product"   # <--- product sku goes here
          }
        }
      ]
    }
  ) {
    cart {
      items {
        id
        product {
          sku
          stock_status
        }
        quantity
      }
    }
  }

Metadata

Metadata

Labels

Component: GraphQLGraphQLFixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions