Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve checkout total base calculations
IKarbowiak527b67f9b7a0
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.pyindex 165b8eeadc..10c6141815 100644--- a/saleor/checkout/base_calculations.py+++ b/saleor/checkout/base_calculations.py@@ -8,10 +8,11 @@ from decimal import Decimalfrom typing import TYPE_CHECKING, Iterable, Optionalfrom prices import TaxedMoney+from stripe import TaxCodefrom ..core.prices import quantize_pricefrom ..core.taxes import zero_money, zero_taxed_money-from ..discount import DiscountInfo+from ..discount import DiscountInfo, VoucherTypefrom ..order.interface import OrderTaxedPricesDatafrom .fetch import CheckoutLineInfo, ShippingMethodInfofrom .interface import CheckoutPricesData, CheckoutTaxedPricesData@@ -224,15 +225,32 @@ def base_checkout_total(shipping_price = base_checkout_delivery_price(checkout_info, lines)discount = checkout_info.checkout.discount- zero = zero_taxed_money(currency)- total = subtotal + shipping_price - discount+ is_shipping_voucher = (+ checkout_info.voucher.type == VoucherType.SHIPPING+ if checkout_info.voucher+ else False+ )# Discount is subtracted from both gross and net values, which may cause negative# net value if we are having a discount that covers whole price.- # Comparing TaxedMoney objects works only on gross values. That is why we are- # explicitly returning zero_taxed_money if total.gross is less or equal zero.- if total.gross <= zero.gross:+ if is_shipping_voucher:+ shipping_price = _return_zero_when_price_is_negative(+ shipping_price - discount, currency+ )+ else:+ subtotal = _return_zero_when_price_is_negative(subtotal - discount, currency)+ return subtotal + shipping_price+++def _return_zero_when_price_is_negative(price: TaxCode, currency: str):+ """Return zero when the price is negative, otherwise return unchanged price.++ Comparing TaxedMoney objects works only on gross values. That is why we are+ explicitly returning zero_taxed_money if total.gross is less or equal zero.+ """+ zero = zero_taxed_money(currency)+ if price.gross <= zero.gross:return zero- return total+ return pricedef base_checkout_lines_total(diff --git a/saleor/checkout/tests/test_base_calculations.py b/saleor/checkout/tests/test_base_calculations.pyindex 6df7b46783..8742f1f4ef 100644--- a/saleor/checkout/tests/test_base_calculations.py+++ b/saleor/checkout/tests/test_base_calculations.py@@ -2,7 +2,6 @@ from decimal import Decimalfrom prices import Money, TaxedMoney-from ...core.taxes import zero_taxed_moneyfrom ...discount import DiscountValueType, VoucherTypefrom ...discount.utils import get_product_discount_on_salefrom ...plugins.manager import get_plugins_manager@@ -825,13 +824,11 @@ def test_base_checkout_total(checkout_with_item, shipping_method, voucher_percenassert total == TaxedMoney(net=expected_price, gross=expected_price)-def test_base_checkout_total_high_discount(+def test_base_checkout_total_high_discount_on_entire_order(checkout_with_item, shipping_method, voucher_percentage):# givenmanager = get_plugins_manager()- checkout_lines, _ = fetch_checkout_lines(checkout_with_item)- checkout_info = fetch_checkout_info(checkout_with_item, checkout_lines, [], manager)currency = checkout_with_item.currencydiscount_amount = Money(100, currency)@@ -840,8 +837,44 @@ def test_base_checkout_total_high_discount(checkout_with_item.discount = discount_amountcheckout_with_item.save()+ shipping_price = shipping_method.channel_listings.get(+ channel=checkout_with_item.channel+ ).price++ checkout_lines, _ = fetch_checkout_lines(checkout_with_item)+ checkout_info = fetch_checkout_info(checkout_with_item, checkout_lines, [], manager)+# whentotal = base_checkout_total(checkout_info, [], checkout_lines)# then- assert total == zero_taxed_money(currency)+ assert total == TaxedMoney(gross=shipping_price, net=shipping_price)+++def test_base_checkout_total_high_discount_on_shipping(+ checkout_with_item, shipping_method, voucher_shipping_type+):+ # given+ manager = get_plugins_manager()++ channel = checkout_with_item.channel+ shipping_price = shipping_method.channel_listings.get(channel=channel).price++ currency = checkout_with_item.currency+ checkout_with_item.shipping_method = shipping_method+ checkout_with_item.voucher_code = voucher_shipping_type.code+ checkout_with_item.discount = shipping_price + Money(10, currency)+ checkout_with_item.save()++ checkout_lines, _ = fetch_checkout_lines(checkout_with_item)+ checkout_info = fetch_checkout_info(checkout_with_item, checkout_lines, [], manager)++ # when+ total = base_checkout_total(checkout_info, [], checkout_lines)++ # then+ variant = checkout_with_item.lines.first().variant+ channel_listing = variant.channel_listings.get(channel=channel)+ net = variant.get_price(variant.product, [], channel, channel_listing)+ expected_price = net * checkout_with_item.lines.first().quantity+ assert total == TaxedMoney(net=expected_price, gross=expected_price)