Skip to content

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

Use use_legacy_line_voucher_propagation_for_order to control entire-order propagation (#17622)

Maciej Korycinskia4fd5f5fb8d0
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 9e1edf5444..1e10fdd32a 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -33,7 +33,7 @@ from ..discount.utils.voucher import (
calculate_line_discount_amount_from_voucher,
increase_voucher_usage,
is_line_level_voucher,
- is_shipping_voucher,
+ is_order_level_voucher,
release_voucher_code_usage,
)
from ..graphql.checkout.utils import (
@@ -346,16 +346,9 @@ def _create_line_for_order(
prices_entered_with_tax,
)
- discount_price = undiscounted_unit_price - unit_price
- if prices_entered_with_tax:
- discount_amount = discount_price.gross
- else:
- discount_amount = discount_price.net
-
voucher_code = checkout_info.checkout.voucher_code
is_line_voucher_code = bool(checkout_line_info.voucher)
- tax_class = None
if product.tax_class_id:
tax_class = product.tax_class
else:
@@ -363,6 +356,15 @@ def _create_line_for_order(
is_price_overridden = checkout_line.price_override is not None
+ discount_amount = _get_unit_discount(
+ unit_price=unit_price,
+ undiscounted_unit_price=undiscounted_unit_price,
+ base_unit_price=base_unit_price,
+ undiscounted_base_unit_price=undiscounted_base_unit_price,
+ use_legacy_voucher_propagation=checkout_info.channel.use_legacy_line_voucher_propagation_for_order,
+ prices_entered_with_tax=prices_entered_with_tax,
+ )
+
line = OrderLine( # type: ignore[misc] # see below:
product_name=product_name,
variant_name=variant_name,
@@ -395,11 +397,15 @@ def _create_line_for_order(
line_discounts = _create_order_line_discounts(
checkout_line_info, line, checkout_info.channel, voucher_channel_listing
)
+
line.unit_discount_reason = _get_unit_discount_reason(
- voucher_code,
- is_line_voucher_code,
- is_shipping_voucher(checkout_info.voucher),
line_discounts,
+ order_lvl_voucher_code=(
+ checkout_info.voucher.code
+ if checkout_info.voucher and is_order_level_voucher(checkout_info.voucher)
+ else None
+ ),
+ use_legacy_voucher_propagation=checkout_info.channel.use_legacy_line_voucher_propagation_for_order,
)
if line_discounts:
@@ -422,23 +428,48 @@ def _create_line_for_order(
return line_info
+def _get_unit_discount(
+ unit_price: TaxedMoney,
+ undiscounted_unit_price: TaxedMoney,
+ base_unit_price: Money,
+ undiscounted_base_unit_price: Money,
+ use_legacy_voucher_propagation: bool,
+ prices_entered_with_tax: bool,
+) -> Money:
+ """Returng the discount applicable on single line.
+
+ When `use_legacy_voucher_propagation` is `True`, the discount
+ amount includes the `ENTIRE_ORDER` voucher discount. This is
+ already reflected in the order-level discounts, but to maintain
+ backward compatibility, the legacy flow behaves as before.
+
+ When `use_legacy_voucher_propagation` is `False`, the
+ `unit_discount` includes only the order-line level discounts.
+ """
+ if use_legacy_voucher_propagation:
+ discount_price = undiscounted_unit_price - unit_price
+ if prices_entered_with_tax:
+ return discount_price.gross
+ return discount_price.net
+ return undiscounted_base_unit_price - base_unit_price
+
+
def _get_unit_discount_reason(
- voucher_code: str | None,
- is_line_voucher_code: bool,
- is_shipping_voucher: bool,
line_discounts: list[OrderLineDiscount],
+ order_lvl_voucher_code: str | None,
+ use_legacy_voucher_propagation: bool,
) -> str | None:
- voucher_not_applicable = not voucher_code or is_shipping_voucher
- if voucher_not_applicable and not line_discounts:
+ include_entire_order_lvl_reason = (
+ order_lvl_voucher_code and use_legacy_voucher_propagation
+ )
+ if not include_entire_order_lvl_reason and not line_discounts:
return None
+
reasons = []
- if not is_line_voucher_code and voucher_code:
- reasons.append(f"Entire order voucher code: {voucher_code}")
+ if include_entire_order_lvl_reason:
+ reasons.append(f"Entire order voucher code: {order_lvl_voucher_code}")
reasons.extend([discount.reason for discount in line_discounts if discount.reason])
-
- if not reasons:
- return None
return " & ".join(reasons)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
index 50fc4d6d3a..74612092b5 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
@@ -909,8 +909,22 @@ def test_checkout_complete_requires_confirmation(
recalculate_with_plugins_mock.assert_not_called()
+ (
+ "legacy_voucher_propagation",
+ "expected_unit_discount_amount",
+ "expected_unit_discount_reason",
+ ),
+ [
+ (True, Decimal(1), "Entire order voucher code: saleor"),
+ (False, Decimal(0), None),
+ ],
+)
@pytest.mark.integration
def test_checkout_with_voucher_complete(
+ legacy_voucher_propagation,
+ expected_unit_discount_amount,
+ expected_unit_discount_reason,
user_api_client,
checkout_with_voucher_percentage,
voucher_percentage,
@@ -919,6 +933,10 @@ def test_checkout_with_voucher_complete(
shipping_method,
):
# given
+ channel = checkout_with_voucher_percentage.channel
+ channel.use_legacy_line_voucher_propagation_for_order = legacy_voucher_propagation
+ channel.save()
+
code = voucher_percentage.codes.first()
voucher_used_count = code.used
voucher_percentage.usage_limit = voucher_used_count + 1
@@ -995,11 +1013,9 @@ def test_checkout_with_voucher_complete(
order_payment = order.payments.first()
assert order_payment == payment
assert payment.transactions.count() == 1
- assert (
- order_line.unit_discount_amount
- == (discount_amount / checkout_line_quantity).amount
- )
- assert order_line.unit_discount_reason
+
+ assert order_line.unit_discount_amount == expected_unit_discount_amount
+ assert order_line.unit_discount_reason == expected_unit_discount_reason
code.refresh_from_db()
assert code.used == voucher_used_count + 1
@@ -1017,6 +1033,110 @@ def test_checkout_with_voucher_complete(
)
+ (
+ "legacy_propagation",
+ "expected_unit_discount_amount",
+ ),
+ [
+ (True, Decimal("1.67")),
+ (False, Decimal(0)),
+ ],
+)
+def test_checkout_with_order_promotion_complete(
+ legacy_propagation,
+ expected_unit_discount_amount,
+ user_api_client,
+ checkout_with_item_and_order_discount,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_item_and_order_discount
+
+ channel = checkout.channel
+ channel.use_legacy_line_voucher_propagation_for_order = legacy_propagation
+ channel.save()
+
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.save()
+
+ discount_amount = checkout.discount
+
+ checkout_line = checkout.lines.first()
+ checkout_line_quantity = checkout_line.quantity
+ checkout_line_variant = checkout_line.variant
+
+ 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=address
+ )
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ assert not payment.transactions.exists()
+
+ orders_count = Order.objects.count()
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "redirectUrl": "https://www.example.com",
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order_token = data["order"]["token"]
+ order_id = data["order"]["id"]
+ assert Order.objects.count() == orders_count + 1
+ order = Order.objects.first()
+ assert str(order.id) == order_token
+ assert order_id == graphene.Node.to_global_id("Order", order.id)
+ assert order.metadata == checkout.metadata_storage.metadata
+ assert order.private_metadata == checkout.metadata_storage.private_metadata
+
+ subtotal = get_subtotal(order.lines.all(), order.currency)
+ assert order.subtotal == subtotal
+ assert data["order"]["subtotal"]["gross"]["amount"] == subtotal.gross.amount
+ assert order.total == total
+ assert order.undiscounted_total == total + discount_amount
+
+ order_line = order.lines.first()
+ assert checkout_line_quantity == order_line.quantity
+ assert checkout_line_variant == order_line.variant
+ assert order.shipping_address == address
+ assert order.shipping_method == checkout.shipping_method
+ assert order.payments.exists()
+ order_payment = order.payments.first()
+ assert order_payment == payment
+ assert payment.transactions.count() == 1
+
+ assert order_line.unit_discount_amount == expected_unit_discount_amount
+ assert order_line.unit_discount_reason is None
+
+ order_discount = order.discounts.filter(type=DiscountType.ORDER_PROMOTION).first()
+ assert order_discount
+ assert (
+ order_discount.amount_value
+ == (order.undiscounted_total - order.total).gross.amount
+ )
+
+
@pytest.mark.parametrize(
(
"use_legacy_voucher_propagation",
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index eb257d0c50..b4e3ff50e8 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -1943,8 +1943,22 @@ def test_checkout_complete_with_variant_without_sku(
assert order_line.product_variant_id == order_line.variant.get_global_id()
+ (
+ "legacy_voucher_propagation",
+ "expected_unit_discount_amount",
+ "expected_unit_discount_reason",
+ ),
+ [
+ (True, Decimal(1), "Entire order voucher code: saleor"),
+ (False, Decimal(0), None),
+ ],
+)
@pytest.mark.integration
def test_checkout_with_voucher_complete(
+ legacy_voucher_propagation,
+ expected_unit_discount_amount,
+ expected_unit_discount_reason,
user_api_client,
checkout_with_voucher_percentage,
voucher_percentage,
@@ -1954,6 +1968,10 @@ def test_checkout_with_voucher_complete(
transaction_item_generator,
):
# given
+ channel = checkout_with_voucher_percentage.channel
+ channel.use_legacy_line_voucher_propagation_for_order = legacy_voucher_propagation
+ channel.save()
+
code = voucher_percentage.codes.first()
checkout = prepare_checkout_for_test(
checkout_with_voucher_percentage,
@@ -2025,11 +2043,100 @@ def test_checkout_with_voucher_complete(
)
order_line = order.lines.first()
+ assert order_line.unit_discount_amount == expected_unit_discount_amount
+ assert order_line.unit_discount_reason == expected_unit_discount_reason
+
+
+ (
+ "legacy_propagation",
+ "expected_unit_discount_amount",
+ ),
+ [
+ (True, Decimal("1.67")),
+ (
+ False,
+ Decimal(0),
+ ),
+ ],
+)
+def test_checkout_with_order_promotion_complete(
+ legacy_propagation,
+ expected_unit_discount_amount,
+ user_api_client,
+ checkout_with_item_and_order_discount,
+ address,
+ shipping_method,
+ transaction_events_generator,
+ transaction_item_generator,
+):
+ # given
+ checkout = checkout_with_item_and_order_discount
+
+ channel = checkout.channel
+ channel.use_legacy_line_voucher_propagation_for_order = legacy_propagation
+ channel.save()
+
+ checkout = prepare_checkout_for_test(
+ checkout,
+ address,
+ address,
+ shipping_method,
+ transaction_item_generator,
+ transaction_events_generator,
+ )
+
+ discount_amount = checkout.discount
+
+ 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=address
+ )
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "redirectUrl": "https://www.example.com",
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order_token = data["order"]["token"]
+ order_id = data["order"]["id"]
+ assert Order.objects.count() == 1
+ order = Order.objects.first()
+ assert str(order.id) == order_token
+ assert order_id == graphene.Node.to_global_id("Order", order.id)
+ assert order.metadata == checkout.metadata_storage.metadata
+ assert order.private_metadata == checkout.metadata_storage.private_metadata
+
+ assert order.total == total
+ assert order.subtotal.gross == get_subtotal(order.lines.all(), order.currency).gross
+ assert order.undiscounted_total == total + discount_amount
+
+ assert not Checkout.objects.filter(pk=checkout.pk).exists(), (
+ "Checkout should have been deleted"
+ )
+
+ order_line = order.lines.first()
+ assert order_line.unit_discount_amount == expected_unit_discount_amount
+ assert order_line.unit_discount_reason is None
+
+ order_discount = order.discounts.filter(type=DiscountType.ORDER_PROMOTION).first()
+ assert order_discount
assert (
- order_line.unit_discount_amount
- == (discount_amount / order_line.quantity).amount
+ order_discount.amount_value
+ == (order.undiscounted_total - order.total).gross.amount
)
- assert order_line.unit_discount_reason
@pytest.mark.integration
diff --git a/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py b/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
index 555a4cd2ec..5b22768811 100644
--- a/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
+++ b/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
@@ -87,8 +87,14 @@ def prepare_voucher(
return voucher_discount_value, voucher_code
+ ("legacy_voucher_propagation", "entire_voucher_discount_reason"),
+ [(True, "Entire order voucher code: VOUCHER001"), (False, "")],
+)
@pytest.mark.e2e
def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_CORE_0114(
+ legacy_voucher_propagation,
+ entire_voucher_discount_reason,
e2e_not_logged_api_client,
e2e_staff_api_client,
shop_permissions,
@@ -103,7 +109,12 @@ def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_
]
assign_permissions(e2e_staff_api_client, permissions)
- shop_data = prepare_default_shop(e2e_staff_api_client)
+ shop_data = prepare_default_shop(
+ e2e_staff_api_client,
+ channel_order_settings={
+ "useLegacyLineVoucherPropagation": legacy_voucher_propagation
+ },
+ )
channel_id = shop_data["channel"]["id"]
channel_slug = shop_data["channel"]["slug"]
warehouse_id = shop_data["warehouse"]["id"]
@@ -459,9 +470,9 @@ def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_
assert order_data["discounts"][0]["value"] == calculated_discount
assert order_line1["quantity"] == product2_new_quantity
- assert order_line1["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+ assert entire_voucher_discount_reason in order_line1["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line1["unitDiscountReason"]
+
assert (
order_line1["undiscountedUnitPrice"]["gross"]["amount"]
== product2_variant_price
@@ -470,13 +481,20 @@ def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_
assert order_line1["unitPrice"]["gross"]["amount"] == round(
calculated_line1_total / product2_new_quantity, 2
)
- assert order_line1["unitDiscount"]["amount"] == round(
- line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+
+ unit_discount1 = line1_discount
+ unit_discount1 += (
+ calculated_line1_discount / product2_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line1["unitDiscount"]["amount"] == round(unit_discount1, 2)
+
assert order_line2["quantity"] == product3_new_quantity
- assert order_line2["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+
+ assert entire_voucher_discount_reason in order_line2["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line2["unitDiscountReason"]
+
assert (
order_line2["undiscountedUnitPrice"]["gross"]["amount"]
== product3_variant_price
@@ -485,14 +503,21 @@ def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_
assert order_line2["unitPrice"]["gross"]["amount"] == round(
calculated_line2_total / product3_new_quantity, 2
)
- assert order_line2["unitDiscount"]["amount"] == round(
- line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ unit_discount2 = line2_discount
+ unit_discount2 += (
+ calculated_line2_discount / product3_new_quantity
+ if legacy_voucher_propagation
+ else 0
+ )
+ assert order_line2["unitPrice"]["gross"]["amount"] == round(
+ calculated_line2_total / product3_new_quantity, 2
)
assert order_line3["quantity"] == product4_quantity
- assert order_line3["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code}"
- )
+
+ line_reason3 = order_line3["unitDiscountReason"] or ""
+ assert entire_voucher_discount_reason in line_reason3
+
assert (
order_line3["undiscountedUnitPrice"]["gross"]["amount"]
== product4_variant_price
@@ -501,11 +526,19 @@ def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_
assert order_line3["unitPrice"]["gross"]["amount"] == round(
calculated_line3_total / product4_quantity, 2
)
- assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+ unit_discount3 = 0
+ unit_discount3 += calculated_line3_discount if legacy_voucher_propagation else 0
+ assert order_line3["unitDiscount"]["amount"] == unit_discount3
+ ("legacy_voucher_propagation", "entire_voucher_discount_reason"),
+ [(True, "Entire order voucher code: VOUCHER002"), (False, "")],
+)
@pytest.mark.e2e
def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
+ legacy_voucher_propagation,
+ entire_voucher_discount_reason,
e2e_not_logged_api_client,
e2e_staff_api_client,
shop_permissions,
@@ -520,7 +553,12 @@ def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
]
assign_permissions(e2e_staff_api_client, permissions)
- shop_data = prepare_default_shop(e2e_staff_api_client)
+ shop_data = prepare_default_shop(
+ e2e_staff_api_client,
+ channel_order_settings={
+ "useLegacyLineVoucherPropagation": legacy_voucher_propagation
+ },
+ )
channel_id = shop_data["channel"]["id"]
channel_slug = shop_data["channel"]["slug"]
warehouse_id = shop_data["warehouse"]["id"]
@@ -878,9 +916,9 @@ def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
assert order_data["discounts"][0]["value"] == calculated_discount
assert order_line1["quantity"] == product2_new_quantity
- assert order_line1["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+ assert entire_voucher_discount_reason in order_line1["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line1["unitDiscountReason"]
+
assert (
order_line1["undiscountedUnitPrice"]["gross"]["amount"]
== product2_variant_price
@@ -889,13 +927,20 @@ def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
assert order_line1["unitPrice"]["gross"]["amount"] == round(
calculated_line1_total / product2_new_quantity, 2
)
- assert order_line1["unitDiscount"]["amount"] == round(
- line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+
+ unit_discount1 = line1_discount
+ unit_discount1 += (
+ calculated_line1_discount / product2_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line1["unitDiscount"]["amount"] == round(unit_discount1, 2)
+
assert order_line2["quantity"] == product3_new_quantity
- assert order_line2["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+
+ assert entire_voucher_discount_reason in order_line2["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line2["unitDiscountReason"]
+
assert (
order_line2["undiscountedUnitPrice"]["gross"]["amount"]
== product3_variant_price
@@ -904,14 +949,19 @@ def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
assert order_line2["unitPrice"]["gross"]["amount"] == round(
calculated_line2_total / product3_new_quantity, 2
)
- assert order_line2["unitDiscount"]["amount"] == round(
- line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ unit_discount2 = line2_discount
+ unit_discount2 += (
+ calculated_line2_discount / product3_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line2["unitDiscount"]["amount"] == round(unit_discount2, 2)
assert order_line3["quantity"] == product4_quantity
- assert order_line3["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code}"
- )
+
+ line_reason3 = order_line3["unitDiscountReason"] or ""
+ assert entire_voucher_discount_reason in line_reason3
+
assert (
order_line3["undiscountedUnitPrice"]["gross"]["amount"]
== product4_variant_price
@@ -920,11 +970,19 @@ def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
assert order_line3["unitPrice"]["gross"]["amount"] == round(
calculated_line3_total / product4_quantity, 2
)
- assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+ unit_discount3 = 0
+ unit_discount3 += calculated_line3_discount if legacy_voucher_propagation else 0
+ assert order_line3["unitDiscount"]["amount"] == unit_discount3
+ ("legacy_voucher_propagation", "entire_voucher_discount_reason"),
+ [(True, "Entire order voucher code: VOUCHER003"), (False, "")],
+)
@pytest.mark.e2e
def test_checkout_calculate_discount_for_fixed_sale_and_percentage_voucher_CORE_0114(
+ legacy_voucher_propagation,
+ entire_voucher_discount_reason,
e2e_not_logged_api_client,
e2e_staff_api_client,
shop_permissions,
@@ -939,7 +997,12 @@ def test_checkout_calculate_discount_for_fixed_sale_and_percentage_voucher_CORE_
]
assign_permissions(e2e_staff_api_client, permissions)
- shop_data = prepare_default_shop(e2e_staff_api_client)
+ shop_data = prepare_default_shop(
+ e2e_staff_api_client,
+ channel_order_settings={
+ "useLegacyLineVoucherPropagation": legacy_voucher_propagation
+ },
+ )
channel_id = shop_data["channel"]["id"]
channel_slug = shop_data["channel"]["slug"]
warehouse_id = shop_data["warehouse"]["id"]
@@ -1296,9 +1359,9 @@ def test_checkout_calculate_discount_for_fixed_sale_and_percentage_voucher_CORE_
assert order_data["discounts"][0]["value"] == calculated_discount
assert order_line1["quantity"] == product2_new_quantity
- assert order_line1["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+ assert entire_voucher_discount_reason in order_line1["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line1["unitDiscountReason"]
+
assert (
order_line1["undiscountedUnitPrice"]["gross"]["amount"]
== product2_variant_price
@@ -1307,29 +1370,39 @@ def test_checkout_calculate_discount_for_fixed_sale_and_percentage_voucher_CORE_
assert order_line1["unitPrice"]["gross"]["amount"] == round(
calculated_line1_total / product2_new_quantity, 2
)
- assert order_line1["unitDiscount"]["amount"] == round(
- line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+ unit_discount1 = line1_discount
+ unit_discount1 += (
+ calculated_line1_discount / product2_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line1["unitDiscount"]["amount"] == round(unit_discount1, 2)
assert order_line2["quantity"] == product3_new_quantity
- assert order_line2["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+
+ assert entire_voucher_discount_reason in order_line2["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line2["unitDiscountReason"]
+
assert (
order_line2["undiscountedUnitPrice"]["gross"]["amount"]
== product3_variant_price
)
assert order_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
assert order_line2["unitPrice"]["gross"]["amount"] == round(
calculated_line2_total / product3_new_quantity, 2
)
- assert order_line2["unitDiscount"]["amount"] == round(
- line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ unit_discount2 = line2_discount
+ unit_discount2 += (
+ calculated_line2_discount / product3_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line2["unitDiscount"]["amount"] == round(unit_discount2, 2)
assert order_line3["quantity"] == product4_quantity
- assert order_line3["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code}"
- )
+ line_reason3 = order_line3["unitDiscountReason"] or ""
+ assert entire_voucher_discount_reason in line_reason3
+
assert (
order_line3["undiscountedUnitPrice"]["gross"]["amount"]
== product4_variant_price
@@ -1338,11 +1411,19 @@ def test_checkout_calculate_discount_for_fixed_sale_and_percentage_voucher_CORE_
assert order_line3["unitPrice"]["gross"]["amount"] == round(
calculated_line3_total / product4_quantity, 2
)
- assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+ unit_discount3 = 0
+ unit_discount3 += calculated_line3_discount if legacy_voucher_propagation else 0
+ assert order_line3["unitDiscount"]["amount"] == unit_discount3
+ ("legacy_voucher_propagation", "entire_voucher_discount_reason"),
+ [(True, "Entire order voucher code: VOUCHER004"), (False, "")],
+)
@pytest.mark.e2e
def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_0114(
+ legacy_voucher_propagation,
+ entire_voucher_discount_reason,
e2e_not_logged_api_client,
e2e_staff_api_client,
shop_permissions,
@@ -1357,7 +1438,12 @@ def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_
]
assign_permissions(e2e_staff_api_client, permissions)
- shop_data = prepare_default_shop(e2e_staff_api_client)
+ shop_data = prepare_default_shop(
+ e2e_staff_api_client,
+ channel_order_settings={
+ "useLegacyLineVoucherPropagation": legacy_voucher_propagation
+ },
+ )
channel_id = shop_data["channel"]["id"]
channel_slug = shop_data["channel"]["slug"]
warehouse_id = shop_data["warehouse"]["id"]
@@ -1714,9 +1800,10 @@ def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_
assert order_data["discounts"][0]["value"] == calculated_discount
assert order_line1["quantity"] == product2_new_quantity
- assert order_line1["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+
+ assert entire_voucher_discount_reason in order_line1["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line1["unitDiscountReason"]
+
assert (
order_line1["undiscountedUnitPrice"]["gross"]["amount"]
== product2_variant_price
@@ -1725,13 +1812,18 @@ def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_
assert order_line1["unitPrice"]["gross"]["amount"] == round(
calculated_line1_total / product2_new_quantity, 2
)
- assert order_line1["unitDiscount"]["amount"] == round(
- line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+ unit_discount1 = line1_discount
+ unit_discount1 += (
+ calculated_line1_discount / product2_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line1["unitDiscount"]["amount"] == round(unit_discount1, 2)
assert order_line2["quantity"] == product3_new_quantity
- assert order_line2["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
- )
+
+ assert entire_voucher_discount_reason in order_line2["unitDiscountReason"]
+ assert f"Sale: {sale_id}" in order_line2["unitDiscountReason"]
+
assert (
order_line2["undiscountedUnitPrice"]["gross"]["amount"]
== product3_variant_price
@@ -1740,14 +1832,20 @@ def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_
assert order_line2["unitPrice"]["gross"]["amount"] == round(
calculated_line2_total / product3_new_quantity, 2
)
- assert order_line2["unitDiscount"]["amount"] == round(
- line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+
+ unit_discount2 = line2_discount
+ unit_discount2 += (
+ calculated_line2_discount / product3_new_quantity
+ if legacy_voucher_propagation
+ else 0
)
+ assert order_line2["unitDiscount"]["amount"] == round(unit_discount2, 2)
assert order_line3["quantity"] == product4_quantity
- assert order_line3["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code}"
- )
+
+ line_reason3 = order_line3["unitDiscountReason"] or ""
+ assert entire_voucher_discount_reason in line_reason3
+
assert (
order_line3["undiscountedUnitPrice"]["gross"]["amount"]
== product4_variant_price
@@ -1756,4 +1854,6 @@ def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_
assert order_line3["unitPrice"]["gross"]["amount"] == round(
calculated_line3_total / product4_quantity, 2
)
- assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+ unit_discount3 = 0
+ unit_discount3 += calculated_line3_discount if legacy_voucher_propagation else 0
+ assert order_line3["unitDiscount"]["amount"] == unit_discount3
diff --git a/saleor/tests/e2e/checkout/discounts/test_checkout_with_promotion_and_voucher.py b/saleor/tests/e2e/checkout/discounts/test_checkout_with_promotion_and_voucher.py
index b2c49cb49a..b4d938088e 100644
--- a/saleor/tests/e2e/checkout/discounts/test_checkout_with_promotion_and_voucher.py
+++ b/saleor/tests/e2e/checkout/discounts/test_checkout_with_promotion_and_voucher.py
@@ -104,6 +104,171 @@ def prepare_voucher(
return voucher_discount_value, voucher_code
+ (
+ "variant_price",
+ "promotion_value",
+ "promotion_value_type",
+ "expected_promotion_discount",
+ "voucher_discount_value",
+ "voucher_discount_type",
+ "expected_voucher_discount",
+ "expected_unit_price",
+ ),
+ [
+ ("19.99", 13, "PERCENTAGE", 2.60, 13, "PERCENTAGE", 2.26, 15.13),
+ ("30", 10, "FIXED", 10, 5, "FIXED", 5, 17.5),
+ ("56.50", 19.99, "FIXED", 19.99, 33, "PERCENTAGE", 12.05, 24.46),
+ ("77.77", 17.5, "PERCENTAGE", 13.61, 25, "FIXED", 25, 51.66),
+ ],
+)
+def test_checkout_with_promotion_and_voucher_legacy_propagation_CORE_2107(
+ e2e_not_logged_api_client,
+ e2e_staff_api_client,
+ shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ variant_price,
+ promotion_value_type,
+ promotion_value,
+ expected_promotion_discount,
+ voucher_discount_value,
+ voucher_discount_type,
+ expected_voucher_discount,
+ expected_unit_price,
+):
+ # Before
+ permissions = [
+ *shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(
+ e2e_staff_api_client,
+ channel_order_settings={"useLegacyLineVoucherPropagation": True},
+ )
+ channel_id = shop_data["channel"]["id"]
+ channel_slug = shop_data["channel"]["slug"]
+ warehouse_id = shop_data["warehouse"]["id"]
+ shipping_method_id = shop_data["shipping_method"]["id"]
+
+ (
+ product_id,
+ product_variant_id,
+ product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ variant_price,
+ )
+
+ promotion_id, promotion_value = prepare_promotion(
+ e2e_staff_api_client,
+ product_id,
+ channel_id,
+ promotion_value,
+ promotion_value_type,
+ )
+
+ voucher_discount_value, voucher_code = prepare_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ product_id,
+ voucher_discount_type,
+ voucher_discount_value,
+ )
+
+ # prices are updated in the background, we need to force it to retrieve the correct
+ # ones
+ recalculate_discounted_price_for_products_task()
+
+ # Step 1 - Create checkout for product with promotion
+ lines = [
+ {"variantId": product_variant_id, "quantity": 1},
+ ]
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ )
+ checkout_id = checkout_data["id"]
+ checkout_lines = checkout_data["lines"][0]
+
+ assert checkout_data["isShippingRequired"] is True
+ unit_price_with_promotion = round(
+ float(product_variant_price - expected_promotion_discount), 2
+ )
+ assert checkout_lines["unitPrice"]["gross"]["amount"] == unit_price_with_promotion
+ assert checkout_lines["undiscountedUnitPrice"]["amount"] == product_variant_price
+
+ # Step 2 Add voucher code to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_not_logged_api_client, checkout_id, voucher_code
+ )
+ assert checkout_data["discount"]["amount"] == expected_voucher_discount
+ assert checkout_data["lines"][0]["unitPrice"]["gross"]["amount"] == round(
+ float(unit_price_with_promotion - expected_voucher_discount), 2
+ )
+
+ # Step 3 Add variant to the checkout
+ lines_add = [
+ {"variantId": product_variant_id, "quantity": 1},
+ ]
+ checkout_data = checkout_lines_add(
+ e2e_staff_api_client,
+ checkout_id,
+ lines_add,
+ )
+ checkout_lines = checkout_data["lines"][0]
+ assert checkout_lines["quantity"] == 2
+ assert checkout_lines["unitPrice"]["gross"]["amount"] == expected_unit_price
+ subtotal_amount = expected_unit_price * 2
+ assert checkout_lines["totalPrice"]["gross"]["amount"] == subtotal_amount
+
+ # Step 4 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ total_gross_amount = round(subtotal_amount + shipping_price, 2)
+ assert checkout_data["totalPrice"]["gross"]["amount"] == total_gross_amount
+
+ # Step 5 - Create payment for checkout.
+ checkout_dummy_payment_create(
+ e2e_not_logged_api_client, checkout_id, total_gross_amount
+ )
+
+ # Step 6 - Complete checkout.
+ order_data = checkout_complete(
+ e2e_not_logged_api_client,
+ checkout_id,
+ )
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["discounts"][0]["type"] == "VOUCHER"
+ assert order_data["voucher"]["code"] == voucher_code
+
+ order_line = order_data["lines"][0]
+ assert order_line["unitDiscountType"] == "FIXED"
+ assert order_line["unitPrice"]["gross"]["amount"] == expected_unit_price
+ assert order_data["total"]["gross"]["amount"] == total_gross_amount
+ assert order_data["subtotal"]["gross"]["amount"] == subtotal_amount
+ assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
+ product_variant_price
+ )
+ assert f"Promotion: {promotion_id}" in order_line["unitDiscountReason"]
+ assert (
+ f"Entire order voucher code: {voucher_code}" in order_line["unitDiscountReason"]
+ )
+
+
@pytest.mark.e2e
@pytest.mark.parametrize(
(
@@ -146,7 +311,10 @@ def test_checkout_with_promotion_and_voucher_CORE_2107(
]
assign_permissions(e2e_staff_api_client, permissions)
- shop_data = prepare_default_shop(e2e_staff_api_client)
+ shop_data = prepare_default_shop(
+ e2e_staff_api_client,
+ channel_order_settings={"useLegacyLineVoucherPropagation": False},
+ )
channel_id = shop_data["channel"]["id"]
channel_slug = shop_data["channel"]["slug"]
warehouse_id = shop_data["warehouse"]["id"]
@@ -260,6 +428,8 @@ def test_checkout_with_promotion_and_voucher_CORE_2107(
assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
product_variant_price
)
- assert order_line["unitDiscountReason"] == (
- f"Entire order voucher code: {voucher_code} & Promotion: {promotion_id}"
+ assert f"Promotion: {promotion_id}" in order_line["unitDiscountReason"]
+ assert (
+ f"Entire order voucher code: {voucher_code}"
+ not in order_line["unitDiscountReason"]
)
diff --git a/saleor/tests/e2e/shop/utils/preparing_shop.py b/saleor/tests/e2e/shop/utils/preparing_shop.py
index 99d8ec3859..0fbc367520 100644
--- a/saleor/tests/e2e/shop/utils/preparing_shop.py
+++ b/saleor/tests/e2e/shop/utils/preparing_shop.py
@@ -137,14 +137,13 @@ def prepare_shop(
return created_channels, tax_config
-def prepare_default_shop(
- e2e_staff_api_client,
-):
+def prepare_default_shop(e2e_staff_api_client, channel_order_settings=None):
created_warehouse = create_warehouse(e2e_staff_api_client)
created_channel = create_channel(
e2e_staff_api_client,
warehouse_ids=[created_warehouse["id"]],
+ order_settings=channel_order_settings,
)
created_shipping_zone = create_shipping_zone(