Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Set unit_discount_reason on line when the entire order voucher is applied (#16084)
Iga Karbowiak186077dd3466
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex c619508ea0..14adaeec96 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -307,9 +307,12 @@ def _create_line_for_order(prices_entered_with_tax,)- voucher_code = None+ line_voucher_code = Noneif checkout_line_info.voucher:- voucher_code = checkout_line_info.voucher.code+ line_voucher_code = checkout_line_info.voucher.code+ order_voucher_code = None+ if checkout_info.voucher:+ order_voucher_code = checkout_info.voucher.codediscount_price = undiscounted_unit_price - unit_priceif prices_entered_with_tax:@@ -317,9 +320,9 @@ def _create_line_for_order(else:discount_amount = discount_price.net- unit_discount_reason = None- if voucher_code:- unit_discount_reason = f"Voucher code: {voucher_code}"+ unit_discount_reason = _get_unit_discount_reason(+ line_voucher_code, order_voucher_code+ )tax_class = Noneif product.tax_class_id:@@ -344,7 +347,7 @@ def _create_line_for_order(undiscounted_total_price=undiscounted_total_price, # money field not supported by mypy_django_plugin # noqa: E501total_price=total_line_price,tax_rate=tax_rate,- voucher_code=voucher_code,+ voucher_code=line_voucher_code,unit_discount=discount_amount, # money field not supported by mypy_django_plugin # noqa: E501unit_discount_reason=unit_discount_reason,unit_discount_value=discount_amount.amount, # we store value as fixed discount@@ -385,6 +388,26 @@ def _create_line_for_order(return line_info+def _get_unit_discount_reason(+ line_voucher_code: Optional[str],+ order_voucher_code: Optional[str],+) -> Optional[str]:+ unit_discount_reason = None+ if line_voucher_code:+ if unit_discount_reason:+ unit_discount_reason += f" & Voucher code: {line_voucher_code}"+ else:+ unit_discount_reason = f"Voucher code: {line_voucher_code}"+ elif order_voucher_code:+ if unit_discount_reason:+ msg = f" & Entire order voucher code: {order_voucher_code}"+ unit_discount_reason += msg+ else:+ unit_discount_reason = f"Entire order voucher code: {order_voucher_code}"++ return unit_discount_reason++def _create_order_line_discounts(checkout_line_info: "CheckoutLineInfo", order_line: "OrderLine") -> list["OrderLineDiscount"]: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.pyindex a67f5f79cb..8819c1ebda 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@@ -912,6 +912,11 @@ def test_checkout_with_voucher_complete(order_payment = order.payments.first()assert order_payment == paymentassert payment.transactions.count() == 1+ assert (+ order_line.unit_discount_amount+ == (discount_amount / checkout_line_quantity).amount+ )+ assert order_line.unit_discount_reasoncode.refresh_from_db()assert code.used == voucher_used_count + 1@@ -1167,7 +1172,9 @@ def test_checkout_with_voucher_complete_product_on_promotion(assert order_line.sale_id == graphene.Node.to_global_id("Promotion", catalogue_promotion_without_rules.id)- assert order_line.unit_discount_reason == f"Promotion: {order_line.sale_id}"+ assert order_line.unit_discount_reason == (+ f"Entire order voucher code: {code.code} & Promotion: {order_line.sale_id}"+ )assert checkout_line_quantity == order_line.quantityassert checkout_line_variant == order_line.variantdiff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.pyindex c553436e27..9a9f752789 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@@ -1672,6 +1672,13 @@ def test_checkout_with_voucher_complete(pk=checkout.pk).exists(), "Checkout should have been deleted"+ order_line = order.lines.first()+ assert (+ order_line.unit_discount_amount+ == (discount_amount / order_line.quantity).amount+ )+ assert order_line.unit_discount_reason+@pytest.mark.integrationdef test_checkout_complete_with_voucher_apply_once_per_order(@@ -1956,7 +1963,9 @@ def test_checkout_with_voucher_complete_product_on_sale(assert order_line.sale_id == graphene.Node.to_global_id("Sale", catalogue_promotion_without_rules.old_sale_id)- assert order_line.unit_discount_reason == f"Sale: {order_line.sale_id}"+ assert order_line.unit_discount_reason == (+ f"Entire order voucher code: {code.code} & Sale: {order_line.sale_id}"+ )code.refresh_from_db()assert code.used == voucher_used_count + 1diff --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.pyindex a6120932e9..a291836b36 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@@ -231,7 +231,9 @@ def test_checkout_calculate_discount_for_sale_and_voucher_1014(order_line = order_data["lines"][0]assert order_line["unitDiscountType"] == "FIXED"assert order_line["unitPrice"]["gross"]["amount"] == expected_unit_price- assert order_line["unitDiscountReason"] == f"Sale: {sale_id}"+ assert order_line["unitDiscountReason"] == (+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"+ )assert order_data["total"]["gross"]["amount"] == total_gross_amountassert order_data["subtotal"]["gross"]["amount"] == subtotal_amountassert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(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.pyindex 7905c26d1e..d34d733de9 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@@ -262,4 +262,6 @@ def test_checkout_with_promotion_and_voucher_CORE_2107(assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(product_variant_price)- assert order_line["unitDiscountReason"] == f"Promotion: {promotion_id}"+ assert order_line["unitDiscountReason"] == (+ f"Entire order voucher code: {voucher_code} & Promotion: {promotion_id}"+ )