Skip to content

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

Assgin logged in customer to checkout

fowczarekb5f827592aac
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 07afb62190..3f93c6c24b 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -93,6 +93,8 @@ class CheckoutCreateInput(graphene.InputObjectType):
shipping_address = AddressInput(
description=(
'The mailling address to where the checkout will be shipped.'))
+ billing_address = AddressInput(
+ description=('Billing address of the customer.'))
class CheckoutCreate(ModelMutation, I18nMixin):
@@ -108,6 +110,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):
@classmethod
def clean_input(cls, info, instance, input, errors):
cleaned_input = super().clean_input(info, instance, input, errors)
+ user = info.context.user
lines = input.pop('lines', None)
if lines:
variant_ids = [line.get('variant_id') for line in lines]
@@ -128,16 +131,26 @@ class CheckoutCreate(ModelMutation, I18nMixin):
shipping_address, errors = cls.validate_address(
shipping_address_data, errors)
cleaned_input['shipping_address'] = shipping_address
+ elif user:
+ cleaned_input['shipping_address'] = user.default_shipping_address
+
return cleaned_input
@classmethod
def save(cls, info, instance, cleaned_input):
shipping_address = cleaned_input.get('shipping_address')
+ update_fields = []
if shipping_address:
shipping_address.save()
instance.shipping_address = shipping_address
+ update_fields.append('shipping_address')
super().save(info, instance, cleaned_input)
- instance.save(update_fields=['shipping_address'])
+ user = info.context.user
+ if not user.is_anonymous:
+ instance.user = user
+ update_fields.append('user')
+ instance.save(update_fields=update_fields)
+
variants = cleaned_input.get('variants')
quantities = cleaned_input.get('quantities')
if variants and quantities:
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 5d80d3cbb0..ae88491769 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -90,6 +90,7 @@ def test_checkout_create_logged_in_customer(
def test_checkout_create_logged_in_customer_custom_email(
user_api_client, variant):
variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
+ customer = user_api_client.user
custom_email = '[email protected]'
variables = {
'checkoutInput': {
@@ -98,6 +99,7 @@ def test_checkout_create_logged_in_customer_custom_email(
'variantId': variant_id}],
'email': custom_email}}
assert not Cart.objects.exists()
+ assert not custom_email == customer.email
response = user_api_client.post_graphql(
MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)
@@ -106,7 +108,6 @@ def test_checkout_create_logged_in_customer_custom_email(
checkout_data = content['data']['checkoutCreate']['checkout']
assert checkout_data['token'] == str(new_cart.token)
cart_user = new_cart.user
- customer = user_api_client.user
assert customer.id == cart_user.id
assert new_cart.email == custom_email
@@ -114,7 +115,6 @@ def test_checkout_create_logged_in_customer_custom_email(
def test_checkout_create_logged_in_customer_custom_addresses(
user_api_client, variant, graphql_address_data):
variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
- test_email = '[email protected]'
shipping_address = graphql_address_data
billing_address = graphql_address_data
variables = {