Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Split calculating discount for checkout
Anna Siwikca6f56362e5d
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex 30984a7cc2..0748066d67 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -13,7 +13,8 @@ from ..cart.models import Cartfrom ..cart.utils import get_or_empty_db_cartfrom ..core import analyticsfrom ..discount.models import NotApplicable, Voucher-from ..discount.utils import increase_voucher_usage+from ..discount.utils import (+ increase_voucher_usage, get_voucher_discount_for_checkout)from ..order.models import Orderfrom ..shipping.models import ANY_COUNTRY, ShippingMethodCountryfrom ..userprofile.models import Address@@ -367,7 +368,8 @@ class Checkout:voucher = self._get_voucher()if voucher is not None:try:- self.discount = voucher.get_discount_for_checkout(self)+ self.discount = get_voucher_discount_for_checkout(+ voucher, self)except NotApplicable:del self.discountdel self.voucher_codediff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 50cd53a52a..f21e260c03 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -170,4 +170,3 @@ class Sale(models.Model):if self.type == DiscountValueType.PERCENTAGE:return percentage_discount(value=self.value, name=self.name)raise NotImplementedError('Unknown discount type')-diff --git a/saleor/discount/utils.py b/saleor/discount/utils.pyindex 19e6cbaadb..03dae42a51 100644--- a/saleor/discount/utils.py+++ b/saleor/discount/utils.py@@ -58,56 +58,69 @@ def get_product_discount_on_sale(sale, product):'Discount not applicable for this product'))+def _get_value_voucher_discount_for_checkout(voucher, checkout):+ cart_total = checkout.get_subtotal()+ voucher.validate_limit(cart_total)+ return voucher.get_fixed_discount_for(cart_total)+++def _get_shipping_voucher_discount_for_checkout(voucher, checkout):+ 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)+ not_valid_for_country = (+ voucher.apply_to and shipping_method.country_code != voucher.apply_to)+ if not_valid_for_country:+ 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)+++def _get_product_or_category_voucher_discount_for_checkout(voucher, checkout):+ 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)++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)+ return _get_value_voucher_discount_for_checkout(voucher, checkout)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)+ return _get_shipping_voucher_discount_for_checkout(voucher, checkout)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)+ return _get_product_or_category_voucher_discount_for_checkout(+ voucher, checkout)raise NotImplementedError('Unknown discount type')-diff --git a/tests/test_discounts.py b/tests/test_discounts.pyindex 020da56669..e77805ec8c 100644--- a/tests/test_discounts.py+++ b/tests/test_discounts.py@@ -136,7 +136,7 @@ def test_shipping_voucher_checkout_discount((True, Mock(price=Price(10, currency='USD')), 10,DiscountValueType.FIXED, None, 5, Price(2, currency='USD'),'This offer is only valid for orders over $5.00.')])-def test_shipping_voucher_checkout_discountnot_applicable(+def test_shipping_voucher_checkout_discount_not_applicable(settings, is_shipping_required, shipping_method, discount_value,discount_type, apply_to, limit, subtotal, error_msg):checkout = Mock(is_shipping_required=is_shipping_required,