Skip to content

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

Fix passing replace flag to check_stock_quantity_bulk

dex993cfaac413c
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 6e0d954c67..920b59acc4 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -127,6 +127,7 @@ def check_lines_quantity(
channel_slug,
allow_zero_quantity=False,
existing_lines=None,
+ replace=False,
):
"""Clean quantities and check if stock is sufficient for each checkout line.
@@ -169,7 +170,12 @@ def check_lines_quantity(
)
try:
check_stock_quantity_bulk(
- variants, country, quantities, channel_slug, existing_lines=existing_lines
+ variants,
+ country,
+ quantities,
+ channel_slug,
+ existing_lines=existing_lines,
+ replace=replace,
)
except InsufficientStock as e:
errors = [
@@ -587,6 +593,7 @@ class CheckoutLinesUpdate(CheckoutLinesAdd):
channel_slug,
allow_zero_quantity=True,
existing_lines=lines,
+ replace=True,
)
@classmethod
diff --git a/saleor/graphql/checkout/tests/test_checkout_lines.py b/saleor/graphql/checkout/tests/test_checkout_lines.py
index c7b9813ca2..c664a02928 100644
--- a/saleor/graphql/checkout/tests/test_checkout_lines.py
+++ b/saleor/graphql/checkout/tests/test_checkout_lines.py
@@ -527,6 +527,27 @@ def test_checkout_lines_update_channel_without_shipping_zones(
assert errors[0]["field"] == "quantity"
+def test_checkout_lines_update_variant_quantity_over_avability_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)
+ current_stock = line.variant.stocks.first()
+ line.quantity = current_stock.quantity - 1
+ line.save()
+
+ variables = {
+ "token": checkout.token,
+ "lines": [{"variantId": variant_id, "quantity": current_stock.quantity - 2}],
+ "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 data["checkout"]["lines"][0]["quantity"] == variables["lines"][0]["quantity"]
+
+
@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 cc0dd39900..badad5180c 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -48,6 +48,7 @@ def check_stock_quantity_bulk(
channel_slug: str,
additional_filter_lookup: Optional[Dict[str, Any]] = None,
existing_lines: Iterable = None,
+ replace=False,
):
"""Validate if there is stock available for given variants in given country.
@@ -72,8 +73,8 @@ def check_stock_quantity_bulk(
line.variant.pk: line.line.quantity for line in existing_lines or []
}
for variant, quantity in zip(variants, quantities):
-
- quantity += variants_quantities.get(variant.pk, 0)
+ if not replace:
+ quantity += variants_quantities.get(variant.pk, 0)
stocks = variant_stocks.get(variant.pk, [])
available_quantity = sum(