Skip to content

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

Do not require email when adding voucher to checkout (#10853)

Krzysztof Waliczek93d830f944db
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index 7567d17a8b..f9befe33fa 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -33,8 +33,13 @@ def add_gift_card_code_to_checkout(
):
"""Add gift card data to checkout by code.
+ Raise ValidationError if email is not provided.
Raise InvalidPromoCode if gift card cannot be applied.
"""
+ from ..checkout.checkout_cleaner import validate_checkout_email
+
+ validate_checkout_email(checkout)
+
try:
# only active gift card with currency the same as channel currency can be used
gift_card = (
diff --git a/saleor/graphql/checkout/mutations/checkout_add_promo_code.py b/saleor/graphql/checkout/mutations/checkout_add_promo_code.py
index 7f2bb12ebf..2adac22ca8 100644
--- a/saleor/graphql/checkout/mutations/checkout_add_promo_code.py
+++ b/saleor/graphql/checkout/mutations/checkout_add_promo_code.py
@@ -1,7 +1,6 @@
import graphene
from django.core.exceptions import ValidationError
-from ....checkout.checkout_cleaner import validate_checkout_email
from ....checkout.error_codes import CheckoutErrorCode
from ....checkout.fetch import (
fetch_checkout_info,
@@ -61,7 +60,6 @@ class CheckoutAddPromoCode(BaseMutation):
error_class=CheckoutErrorCode,
)
- validate_checkout_email(checkout)
manager = load_plugin_manager(info.context)
discounts = load_discounts(info.context)
lines, unavailable_variant_pks = fetch_checkout_lines(checkout)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
index 8e74906c81..120d9ca250 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
@@ -866,9 +866,10 @@ def test_checkout_add_promo_code_invalidate_shipping_method(
assert shipping_method_id not in data["checkout"]["availableShippingMethods"]
-def test_checkout_add_promo_code_no_checkout_email(
+def test_checkout_add_promo_code_without_checkout_email(
api_client, checkout_with_item, voucher
):
+ # given
checkout_with_item.email = None
checkout_with_item.save(update_fields=["email"])
@@ -876,8 +877,34 @@ def test_checkout_add_promo_code_no_checkout_email(
"id": to_global_id_or_none(checkout_with_item),
"promoCode": voucher.code,
}
+
+ # when
+ data = _mutate_checkout_add_promo_code(api_client, variables)
+
+ # then
+ assert not data["errors"]
+ assert data["checkout"]["voucherCode"] == voucher.code
+
+
+def test_checkout_add_gift_card_without_checkout_email(
+ api_client, checkout_with_item, gift_card
+):
+ # given
+ checkout_with_item.email = None
+ checkout_with_item.save(update_fields=["email"])
+
+ gift_card.expiry_date = date.today() - timedelta(days=1)
+ gift_card.save()
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_item),
+ "promoCode": gift_card.code,
+ }
+
+ # when
data = _mutate_checkout_add_promo_code(api_client, variables)
+ # then
assert data["errors"]
assert data["errors"][0]["code"] == CheckoutErrorCode.EMAIL_NOT_SET.name