Skip to content

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

Fix CheckoutInfoByCheckoutTokenLoader for checkout with voucher that does not exist (#15933)

Iga Karbowiakc09d8cca4a35
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index b3e2a8b5e4..8671820e50 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -503,6 +503,7 @@ class CheckoutInfoByCheckoutTokenLoader(DataLoader[str, CheckoutInfo]):
voucher_code_map = {
voucher_code.code: voucher_code
for voucher_code in voucher_codes
+ if voucher_code
}
tax_configuration_by_channel_map = {
tax_configuration.channel_id: tax_configuration
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 2d44994866..6f3c825312 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -2103,6 +2103,71 @@ def test_checkout_prices_with_voucher(user_api_client, checkout_with_item_and_vo
)
+def test_checkout_prices_with_voucher_code_that_doesnt_exist(
+ user_api_client, checkout_with_item_and_voucher, voucher
+):
+ # given
+ checkout = checkout_with_item_and_voucher
+ query = QUERY_CHECKOUT_PRICES
+ variables = {"id": to_global_id_or_none(checkout)}
+ voucher.delete()
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]
+
+ # then
+ assert data["token"] == str(checkout.token)
+ assert len(data["lines"]) == checkout.lines.count()
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ total = calculations.checkout_total(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout_info.shipping_address,
+ )
+ assert data["totalPrice"]["gross"]["amount"] == (total.gross.amount)
+ subtotal = calculations.checkout_subtotal(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout_info.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"] == float(
+ quantize_price(
+ line_total_price.gross.amount / line_info.line.quantity, checkout.currency
+ )
+ )
+ assert (
+ data["lines"][0]["totalPrice"]["gross"]["amount"]
+ == line_total_price.gross.amount
+ )
+ undiscounted_unit_price = line_info.variant.get_price(
+ line_info.channel_listing,
+ line_info.line.price_override,
+ )
+ assert (
+ data["lines"][0]["undiscountedUnitPrice"]["amount"]
+ == undiscounted_unit_price.amount
+ )
+ assert (
+ data["lines"][0]["undiscountedTotalPrice"]["amount"]
+ == undiscounted_unit_price.amount * line_info.line.quantity
+ )
+
+
def test_query_checkouts(
checkout_with_item, staff_api_client, permission_manage_checkouts
):