Skip to content

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

Validate checkoutline quantity (#7201)

Damian Wysocki2f67d47b5145
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index b3362c6c51..e37078bac0 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -115,10 +115,19 @@ def update_checkout_shipping_method_if_invalid(
checkout.save(update_fields=["shipping_method", "last_change"])
-def check_lines_quantity(variants, quantities, country, channel_slug):
- """Clean quantities and check if stock is sufficient for each checkout line."""
+def check_lines_quantity(
+ variants, quantities, country, channel_slug, allow_zero_quantity=False
+):
+ """Clean quantities and check if stock is sufficient for each checkout line.
+
+ By default, zero quantity is not allowed,
+ but if this validation is used for updating existing checkout lines,
+ allow_zero_quantities can be set to True
+ and checkout lines with this quantity can be later removed.
+ """
+
for quantity in quantities:
- if quantity < 0:
+ if not allow_zero_quantity and quantity <= 0:
raise ValidationError(
{
"quantity": ValidationError(
@@ -127,6 +136,17 @@ def check_lines_quantity(variants, quantities, country, channel_slug):
)
}
)
+
+ elif allow_zero_quantity and quantity < 0:
+ raise ValidationError(
+ {
+ "quantity": ValidationError(
+ "The quantity should be higher or equal zero.",
+ code=CheckoutErrorCode.ZERO_QUANTITY,
+ )
+ }
+ )
+
if quantity > settings.MAX_CHECKOUT_LINE_QUANTITY:
raise ValidationError(
{
@@ -443,20 +463,14 @@ class CheckoutLinesAdd(BaseMutation):
error_type_field = "checkout_errors"
@classmethod
- def perform_mutation(cls, _root, info, checkout_id, lines, replace=False):
- checkout = cls.get_node_or_error(
- info, checkout_id, only_type=Checkout, field="checkout_id"
- )
- discounts = info.context.discounts
- manager = info.context.plugins
-
- variant_ids = [line.get("variant_id") for line in lines]
- variants = cls.get_nodes_or_error(variant_ids, "variant_id", ProductVariant)
- quantities = [line.get("quantity") for line in lines]
-
- checkout_info = fetch_checkout_info(checkout, [], discounts, manager)
+ def validate_checkout_lines(cls, variants, quantities, country, channel_slug):
+ check_lines_quantity(variants, quantities, country, channel_slug)
- check_lines_quantity(
+ @classmethod
+ def clean_input(
+ cls, checkout, variants, quantities, checkout_info, manager, discounts, replace
+ ):
+ cls.validate_checkout_lines(
variants, quantities, checkout.get_country(), checkout_info.channel.slug
)
variants_db_ids = {variant.id for variant in variants}
@@ -489,6 +503,30 @@ class CheckoutLinesAdd(BaseMutation):
)
)
+ @classmethod
+ def perform_mutation(cls, _root, info, checkout_id, lines, replace=False):
+ checkout = cls.get_node_or_error(
+ info, checkout_id, only_type=Checkout, field="checkout_id"
+ )
+ discounts = info.context.discounts
+ manager = info.context.plugins
+
+ variant_ids = [line.get("variant_id") for line in lines]
+ variants = cls.get_nodes_or_error(variant_ids, "variant_id", ProductVariant)
+ quantities = [line.get("quantity") for line in lines]
+
+ checkout_info = fetch_checkout_info(checkout, [], discounts, manager)
+ cls.clean_input(
+ checkout, variants, quantities, checkout_info, manager, discounts, replace
+ )
+
+ lines = fetch_checkout_lines(checkout)
+ checkout_info.valid_shipping_methods = (
+ get_valid_shipping_method_list_for_checkout_info(
+ checkout_info, checkout_info.shipping_address, lines, discounts, manager
+ )
+ )
+
update_checkout_shipping_method_if_invalid(checkout_info, lines)
recalculate_checkout_discount(
manager, checkout_info, lines, info.context.discounts
@@ -505,6 +543,12 @@ class CheckoutLinesUpdate(CheckoutLinesAdd):
error_type_class = CheckoutError
error_type_field = "checkout_errors"
+ @classmethod
+ def validate_checkout_lines(cls, variants, quantities, country, channel_slug):
+ check_lines_quantity(
+ variants, quantities, country, channel_slug, allow_zero_quantity=True
+ )
+
@classmethod
def perform_mutation(cls, root, info, checkout_id, lines):
return super().perform_mutation(root, info, checkout_id, lines, replace=True)
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index d83431efeb..9a5a4548a9 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -220,6 +220,31 @@ def test_checkout_create_with_inactive_channel(
assert error["code"] == CheckoutErrorCode.CHANNEL_INACTIVE.name
+def test_checkout_create_with_zero_quantity(
+ api_client, stock, graphql_address_data, channel_USD
+):
+
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 0, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "quantity"
+ assert error["code"] == CheckoutErrorCode.ZERO_QUANTITY.name
+
+
def test_checkout_create_with_unavailable_variant(
api_client, stock, graphql_address_data, channel_USD
):
@@ -1522,6 +1547,26 @@ def test_checkout_lines_add_with_unavailable_variant(
assert errors[0]["variants"] == [variant_id]
+def test_checkout_lines_add_with_zero_quantity(
+ user_api_client, checkout_with_item, stock
+):
+ variant = stock.product_variant
+ checkout = checkout_with_item
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ variables = {
+ "checkoutId": checkout_id,
+ "lines": [{"variantId": variant_id, "quantity": 0}],
+ "channelSlug": checkout.channel.slug,
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["checkoutLinesAdd"]["errors"]
+ assert errors[0]["code"] == CheckoutErrorCode.ZERO_QUANTITY.name
+ assert errors[0]["field"] == "quantity"
+
+
def test_checkout_lines_add_no_channel_shipping_zones(
user_api_client, checkout_with_item, stock
):