Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use min_quantity_of_products in voucher validations
fowczarek92a403e55689
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex aabeb91b84..f8758ef34e 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -767,10 +767,11 @@ def _get_shipping_voucher_discount_for_checkout(voucher, checkout, discounts=Nonvoucher,calculate_checkout_subtotal(checkout, discounts).gross,calculate_checkout_shipping(checkout, discounts).gross,+ checkout.quantity,)-def _get_products_voucher_discount(checkout, voucher):+def _get_products_voucher_discount(checkout, voucher, discounts=None):"""Calculate products discount value for a voucher, depending on its type."""prices = None@@ -791,7 +792,12 @@ def _get_products_voucher_discount(checkout, voucher):"Voucher not applicable", "This offer is only valid for selected items.")raise NotApplicable(msg)- return get_products_voucher_discount(voucher, prices)+ return get_products_voucher_discount(+ voucher,+ prices,+ calculate_checkout_subtotal(checkout, discounts).gross,+ checkout.quantity,+ )def get_voucher_discount_for_checkout(voucher, checkout, discounts=None):@@ -801,7 +807,9 @@ def get_voucher_discount_for_checkout(voucher, checkout, discounts=None):"""if voucher.type == VoucherType.ENTIRE_ORDER:return get_value_voucher_discount(- voucher, calculate_checkout_subtotal(checkout, discounts).gross+ voucher,+ calculate_checkout_subtotal(checkout, discounts).gross,+ checkout.quantity,)if voucher.type == VoucherType.SHIPPING:return _get_shipping_voucher_discount_for_checkout(voucher, checkout, discounts)@@ -811,7 +819,7 @@ def get_voucher_discount_for_checkout(voucher, checkout, discounts=None):VoucherType.CATEGORY,VoucherType.SPECIFIC_PRODUCT,):- return _get_products_voucher_discount(checkout, voucher)+ return _get_products_voucher_discount(checkout, voucher, discounts)raise NotImplementedError("Unknown discount type")diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex 52b08344cf..7509c9b910 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -128,7 +128,7 @@ class ShippingVoucherForm(forms.ModelForm):class Meta:model = Voucher- fields = ["countries", "min_amount_spent"]+ fields = ["countries", "min_amount_spent", "min_quantity_of_products"]class EntireOrderVoucherForm(forms.ModelForm):@@ -136,7 +136,7 @@ class EntireOrderVoucherForm(forms.ModelForm):class Meta:model = Voucher- fields = ["min_amount_spent"]+ fields = ["min_amount_spent", "min_quantity_of_products"]def save(self, commit=True):self.instance.category = None@@ -187,6 +187,7 @@ class SpecificProductVoucherForm(CommonVoucherForm):"categories","apply_once_per_order","min_amount_spent",+ "min_quantity_of_products",]labels = {"collections": pgettext_lazy(@@ -212,7 +213,12 @@ class ProductVoucherForm(CommonVoucherForm):class Meta:model = Voucher- fields = ["products", "apply_once_per_order", "min_amount_spent"]+ fields = [+ "products",+ "apply_once_per_order",+ "min_amount_spent",+ "min_quantity_of_products",+ ]def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)@@ -223,7 +229,12 @@ class ProductVoucherForm(CommonVoucherForm):class CollectionVoucherForm(CommonVoucherForm):class Meta:model = Voucher- fields = ["collections", "apply_once_per_order", "min_amount_spent"]+ fields = [+ "collections",+ "apply_once_per_order",+ "min_amount_spent",+ "min_quantity_of_products",+ ]labels = {"collections": pgettext_lazy("Collections", "Collections")}def __init__(self, *args, **kwargs):@@ -240,4 +251,9 @@ class CategoryVoucherForm(CommonVoucherForm):class Meta:model = Voucher- fields = ["categories", "apply_once_per_order", "min_amount_spent"]+ fields = [+ "categories",+ "apply_once_per_order",+ "min_amount_spent",+ "min_quantity_of_products",+ ]diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.pyindex 05e3999bf9..e2c8ed0915 100644--- a/saleor/dashboard/order/utils.py+++ b/saleor/dashboard/order/utils.py@@ -140,7 +140,9 @@ def get_products_voucher_discount_for_order(order, voucher):"Voucher not applicable", "This offer is only valid for selected items.")raise NotApplicable(msg)- return get_products_voucher_discount(voucher, prices)+ return get_products_voucher_discount(+ voucher, prices, order.get_subtotal().gross, order.get_total_quantity()+ )def get_voucher_discount_for_order(order):@@ -152,10 +154,15 @@ def get_voucher_discount_for_order(order):return zero_money()subtotal = order.get_subtotal()if order.voucher.type == VoucherType.ENTIRE_ORDER:- return get_value_voucher_discount(order.voucher, subtotal.gross)+ return get_value_voucher_discount(+ order.voucher, subtotal.gross, order.get_total_quantity()+ )if order.voucher.type == VoucherType.SHIPPING:return get_shipping_voucher_discount(- order.voucher, subtotal.gross, order.shipping_price+ order.voucher,+ subtotal.gross,+ order.shipping_price,+ order.get_total_quantity(),)if order.voucher.type in (VoucherType.PRODUCT,diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 0fc10a0e51..50831d152a 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -152,7 +152,7 @@ class Voucher(models.Model):def validate_min_quantity_of_products(self, quantity):min_quantity_of_products = self.min_quantity_of_products- if min_quantity_of_products > quantity:+ if min_quantity_of_products and min_quantity_of_products > quantity:msg = pgettext("Voucher not applicable",(diff --git a/saleor/discount/utils.py b/saleor/discount/utils.pyindex 800c9caab3..3b1f303ef8 100644--- a/saleor/discount/utils.py+++ b/saleor/discount/utils.py@@ -61,20 +61,24 @@ def calculate_discounted_price(product, price, discounts: Iterable[DiscountInfo]return price-def get_value_voucher_discount(voucher, total_price):+def get_value_voucher_discount(voucher, total_price, quantity):"""Calculate discount value for a voucher of value type."""voucher.validate_min_amount_spent(total_price)+ voucher.validate_min_quantity_of_products(quantity)return voucher.get_discount_amount_for(total_price)-def get_shipping_voucher_discount(voucher, total_price, shipping_price):+def get_shipping_voucher_discount(voucher, total_price, shipping_price, quantity):"""Calculate discount value for a voucher of shipping type."""voucher.validate_min_amount_spent(total_price)+ voucher.validate_min_quantity_of_products(quantity)return voucher.get_discount_amount_for(shipping_price)-def get_products_voucher_discount(voucher, prices):+def get_products_voucher_discount(voucher, prices, total_price, quantity):"""Calculate discount value for a voucher of product or category type."""+ voucher.validate_min_amount_spent(total_price)+ voucher.validate_min_quantity_of_products(quantity)if voucher.apply_once_per_order:return voucher.get_discount_amount_for(min(prices))discounts = (voucher.get_discount_amount_for(price) for price in prices)