Skip to content

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

Fix checkout `line.undiscountedTotalPrice` and `line.undiscountedUnitPrice` calculations (#17193)

Iga Karbowiakfbb4bde6547d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95080639c9..fcf68c707d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,8 @@ All notable, unreleased changes to this project will be documented in this file.
- Add `customerNote` field to `Checkout` type to make it consistent with `Order` model - #16561 by @Air-t
- Add `type` field to `TaxableObjectDiscount` type - #16630 by @zedzior
- Add `productVariants` field to `Product` instead of `variants`. Mark `Product.variants` as deprecated - #16998 by @kadewu
+- Fix checkout `line.undiscountedTotalPrice` and `line.undiscountedUnitPrice` calculation. - #17193 by @IKarbowiak
+ - Return the normalized price in case the checkout prices are not expired, otherwise fetch the price from variant channel listing.
### Webhooks
diff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.py
index 0c73061105..cca51ad220 100644
--- a/saleor/checkout/calculations.py
+++ b/saleor/checkout/calculations.py
@@ -255,6 +255,33 @@ def checkout_line_tax_rate(
return checkout_line_info.line.tax_rate
+def checkout_line_undiscounted_unit_price(
+ *,
+ checkout_info: "CheckoutInfo",
+ checkout_line_info: "CheckoutLineInfo",
+):
+ # Fetch the undiscounted unit price from channel listings in case the prices
+ # are invalidated.
+ if checkout_info.checkout.price_expiration < timezone.now():
+ return base_calculations.calculate_undiscounted_base_line_unit_price(
+ checkout_line_info, checkout_info.channel
+ )
+ currency = checkout_info.checkout.currency
+ return quantize_price(checkout_line_info.line.undiscounted_unit_price, currency)
+
+
+def checkout_line_undiscounted_total_price(
+ *,
+ checkout_info: "CheckoutInfo",
+ checkout_line_info: "CheckoutLineInfo",
+):
+ undiscounted_unit_price = checkout_line_undiscounted_unit_price(
+ checkout_info=checkout_info, checkout_line_info=checkout_line_info
+ )
+ total_price = undiscounted_unit_price * checkout_line_info.line.quantity
+ return quantize_price(total_price, total_price.currency)
+
+
def update_undiscounted_unit_price_for_lines(lines: Iterable["CheckoutLineInfo"]):
"""Update line undiscounted unit price amount.
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 17e68b8a3c..d701a57105 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -3623,6 +3623,170 @@ def test_checkout_prices_voucher_code_that_doesnt_exist_when_line_without_listin
)
+def test_checkout_prices_variant_listing_price_changed(
+ user_api_client, checkout_with_item
+):
+ # given
+ query = QUERY_CHECKOUT_PRICES
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
+ calculations.fetch_checkout_data(
+ checkout_info,
+ manager,
+ lines,
+ checkout_with_item.shipping_address,
+ force_update=True,
+ )
+
+ line = lines[0]
+ listing = line.variant.channel_listings.get(
+ channel_id=checkout_with_item.channel_id
+ )
+ price_amount = Decimal("2.00")
+ listing.discounted_price_amount = price_amount
+ listing.price_amount = price_amount
+ listing.save(update_fields=["price_amount", "discounted_price_amount"])
+
+ variables = {"id": to_global_id_or_none(checkout_with_item)}
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]
+
+ # then
+ assert data["token"] == str(checkout_with_item.token)
+ assert len(data["lines"]) == checkout_with_item.lines.count()
+
+ total = calculations.checkout_total(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout_with_item.shipping_address,
+ )
+ assert data["totalPrice"]["gross"]["amount"] == (total.gross.amount)
+
+ subtotal = calculations.checkout_subtotal(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout_with_item.shipping_address,
+ )
+ assert data["subtotalPrice"]["gross"]["amount"] == (subtotal.gross.amount)
+
+ line_info = lines[0]
+ assert line_info.line.quantity > 0
+ line_total_price = calculations.checkout_line_total(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ checkout_line_info=line_info,
+ )
+ assert (
+ data["lines"][0]["unitPrice"]["gross"]["amount"]
+ == line_total_price.gross.amount / line_info.line.quantity
+ )
+ assert (
+ data["lines"][0]["totalPrice"]["gross"]["amount"]
+ == line_total_price.gross.amount
+ )
+ assert (
+ data["lines"][0]["undiscountedUnitPrice"]["amount"]
+ == line_info.line.undiscounted_unit_price_amount
+ )
+ assert (
+ data["lines"][0]["undiscountedTotalPrice"]["amount"]
+ == line_info.line.undiscounted_unit_price_amount * line_info.line.quantity
+ )
+
+
+def test_checkout_prices_expired_variant_listing_price_changed(
+ user_api_client, checkout_with_item
+):
+ # given
+ query = QUERY_CHECKOUT_PRICES
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
+ calculations.fetch_checkout_data(
+ checkout_info,
+ manager,
+ lines,
+ checkout_with_item.shipping_address,
+ force_update=True,
+ )
+ checkout_with_item.price_expiration = timezone.now() - datetime.timedelta(days=1)
+ checkout_with_item.save(update_fields=["price_expiration"])
+
+ line = lines[0]
+ listing = line.variant.channel_listings.get(
+ channel_id=checkout_with_item.channel_id
+ )
+ price_amount = Decimal("2.00")
+ listing.discounted_price_amount = price_amount
+ listing.price_amount = price_amount
+ listing.save(update_fields=["price_amount", "discounted_price_amount"])
+
+ variables = {"id": to_global_id_or_none(checkout_with_item)}
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]
+
+ # then
+ assert data["token"] == str(checkout_with_item.token)
+ assert len(data["lines"]) == checkout_with_item.lines.count()
+
+ checkout_info.checkout.refresh_from_db()
+ total = calculations.checkout_total(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout_with_item.shipping_address,
+ )
+ assert data["totalPrice"]["gross"]["amount"] == (total.gross.amount)
+
+ subtotal = calculations.checkout_subtotal(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout_with_item.shipping_address,
+ )
+ assert data["subtotalPrice"]["gross"]["amount"] == (subtotal.gross.amount)
+
+ line_info = lines[0]
+ line_info.line.refresh_from_db()
+ assert line_info.line.quantity > 0
+ line_total_price = calculations.checkout_line_total(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ checkout_line_info=line_info,
+ )
+ assert (
+ data["lines"][0]["unitPrice"]["gross"]["amount"]
+ == line_total_price.gross.amount / line_info.line.quantity
+ )
+ assert (
+ data["lines"][0]["totalPrice"]["gross"]["amount"]
+ == line_total_price.gross.amount
+ )
+ assert (
+ data["lines"][0]["undiscountedUnitPrice"]["amount"]
+ == line_info.line.undiscounted_unit_price_amount
+ == price_amount
+ )
+ assert (
+ data["lines"][0]["undiscountedTotalPrice"]["amount"]
+ == line_info.line.undiscounted_unit_price_amount * line_info.line.quantity
+ == price_amount * line_info.line.quantity
+ )
+
+
CHECKOUTS_QUERY = """
{
checkouts(first: 20) {
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index f84b6e74a0..e42c8414b1 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -6,10 +6,6 @@ from promise import Promise
from ...account.models import User
from ...checkout import calculations, models, problems
-from ...checkout.base_calculations import (
- calculate_undiscounted_base_line_total_price,
- calculate_undiscounted_base_line_unit_price,
-)
from ...checkout.calculations import fetch_checkout_data
from ...checkout.utils import get_valid_collection_points_for_checkout
from ...core.db.connection import allow_writer_in_context
@@ -323,8 +319,9 @@ class CheckoutLine(ModelObjectType[models.CheckoutLine]):
) = data
for line_info in lines:
if line_info.line.pk == root.pk:
- return calculate_undiscounted_base_line_unit_price(
- line_info, checkout_info.channel
+ return calculations.checkout_line_undiscounted_unit_price(
+ checkout_info=checkout_info,
+ checkout_line_info=line_info,
)
return None
@@ -406,8 +403,9 @@ class CheckoutLine(ModelObjectType[models.CheckoutLine]):
) = data
for line_info in lines:
if line_info.line.pk == root.pk:
- return calculate_undiscounted_base_line_total_price(
- line_info, checkout_info.channel
+ return calculations.checkout_line_undiscounted_total_price(
+ checkout_info=checkout_info,
+ checkout_line_info=line_info,
)
return None