Skip to content

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

Fix using discounts in subtotal calculation

Marcin Gębala29fb56431c9d
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 4fa8425126..cc95897424 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -106,9 +106,9 @@ class Checkout(CountableDjangoObjectType):
return root.get_total(discounts=info.context.discounts, taxes=taxes)
@staticmethod
- def resolve_subtotal_price(root: models.Checkout, *_args):
+ def resolve_subtotal_price(root: models.Checkout, info):
taxes = get_taxes_for_address(root.shipping_address)
- return root.get_subtotal(taxes=taxes)
+ return root.get_subtotal(discounts=info.context.discounts, taxes=taxes)
@staticmethod
def resolve_shipping_price(root: models.Checkout, *_args):
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 62f62bb1ea..1c6bbc839b 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -1452,3 +1452,58 @@ def test_checkout_shipping_address_update_with_not_applicable_voucher(
assert checkout_with_item.shipping_address.country == new_address["country"]
assert checkout_with_item.voucher_code is None
+
+
+def test_checkout_totals_use_discounts(api_client, checkout_with_item, sale):
+ query = """
+ query getCheckout($token: UUID) {
+ checkout(token: $token) {
+ lines {
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ }
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ subtotalPrice {
+ gross {
+ amount
+ }
+ }
+ }
+ }
+ """
+ checkout = checkout_with_item
+ variables = {"token": str(checkout.token)}
+ response = api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]
+
+ discounts = [sale]
+
+ # make sure that we're testing a variant that is actually on sale
+ variant = checkout.lines.first().variant
+ assert (
+ variant.get_price().gross.amount
+ == variant.get_price(discounts=discounts).gross.amount + sale.value
+ )
+
+ assert (
+ data["totalPrice"]["gross"]["amount"]
+ == checkout.get_total(discounts=discounts).gross.amount
+ )
+ assert (
+ data["subtotalPrice"]["gross"]["amount"]
+ == checkout.get_subtotal(discounts=discounts).gross.amount
+ )
+
+ line = checkout.lines.first()
+ assert (
+ data["lines"][0]["totalPrice"]["gross"]["amount"]
+ == line.get_total(discounts=discounts).gross.amount
+ )