Skip to content

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

Calculate voucher discount for checkout in utils

Anna Siwik0a88d5862efb
diff --git a/saleor/discount/forms.py b/saleor/discount/forms.py
index a501243877..81c5b8bd84 100644
--- a/saleor/discount/forms.py
+++ b/saleor/discount/forms.py
@@ -5,6 +5,7 @@ from django.utils.encoding import smart_text
from django.utils.translation import pgettext_lazy
from .models import NotApplicable, Voucher
+from .utils import get_voucher_discount_for_checkout
class VoucherField(forms.ModelChoiceField):
@@ -41,7 +42,8 @@ class CheckoutDiscountForm(forms.Form):
if 'voucher' in cleaned_data:
voucher = cleaned_data['voucher']
try:
- discount = voucher.get_discount_for_checkout(self.checkout)
+ discount = get_voucher_discount_for_checkout(
+ voucher, self.checkout)
cleaned_data['discount'] = discount
except NotApplicable as e:
self.add_error('voucher', smart_text(e))
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 4a88ddca83..50cd53a52a 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -137,59 +137,6 @@ class Voucher(models.Model):
'This offer is only valid for orders over %(amount)s.')
raise NotApplicable(msg % {'amount': net(limit)}, limit=limit)
- def get_discount_for_checkout(self, checkout):
- if self.type == VoucherType.VALUE:
- cart_total = checkout.get_subtotal()
- self.validate_limit(cart_total)
- return self.get_fixed_discount_for(cart_total)
- if self.type == VoucherType.SHIPPING:
- if not checkout.is_shipping_required:
- msg = pgettext(
- 'Voucher not applicable',
- 'Your order does not require shipping.')
- raise NotApplicable(msg)
- shipping_method = checkout.shipping_method
- if not shipping_method:
- msg = pgettext(
- 'Voucher not applicable',
- 'Please select a shipping method first.')
- raise NotApplicable(msg)
- if (self.apply_to and
- shipping_method.country_code != self.apply_to):
- msg = pgettext(
- 'Voucher not applicable',
- 'This offer is only valid in %(country)s.')
- raise NotApplicable(
- msg % {'country': self.get_apply_to_display()})
- cart_total = checkout.get_subtotal()
- self.validate_limit(cart_total)
- return self.get_fixed_discount_for(shipping_method.price)
- if self.type in (VoucherType.PRODUCT, VoucherType.CATEGORY):
- if self.type == VoucherType.PRODUCT:
- prices = [
- item[1] for item in get_product_variants_and_prices(
- checkout.cart, self.product)]
- else:
- prices = [
- item[1] for item in get_category_variants_and_prices(
- checkout.cart, self.category)]
- if not prices:
- msg = pgettext(
- 'Voucher not applicable',
- 'This offer is only valid for selected items.')
- raise NotApplicable(msg)
- if self.apply_to == VoucherApplyToProduct.ALL_PRODUCTS:
- discounts = (
- self.get_fixed_discount_for(price) for price in prices)
- discount_total = sum(
- (discount.amount for discount in discounts),
- Price(0, currency=settings.DEFAULT_CURRENCY))
- return FixedDiscount(discount_total, smart_text(self))
- product_total = sum(
- prices, Price(0, currency=settings.DEFAULT_CURRENCY))
- return self.get_fixed_discount_for(product_total)
- raise NotImplementedError('Unknown discount type')
-
class Sale(models.Model):
name = models.CharField(max_length=255)
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index f09a622f7b..19e6cbaadb 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -1,7 +1,14 @@
+from django.conf import settings
from django.db.models import F
+from django.utils.encoding import smart_text
from django.utils.translation import pgettext
+from prices import FixedDiscount, Price
+
+from ..cart.utils import (
+ get_category_variants_and_prices, get_product_variants_and_prices)
from .models import NotApplicable
+from . import VoucherApplyToProduct, VoucherType
def increase_voucher_usage(voucher):
@@ -49,3 +56,58 @@ def get_product_discount_on_sale(sale, product):
pgettext(
'Voucher not applicable',
'Discount not applicable for this product'))
+
+
+def get_voucher_discount_for_checkout(voucher, checkout):
+ if voucher.type == VoucherType.VALUE:
+ cart_total = checkout.get_subtotal()
+ voucher.validate_limit(cart_total)
+ return voucher.get_fixed_discount_for(cart_total)
+ if voucher.type == VoucherType.SHIPPING:
+ if not checkout.is_shipping_required:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'Your order does not require shipping.')
+ raise NotApplicable(msg)
+ shipping_method = checkout.shipping_method
+ if not shipping_method:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'Please select a shipping method first.')
+ raise NotApplicable(msg)
+ if (voucher.apply_to and
+ shipping_method.country_code != voucher.apply_to):
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'This offer is only valid in %(country)s.')
+ raise NotApplicable(
+ msg % {'country': voucher.get_apply_to_display()})
+ cart_total = checkout.get_subtotal()
+ voucher.validate_limit(cart_total)
+ return voucher.get_fixed_discount_for(shipping_method.price)
+ if voucher.type in (VoucherType.PRODUCT, VoucherType.CATEGORY):
+ if voucher.type == VoucherType.PRODUCT:
+ prices = [
+ item[1] for item in get_product_variants_and_prices(
+ checkout.cart, voucher.product)]
+ else:
+ prices = [
+ item[1] for item in get_category_variants_and_prices(
+ checkout.cart, voucher.category)]
+ if not prices:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'This offer is only valid for selected items.')
+ raise NotApplicable(msg)
+ if voucher.apply_to == VoucherApplyToProduct.ALL_PRODUCTS:
+ discounts = (
+ voucher.get_fixed_discount_for(price) for price in prices)
+ discount_total = sum(
+ (discount.amount for discount in discounts),
+ Price(0, currency=settings.DEFAULT_CURRENCY))
+ return FixedDiscount(discount_total, smart_text(voucher))
+ product_total = sum(
+ prices, Price(0, currency=settings.DEFAULT_CURRENCY))
+ return voucher.get_fixed_discount_for(product_total)
+ raise NotImplementedError('Unknown discount type')
+
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index da02acb1c5..020da56669 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -12,7 +12,7 @@ from saleor.discount.forms import CheckoutDiscountForm
from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.discount.utils import (
decrease_voucher_usage, increase_voucher_usage,
- get_product_discount_on_sale)
+ get_product_discount_on_sale, get_voucher_discount_for_checkout)
from saleor.product.models import Product, ProductVariant
@@ -80,7 +80,7 @@ def test_value_voucher_checkout_discount(settings, total, discount_value,
limit=Price(limit, currency='USD') if limit is not None else None)
checkout = Mock(get_subtotal=Mock(return_value=Price(total,
currency='USD')))
- discount = voucher.get_discount_for_checkout(checkout)
+ discount = get_voucher_discount_for_checkout(voucher, checkout)
assert discount.amount == Price(expected_value, currency='USD')
@@ -93,7 +93,7 @@ def test_value_voucher_checkout_discount_not_applicable(settings):
checkout = Mock(get_subtotal=Mock(
return_value=Price(10, currency='USD')))
with pytest.raises(NotApplicable) as e:
- voucher.get_discount_for_checkout(checkout)
+ get_voucher_discount_for_checkout(voucher, checkout)
assert e.value.limit == Price(100, currency='USD')
@@ -117,7 +117,7 @@ def test_shipping_voucher_checkout_discount(
discount_value=discount_value,
apply_to=apply_to,
limit=None)
- discount = voucher.get_discount_for_checkout(checkout)
+ discount = get_voucher_discount_for_checkout(voucher, checkout)
assert discount.amount == Price(expected_value, currency='USD')
@@ -149,14 +149,14 @@ def test_shipping_voucher_checkout_discountnot_applicable(
limit=Price(limit, currency='USD') if limit is not None else None,
apply_to=apply_to)
with pytest.raises(NotApplicable) as e:
- voucher.get_discount_for_checkout(checkout)
+ get_voucher_discount_for_checkout(voucher, checkout)
assert str(e.value) == error_msg
def test_product_voucher_checkout_discount_not_applicable(settings,
monkeypatch):
monkeypatch.setattr(
- 'saleor.discount.models.get_product_variants_and_prices',
+ 'saleor.discount.utils.get_product_variants_and_prices',
lambda cart, product: [])
voucher = Voucher(
code='unique', type=VoucherType.PRODUCT,
@@ -165,14 +165,14 @@ def test_product_voucher_checkout_discount_not_applicable(settings,
checkout = Mock(cart=Mock())
with pytest.raises(NotApplicable) as e:
- voucher.get_discount_for_checkout(checkout)
+ get_voucher_discount_for_checkout(voucher, checkout)
assert str(e.value) == 'This offer is only valid for selected items.'
def test_category_voucher_checkout_discount_not_applicable(settings,
monkeypatch):
monkeypatch.setattr(
- 'saleor.discount.models.get_category_variants_and_prices',
+ 'saleor.discount.utils.get_category_variants_and_prices',
lambda cart, product: [])
voucher = Voucher(
code='unique', type=VoucherType.CATEGORY,
@@ -180,7 +180,7 @@ def test_category_voucher_checkout_discount_not_applicable(settings,
discount_value=10)
checkout = Mock(cart=Mock())
with pytest.raises(NotApplicable) as e:
- voucher.get_discount_for_checkout(checkout)
+ get_voucher_discount_for_checkout(voucher, checkout)
assert str(e.value) == 'This offer is only valid for selected items.'
@@ -188,7 +188,7 @@ def test_invalid_checkout_discount_form(monkeypatch, voucher):
checkout = Mock(cart=Mock())
form = CheckoutDiscountForm({'voucher': voucher.code}, checkout=checkout)
monkeypatch.setattr(
- 'saleor.discount.models.Voucher.get_discount_for_checkout',
+ 'saleor.discount.forms.get_voucher_discount_for_checkout',
Mock(side_effect=NotApplicable('Not applicable')))
assert not form.is_valid()
assert 'voucher' in form.errors
@@ -276,7 +276,7 @@ def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,
discount_value, discount_type,
apply_to, expected_value):
monkeypatch.setattr(
- 'saleor.discount.models.get_product_variants_and_prices',
+ 'saleor.discount.utils.get_product_variants_and_prices',
lambda cart, product: (
(None, Price(p, currency='USD')) for p in prices))
voucher = Voucher(
@@ -285,7 +285,7 @@ def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,
discount_value=discount_value,
apply_to=apply_to)
checkout = Mock(cart=Mock())
- discount = voucher.get_discount_for_checkout(checkout)
+ discount = get_voucher_discount_for_checkout(voucher, checkout)
assert discount.amount == Price(expected_value, currency='USD')