Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix calculating cart discount
Anna Siwik8975242e8517
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 7ba9a62986..08bccafa0f 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -676,7 +676,7 @@ def get_voucher_for_cart(cart, vouchers=None):return None-def recalculate_cart_discount(cart):+def recalculate_cart_discount(cart, discounts, taxes):"""Recalculate `cart.discount` based on the voucher.Will clear both voucher and discount if the discount is no longer@@ -685,11 +685,14 @@ def recalculate_cart_discount(cart):voucher = get_voucher_for_cart(cart)if voucher is not None:try:- cart.discount_amount = get_voucher_discount_for_cart(voucher, cart)- cart.discount_name = voucher.name- cart.save(update_fields=['discount_amount', 'discount_name'])+ discount = get_voucher_discount_for_cart(voucher, cart)except NotApplicable:remove_voucher_from_cart(cart)+ else:+ subtotal = cart.get_subtotal(discounts, taxes).gross+ cart.discount_amount = min(discount, subtotal)+ cart.discount_name = voucher.name+ cart.save(update_fields=['discount_amount', 'discount_name'])else:remove_voucher_from_cart(cart)diff --git a/saleor/checkout/views/discount.py b/saleor/checkout/views/discount.pyindex 831f03d408..ec2026d454 100644--- a/saleor/checkout/views/discount.py+++ b/saleor/checkout/views/discount.py@@ -11,7 +11,8 @@ from ...discount.models import Voucherfrom ..forms import CartVoucherFormfrom ..models import Cartfrom ..utils import (- get_or_empty_db_cart, recalculate_cart_discount, remove_voucher_from_cart)+ get_or_empty_db_cart, get_taxes_for_cart, recalculate_cart_discount,+ remove_voucher_from_cart)def add_voucher_form(view):@@ -33,7 +34,8 @@ def add_voucher_form(view):# if only discount form was used we clear post for other formsrequest.POST = {}else:- recalculate_cart_discount(cart)+ taxes = get_taxes_for_cart(cart, request.taxes)+ recalculate_cart_discount(cart, request.discounts, taxes)response = view(request, cart)if isinstance(response, TemplateResponse):response.context_data['voucher_form'] = voucher_formdiff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex 424141ceef..c3859f4dd2 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -634,7 +634,7 @@ def test_recalculate_cart_discount(cart_with_voucher, voucher):voucher.discount_value = 10voucher.save()- recalculate_cart_discount(cart_with_voucher)+ recalculate_cart_discount(cart_with_voucher, None, None)assert cart_with_voucher.discount_amount == Money('10.00', 'USD')@@ -645,7 +645,7 @@ def test_recalculate_cart_discount_voucher_not_applicable(voucher.limit = 100voucher.save()- recalculate_cart_discount(cart_with_voucher)+ recalculate_cart_discount(cart_with_voucher, None, None)assert not cart.voucher_codeassert not cart.discount_name@@ -658,7 +658,7 @@ def test_recalculate_cart_discount_expired_voucher(cart_with_voucher, voucher):voucher.end_date = date_yesterdayvoucher.save()- recalculate_cart_discount(cart_with_voucher)+ recalculate_cart_discount(cart_with_voucher, None, None)assert not cart.voucher_codeassert not cart.discount_name