Skip to content

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

Add voucher once per customer

fowczarek9f6dea69f4a5
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 7cc6f9d08d..fe16f2618f 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -35,9 +35,11 @@ from ..core.utils.promo_code import (
from ..discount import VoucherType
from ..discount.models import NotApplicable, Voucher
from ..discount.utils import (
+ add_voucher_usege_by_customer,
decrease_voucher_usage,
get_products_voucher_discount,
increase_voucher_usage,
+ remove_voucher_usege_by_customer,
validate_voucher_for_checkout,
)
from ..giftcard.utils import (
@@ -989,6 +991,8 @@ def _get_voucher_data_for_order(checkout):
return {}
increase_voucher_usage(voucher)
+ if voucher.apply_once_per_customer:
+ add_voucher_usege_by_customer(voucher, checkout.get_customer_email())
return {
"voucher": voucher,
"discount_amount": checkout.discount_amount,
@@ -1029,7 +1033,7 @@ def _process_user_data_for_order(checkout):
return {
"user": checkout.user,
- "user_email": checkout.user.email if checkout.user else checkout.email,
+ "user_email": checkout.get_customer_email(),
"billing_address": billing_address,
"customer_note": checkout.note,
}
@@ -1130,7 +1134,10 @@ def prepare_order_data(*, checkout: Checkout, tracking_code: str, discounts) ->
def abort_order_data(order_data: dict):
if "voucher" in order_data:
- decrease_voucher_usage(order_data["voucher"])
+ voucher = order_data["voucher"]
+ decrease_voucher_usage(voucher)
+ if "user_email" in order_data:
+ remove_voucher_usege_by_customer(voucher, order_data["user_email"])
@transaction.atomic
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index a3d3649b34..a7a6a03538 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -59,6 +59,7 @@ class Voucher(models.Model):
# this field indicates if discount should be applied per order or
# individually to every item
apply_once_per_order = models.BooleanField(default=False)
+ apply_once_per_customer = models.BooleanField(default=False)
discount_value_type = models.CharField(
max_length=10,
choices=DiscountValueType.CHOICES,
@@ -167,6 +168,27 @@ class Voucher(models.Model):
min_checkout_items_quantity=min_checkout_items_quantity,
)
+ def validate_once_per_customer(self, customer_email):
+ voucher_customer = VoucherCustomer.objects.filter(
+ voucher=self, customer_email=customer_email
+ )
+ if voucher_customer:
+ msg = pgettext(
+ "Voucher not applicable",
+ ("This offer is valid only once per customer."),
+ )
+ raise NotApplicable(msg)
+
+
+class VoucherCustomer(models.Model):
+ voucher = models.ForeignKey(
+ Voucher, related_name="customers", on_delete=models.CASCADE
+ )
+ customer_email = models.EmailField()
+
+ class Meta:
+ unique_together = (("voucher", "customer_email"),)
+
class SaleQueryset(models.QuerySet):
def active(self, date):
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index 69dd5ba86b..d7b156c1ef 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -8,7 +8,7 @@ from django.utils.translation import pgettext
from ..core.taxes import zero_money
from ..core.taxes.interface import calculate_checkout_subtotal
from . import DiscountInfo
-from .models import NotApplicable, Sale
+from .models import NotApplicable, Sale, VoucherCustomer
def increase_voucher_usage(voucher):
@@ -23,6 +23,28 @@ def decrease_voucher_usage(voucher):
voucher.save(update_fields=["used"])
+def add_voucher_usege_by_customer(voucher, customer_email):
+ voucher_customer = VoucherCustomer.objects.filter(
+ voucher=voucher, customer_email=customer_email
+ )
+ if voucher_customer:
+ raise NotApplicable(
+ pgettext(
+ "Voucher not applicable",
+ ("This offer is only valid once per customer."),
+ )
+ )
+ VoucherCustomer.objects.create(voucher=voucher, customer_email=customer_email)
+
+
+def remove_voucher_usege_by_customer(voucher, customer_email):
+ voucher_customer = VoucherCustomer.objects.filter(
+ voucher=voucher, customer_email=customer_email
+ )
+ if voucher_customer:
+ voucher_customer.delete()
+
+
def are_product_collections_on_sale(product, discount: DiscountInfo):
"""Check if any collection is on sale."""
discounted_collections = discount.collection_ids
@@ -64,17 +86,22 @@ def calculate_discounted_price(product, price, discounts: Iterable[DiscountInfo]
def validate_voucher_for_checkout(voucher, checkout, discounts):
subtotal = calculate_checkout_subtotal(checkout, discounts)
- validate_voucher(voucher, subtotal.gross, checkout.quantity)
+ customer_email = checkout.get_customer_email()
+ validate_voucher(voucher, subtotal.gross, checkout.quantity, customer_email)
def validate_voucher_in_order(order):
subtotal = order.get_subtotal()
- validate_voucher(order.voucher, subtotal.gross, order.get_total_quantity())
+ quantity = order.get_total_quantity()
+ customer_email = order.get_customer_email()
+ validate_voucher(order.voucher, subtotal.gross, quantity, customer_email)
-def validate_voucher(voucher, total_price, quantity):
+def validate_voucher(voucher, total_price, quantity, customer_email):
voucher.validate_min_amount_spent(total_price)
voucher.validate_min_checkout_items_quantity(quantity)
+ if voucher.apply_once_per_customer:
+ voucher.validate_once_per_customer(customer_email)
def get_products_voucher_discount(voucher, prices):