Skip to content

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

Fix issue with voucher on draft order when there is no user associated with draft order (#16048) (#16060)

Krzysztof Kwaśniak392344643dfd
diff --git a/saleor/discount/tests/test_discounts.py b/saleor/discount/tests/test_discounts.py
index e074d138af..9e5707dff9 100644
--- a/saleor/discount/tests/test_discounts.py
+++ b/saleor/discount/tests/test_discounts.py
@@ -277,6 +277,15 @@ def test_add_voucher_usage_by_customer_raise_not_applicable(voucher_customer):
add_voucher_usage_by_customer(code, customer_email)
+def test_add_voucher_usage_by_customer_without_customer_email(voucher):
+ # given
+ code = voucher.codes.first()
+
+ # when & then
+ with pytest.raises(NotApplicable):
+ add_voucher_usage_by_customer(code, None)
+
+
def test_remove_voucher_usage_by_customer(voucher_customer):
# given
voucher_customer_count = VoucherCustomer.objects.all().count()
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index b6f8c8ccfd..41ab416009 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -68,7 +68,7 @@ CATALOGUE_FIELDS = ["categories", "collections", "products", "variants"]
def increase_voucher_usage(
voucher: "Voucher",
code: "VoucherCode",
- customer_email: str,
+ customer_email: Optional[str],
increase_voucher_customer_usage: bool = True,
) -> None:
if voucher.usage_limit:
@@ -103,7 +103,12 @@ def activate_voucher_code(code: "VoucherCode") -> None:
code.save(update_fields=["is_active"])
-def add_voucher_usage_by_customer(code: "VoucherCode", customer_email: str) -> None:
+def add_voucher_usage_by_customer(
+ code: "VoucherCode", customer_email: Optional[str]
+) -> None:
+ if not customer_email:
+ raise NotApplicable("Unable to apply voucher as customer details are missing.")
+
_, created = VoucherCustomer.objects.get_or_create(
voucher_code=code, customer_email=customer_email
)
diff --git a/saleor/graphql/order/mutations/draft_order_create.py b/saleor/graphql/order/mutations/draft_order_create.py
index 5203bdbe4a..5b5bf4a191 100644
--- a/saleor/graphql/order/mutations/draft_order_create.py
+++ b/saleor/graphql/order/mutations/draft_order_create.py
@@ -594,6 +594,6 @@ class DraftOrderCreate(
increase_voucher_usage(
voucher,
code_instance,
- instance.user_email or instance.user.email,
+ instance.user_email or instance.user and instance.user.email,
increase_voucher_customer_usage=False,
)
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_create.py b/saleor/graphql/order/tests/mutations/test_draft_order_create.py
index ac1bebe6c8..57f3e43f99 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_create.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_create.py
@@ -2922,3 +2922,45 @@ def test_draft_order_create_with_cc_warehouse_as_shipping_method(
assert len(errors) == 1
assert errors[0]["code"] == OrderErrorCode.INVALID.name
assert errors[0]["field"] == "shippingMethod"
+
+
+def test_draft_order_create_with_voucher_without_user(
+ voucher_percentage,
+ app_api_client,
+ permission_manage_orders,
+ product_available_in_many_channels,
+ graphql_address_data,
+ channel_USD,
+):
+ # given
+ variant = product_available_in_many_channels.variants.first()
+ query = DRAFT_ORDER_CREATE_MUTATION
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ variant_list = [
+ {"variantId": variant_id, "quantity": 2},
+ ]
+ shipping_address = graphql_address_data
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "input": {
+ "lines": variant_list,
+ "billingAddress": shipping_address,
+ "shippingAddress": shipping_address,
+ "channelId": channel_id,
+ "voucherCode": voucher_percentage.code,
+ }
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ query, variables, permissions=(permission_manage_orders,)
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderCreate"]
+ assert len(data["errors"]) == 0
+ order = Order.objects.get(id=graphene.Node.from_global_id(data["order"]["id"])[1])
+ assert order.user is None
+ assert data["order"]["voucherCode"] == voucher_percentage.code == order.voucher_code
+ assert data["order"]["status"] == OrderStatus.DRAFT.upper() == order.status.upper()