Skip to content

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

Always create new checkout in `checkoutCreate` mutation (#7318)

Iga Karbowiakf5f0ef9ab41c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 90372cfba6..ac76ec2250 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -139,6 +139,8 @@ All notable, unreleased changes to this project will be documented in this file.
- drop `productType` from `ProductFilter`
- deprecate mutations' `<name>Errors`, typed `errors` fields and remove deprecation
- Add channel data to Order webhook - #7299 by @krzysztofwolski
+- Always create new checkout in `checkoutCreate` mutation - #7318 by @IKarbowiak
+ - deprecate `created` return field on `checkoutCreate` mutation
### Other
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index e37078bac0..3b44effcae 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -27,7 +27,6 @@ from ...checkout.utils import (
calculate_checkout_quantity,
change_billing_address_in_checkout,
change_shipping_address_in_checkout,
- get_user_checkout,
is_shipping_required,
recalculate_checkout_discount,
remove_promo_code_from_checkout,
@@ -237,6 +236,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):
"Whether the checkout was created or the current active one was returned. "
"Refer to checkoutLinesAdd and checkoutLinesUpdate to merge a cart "
"with an active checkout."
+ "DEPRECATED: Will be removed in Saleor 4.0. Always returns True."
),
)
@@ -414,33 +414,23 @@ class CheckoutCreate(ModelMutation, I18nMixin):
instance.save()
@classmethod
- def perform_mutation(cls, _root, info, **data):
+ def get_instance(cls, info, **data):
+ instance = super().get_instance(info, **data)
user = info.context.user
+ if user.is_authenticated:
+ instance.user = user
+ return instance
+
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
channel_input = data.get("input", {}).get("channel")
channel = cls.clean_channel(channel_input)
if channel:
data["input"]["channel"] = channel
-
- # `perform_mutation` is overridden to properly get or create a checkout
- # instance here and abort mutation if needed.
- if user.is_authenticated:
- checkout_queryset = models.Checkout.objects.filter(channel=channel)
- checkout = get_user_checkout(user, checkout_queryset=checkout_queryset)
-
- if checkout is not None:
- # If user has an active checkout, return it without any
- # modifications.
- return CheckoutCreate(checkout=checkout, created=False)
- checkout = models.Checkout(user=user)
- else:
- checkout = models.Checkout()
- cleaned_input = cls.clean_input(info, checkout, data.get("input"))
- checkout = cls.construct_instance(checkout, cleaned_input)
- cls.clean_instance(info, checkout)
- cls.save(info, checkout, cleaned_input)
- cls._save_m2m(info, checkout, cleaned_input)
- info.context.plugins.checkout_created(checkout)
- return CheckoutCreate(checkout=checkout, created=True)
+ response = super().perform_mutation(_root, info, **data)
+ info.context.plugins.checkout_created(response.checkout)
+ response.created = True
+ return response
class CheckoutLinesAdd(BaseMutation):
diff --git a/saleor/graphql/checkout/tests/deprecated/__init__.py b/saleor/graphql/checkout/tests/deprecated/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/checkout/tests/deprecated/test_checkout.py b/saleor/graphql/checkout/tests/deprecated/test_checkout.py
new file mode 100644
index 0000000000..829fe7333e
--- /dev/null
+++ b/saleor/graphql/checkout/tests/deprecated/test_checkout.py
@@ -0,0 +1,48 @@
+import graphene
+
+from .....checkout.models import Checkout
+from ....tests.utils import get_graphql_content
+
+
+def test_checkout_create(api_client, stock, graphql_address_data, channel_USD):
+ """Create checkout object using GraphQL API."""
+ query = """
+ mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
+ checkoutCreate(input: $checkoutInput) {
+ created
+ checkout {
+ id
+ token
+ email
+ quantity
+ lines {
+ quantity
+ }
+ }
+ errors {
+ field
+ message
+ code
+ variants
+ addressType
+ }
+ }
+ }
+ """
+ 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": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ assert content["created"] is True
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 9a5a4548a9..7d577395c5 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -109,7 +109,6 @@ def test_update_checkout_shipping_method_if_invalid(
MUTATION_CHECKOUT_CREATE = """
mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
checkoutCreate(input: $checkoutInput) {
- created
checkout {
id
token
@@ -178,9 +177,7 @@ def test_checkout_create_with_default_channel(
assert not Checkout.objects.exists()
with warnings.catch_warnings(record=True) as warns:
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- assert content["created"] is True
+ get_graphql_content(response)["data"]["checkoutCreate"]
new_checkout = Checkout.objects.first()
lines = fetch_checkout_lines(new_checkout)
@@ -293,9 +290,7 @@ def test_checkout_create_with_inactive_default_channel(
assert not Checkout.objects.exists()
with warnings.catch_warnings(record=True) as warns:
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- assert content["created"] is True
+ get_graphql_content(response)["data"]["checkoutCreate"]
new_checkout = Checkout.objects.first()
@@ -327,9 +322,7 @@ def test_checkout_create_with_inactive_and_active_default_channel(
assert not Checkout.objects.exists()
with warnings.catch_warnings(record=True) as warns:
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- assert content["created"] is True
+ get_graphql_content(response)["data"]["checkoutCreate"]
new_checkout = Checkout.objects.first()
@@ -460,8 +453,6 @@ def test_checkout_create_with_multiple_channel_with_channel_slug(
content = get_graphql_content(response)["data"]["checkoutCreate"]
- assert content["created"] is True
-
new_checkout = Checkout.objects.first()
assert new_checkout is not None
assert new_checkout.channel == channel_USD
@@ -494,7 +485,6 @@ def test_checkout_create_with_existing_checkout_in_other_channel(
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- assert content["created"] is True
checkout_data = content["checkout"]
assert checkout_data["token"] != str(old_checkout.token)
@@ -544,9 +534,6 @@ def test_checkout_create(api_client, stock, graphql_address_data, channel_USD):
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- # Look at the flag to see whether a new checkout was created or not
- assert content["created"] is True
-
new_checkout = Checkout.objects.first()
assert new_checkout is not None
checkout_data = content["checkout"]
@@ -643,9 +630,6 @@ def test_checkout_create_multiple_warehouse(
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- # Look at the flag to see whether a new checkout was created or not
- assert content["created"] is True
-
new_checkout = Checkout.objects.first()
assert new_checkout is not None
checkout_data = content["checkout"]
@@ -674,9 +658,6 @@ def test_checkout_create_with_null_as_addresses(api_client, stock, channel_USD):
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- # Look at the flag to see whether a new checkout was created or not
- assert content["created"] is True
-
new_checkout = Checkout.objects.first()
assert new_checkout is not None
checkout_data = content["checkout"]
@@ -709,9 +690,6 @@ def test_checkout_create_with_variant_without_inventory_tracking(
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- # Look at the flag to see whether a new checkout was created or not
- assert content["created"] is True
-
new_checkout = Checkout.objects.first()
assert new_checkout is not None
checkout_data = content["checkout"]
@@ -791,16 +769,10 @@ def test_checkout_create_reuse_checkout(checkout, user_api_client, stock):
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- # Look at the flag to see whether a new checkout was created or not
- assert not content["created"]
-
- # assert that existing checkout was reused and returned by mutation
checkout_data = content["checkout"]
- assert checkout_data["token"] == str(checkout.token)
+ assert checkout_data["token"] != str(checkout.token)
- # if checkout was reused it should be returned unmodified (e.g. without
- # adding new lines that was passed)
- assert checkout_data["lines"] == []
+ assert len(checkout_data["lines"]) == 1
def test_checkout_create_required_email(api_client, stock, channel_USD):
@@ -1043,8 +1015,7 @@ def test_checkout_create_sets_country_from_shipping_address_country(
assert not Checkout.objects.exists()
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert data["created"] is True
+ content["data"]["checkoutCreate"]
checkout = Checkout.objects.first()
assert checkout.country == "US"
diff --git a/saleor/graphql/checkout/tests/test_checkout_digital.py b/saleor/graphql/checkout/tests/test_checkout_digital.py
index d832b1bb94..f23902ef50 100644
--- a/saleor/graphql/checkout/tests/test_checkout_digital.py
+++ b/saleor/graphql/checkout/tests/test_checkout_digital.py
@@ -43,15 +43,12 @@ def test_create_checkout(
if with_shipping_address:
checkout_input["shippingAddress"] = graphql_address_data
- content = get_graphql_content(
+ get_graphql_content(
api_client.post_graphql(
MUTATION_CHECKOUT_CREATE, {"checkoutInput": checkout_input}
)
)["data"]["checkoutCreate"]
- # Ensure checkout was created
- assert content["created"] is True, "The checkout should have been created"
-
# Retrieve the created checkout
checkout = Checkout.objects.get()