Skip to content

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

Add gift card to checkout in add promo code mutation

fowczarekf8f155de4fa5
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 3a3d58d9d9..a344b6aaef 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -27,6 +27,7 @@ from ..discount.utils import (
get_value_voucher_discount,
increase_voucher_usage,
)
+from ..giftcard.utils import add_gift_card_code_to_checkout
from ..order import events
from ..order.emails import send_order_confirmation
from ..order.models import Order
@@ -786,7 +787,7 @@ def add_promo_code_to_checkout(checkout, promo_code):
if promo_code_is_voucher(promo_code):
add_voucher_code_to_checkout(checkout, promo_code)
elif promo_code_is_gift_card(promo_code):
- raise ValidationError({"promo_code": "Not implemented yet"})
+ add_gift_card_code_to_checkout(checkout, promo_code)
else:
raise ValidationError({"promo_code": "Promo code does not exists."})
@@ -795,7 +796,7 @@ def add_voucher_code_to_checkout(checkout, voucher_code):
try:
voucher = Voucher.objects.active(date=date.today()).get(code=voucher_code)
except Voucher.DoesNotExist:
- raise ValidationError({"promo_code": "Voucher with given code does not exist."})
+ raise ValidationError({"promo_code": "Voucher with given code is invalid."})
try:
add_voucher_to_checkout(voucher, checkout)
except NotApplicable:
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
new file mode 100644
index 0000000000..1223092900
--- /dev/null
+++ b/saleor/giftcard/utils.py
@@ -0,0 +1,15 @@
+from django.core.exceptions import ValidationError
+
+from .models import GiftCard
+
+
+def add_gift_card_code_to_checkout(checkout, promo_code):
+ try:
+ gift_card = GiftCard.objects.get(code=promo_code)
+ except GiftCard.DoesNotExist:
+ raise ValidationError({"promo_code": "Gift card with given code is invalid."})
+ add_gift_card_to_checkout(checkout, gift_card)
+
+
+def add_gift_card_to_checkout(checkout, gift_card):
+ checkout.gift_cards.add(gift_card)