Skip to content

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

Fix checkout_create to return checkout related with specific channel (#6504)

Damian Wysocki00156147b06b
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 8799ccfbab..e940e2f943 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -288,10 +288,11 @@ class CheckoutCreate(ModelMutation, I18nMixin):
@classmethod
def clean_input(cls, info, instance: models.Checkout, data, input_cls=None):
- cleaned_input = super().clean_input(info, instance, data)
user = info.context.user
country = info.context.country.code
- channel = cls.clean_channel(cleaned_input.get("channel"))
+ channel = data.pop("channel")
+ cleaned_input = super().clean_input(info, instance, data)
+
cleaned_input["channel"] = channel
cleaned_input["currency"] = channel.currency_code
@@ -308,7 +309,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):
(
cleaned_input["variants"],
cleaned_input["quantities"],
- ) = cls.process_checkout_lines(lines, country, cleaned_input["channel"].id)
+ ) = cls.process_checkout_lines(lines, country, channel.id)
cleaned_input["shipping_address"] = cls.retrieve_shipping_address(user, data)
cleaned_input["billing_address"] = cls.retrieve_billing_address(user, data)
@@ -372,11 +373,16 @@ class CheckoutCreate(ModelMutation, I18nMixin):
@classmethod
def perform_mutation(cls, _root, info, **data):
user = info.context.user
+ 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 = get_user_checkout(user)
+ 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
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index abe76a2978..2009b84d1e 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -399,6 +399,33 @@ def test_checkout_create_with_multiple_channel_with_channel_slug(
assert checkout_line.quantity == 1
+def test_checkout_create_with_existing_checkout_in_other_channel(
+ user_api_client, stock, graphql_address_data, channel_USD, user_checkout_PLN
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ old_checkout = Checkout.objects.first()
+
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ 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)
+
+
def test_checkout_create_with_inactive_channel_slug(
api_client, stock, graphql_address_data, channel_USD
):
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 5c42f44c46..a23e2a4417 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -520,6 +520,19 @@ def user_checkout(customer_user, channel_USD):
return checkout
+def user_checkout_PLN(customer_user, channel_PLN):
+ checkout = Checkout.objects.create(
+ user=customer_user,
+ channel=channel_PLN,
+ billing_address=customer_user.default_billing_address,
+ shipping_address=customer_user.default_shipping_address,
+ note="Test notes",
+ currency="PLN",
+ )
+ return checkout
+
+
@pytest.fixture
def user_checkout_with_items(user_checkout, product_list):
for product in product_list: