Skip to content

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

Add quantity validation to CheckoutShippingAddressUpdate

Michał Sipadc6a6c63b78f
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index f8a416e122..8c9f2dfcb3 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -514,6 +514,20 @@ class CheckoutShippingAddressUpdate(BaseMutation, I18nMixin):
error_type_class = CheckoutError
error_type_field = "checkout_errors"
+ @classmethod
+ def process_checkout_lines(
+ cls, lines, country
+ ) -> Tuple[List[product_models.ProductVariant], List[int]]:
+ variant_ids = [line.variant.id for line in lines]
+ variants = product_models.ProductVariant.objects.filter(
+ id__in=variant_ids
+ ).prefetch_related("product__product_type")
+ quantities = [line.quantity for line in lines]
+
+ check_lines_quantity(variants, quantities, country)
+
+ return variants, quantities
+
@classmethod
def perform_mutation(cls, _root, info, checkout_id, shipping_address):
pk = from_global_id_strict_type(checkout_id, Checkout, field="checkout_id")
@@ -548,6 +562,17 @@ class CheckoutShippingAddressUpdate(BaseMutation, I18nMixin):
lines = list(checkout)
+ country = info.context.country.code
+ # set country to one from shipping address
+ if shipping_address and shipping_address.country:
+ if shipping_address.country != country:
+ country = shipping_address.country
+ checkout.set_country(country)
+
+ # Resolve and process the lines, retrieving the variants and quantities
+ if lines:
+ cls.process_checkout_lines(lines, country)
+
update_checkout_shipping_method_if_invalid(
checkout, lines, info.context.discounts
)
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index fb8c13e410..cd731a7e14 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -550,7 +550,7 @@ def test_checkout_create_check_lines_quantity_from_shipping_address_country(
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutCreate"]
- assert data["created"] == True
+ assert data["created"] is True
checkout = Checkout.objects.first()
assert checkout.country == "US"