Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Checkout lines add fix (#7604)
Tomasz Szymański3b118ab11eb7
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 92e4260e0e..672099978b 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -125,13 +125,13 @@ def calculate_checkout_quantity(lines: Iterable["CheckoutLineInfo"]):def add_variants_to_checkout(- checkout, variants, quantities, channel_slug, skip_stock_check=False+ checkout, variants, quantities, channel_slug, skip_stock_check=False, replace=False):"""Add variants to checkout.If a variant is not placed in checkout, a new checkout line will be created.- Otherwise, its quantity will be replaced (if greater than 0).If quantity is set to 0, checkout line will be deleted.+ Otherwise, quantity will be added or replaced (if replace argument is True)."""# check quantities@@ -159,7 +159,10 @@ def add_variants_to_checkout(if variant.pk in variant_ids_in_lines:line = variant_ids_in_lines[variant.pk]if quantity > 0:- line.quantity = quantity+ if replace:+ line.quantity = quantity+ else:+ line.quantity += quantityto_update.append(line)else:to_delete.append(line)diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 644d184cc5..8250362578 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -432,7 +432,10 @@ class CheckoutLinesAdd(BaseMutation):)class Meta:- description = "Adds a checkout line to the existing checkout."+ description = (+ "Adds a checkout line to the existing checkout."+ "If line was already in checkout, its quantity will be increased."+ )error_type_class = CheckoutErrorerror_type_field = "checkout_errors"@@ -442,7 +445,7 @@ class CheckoutLinesAdd(BaseMutation):@classmethoddef clean_input(- cls, checkout, variants, quantities, checkout_info, manager, discounts+ cls, checkout, variants, quantities, checkout_info, manager, discounts, replace):channel_slug = checkout_info.channel.slugcls.validate_checkout_lines(@@ -464,6 +467,7 @@ class CheckoutLinesAdd(BaseMutation):quantities,channel_slug,skip_stock_check=True, # already checked by validate_checkout_lines+ replace=replace,)except ProductNotPublished as exc:raise ValidationError(@@ -479,7 +483,9 @@ class CheckoutLinesAdd(BaseMutation):)@classmethod- def perform_mutation(cls, _root, info, lines, checkout_id=None, token=None):+ def perform_mutation(+ cls, _root, info, lines, checkout_id=None, token=None, replace=False+ ):# DEPRECATEDvalidate_one_of_args_is_in_mutation(CheckoutErrorCode, "checkout_id", checkout_id, "token", token@@ -502,7 +508,7 @@ class CheckoutLinesAdd(BaseMutation):checkout_info = fetch_checkout_info(checkout, [], discounts, manager)cls.clean_input(- checkout, variants, quantities, checkout_info, manager, discounts+ checkout, variants, quantities, checkout_info, manager, discounts, replace)lines = fetch_checkout_lines(checkout)@@ -536,7 +542,9 @@ class CheckoutLinesUpdate(CheckoutLinesAdd):@classmethoddef perform_mutation(cls, root, info, lines, checkout_id=None, token=None):- return super().perform_mutation(root, info, lines, checkout_id, token)+ return super().perform_mutation(+ root, info, lines, checkout_id, token, replace=True+ )class CheckoutLineDelete(BaseMutation):diff --git a/saleor/graphql/checkout/tests/test_checkout_lines.py b/saleor/graphql/checkout/tests/test_checkout_lines.pyindex 2477f1c20f..e02cf40eb9 100644--- a/saleor/graphql/checkout/tests/test_checkout_lines.py+++ b/saleor/graphql/checkout/tests/test_checkout_lines.py@@ -72,6 +72,25 @@ def test_checkout_lines_add(mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)+def test_checkout_lines_add_existing_variant(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)++ variables = {+ "token": checkout.token,+ "lines": [{"variantId": variant_id, "quantity": 7}],+ "channelSlug": checkout.channel.slug,+ }+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutLinesAdd"]+ assert not data["errors"]+ checkout.refresh_from_db()+ line = checkout.lines.latest("pk")+ assert line.quantity == 10++def test_checkout_lines_add_with_unavailable_variant(user_api_client, checkout_with_item, stock):