Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix Insufficient stock error in checkoutLinesUpdate with 0 quantity. (#8175) (#8232)

Dawid Matuszczyk3d082b2138f1
diff --git a/saleor/graphql/checkout/tests/test_checkout_lines.py b/saleor/graphql/checkout/tests/test_checkout_lines.py
index c664a02928..2949350df1 100644
--- a/saleor/graphql/checkout/tests/test_checkout_lines.py
+++ b/saleor/graphql/checkout/tests/test_checkout_lines.py
@@ -548,6 +548,27 @@ def test_checkout_lines_update_variant_quantity_over_avability_stock(
assert data["checkout"]["lines"][0]["quantity"] == variables["lines"][0]["quantity"]
+def test_checkout_lines_delete_with_by_zero_quantity_when_variant_out_of_stock(
+ user_api_client, checkout_with_item
+):
+ checkout = checkout_with_item
+ line = checkout.lines.first()
+ variant_id = graphene.Node.to_global_id("ProductVariant", line.variant.pk)
+ stock = line.variant.stocks.first()
+ stock.quantity = 0
+ stock.save(update_fields=["quantity"])
+
+ variables = {
+ "token": checkout.token,
+ "lines": [{"variantId": variant_id, "quantity": 0}],
+ "channelSlug": checkout.channel.slug,
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutLinesUpdate"]
+ assert not data["checkout"]["lines"]
+
+
@mock.patch(
"saleor.graphql.checkout.mutations.update_checkout_shipping_method_if_invalid",
wraps=update_checkout_shipping_method_if_invalid,
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index badad5180c..e1abe80693 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -81,15 +81,14 @@ def check_stock_quantity_bulk(
[stock.available_quantity for stock in stocks] # type: ignore
)
- if not stocks:
- insufficient_stocks.append(
- InsufficientStockData(
- variant=variant,
- available_quantity=available_quantity,
+ if quantity > 0:
+ if not stocks:
+ insufficient_stocks.append(
+ InsufficientStockData(
+ variant=variant, available_quantity=available_quantity
+ )
)
- )
- elif variant.track_inventory:
- if quantity > available_quantity:
+ elif variant.track_inventory and quantity > available_quantity:
insufficient_stocks.append(
InsufficientStockData(
variant=variant,