Skip to content

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

Fix checkout.discount amount when override is set (#17979)

Marcin Gębala4a044c694fb6
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index df7a349226..7b830e972d 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -73,8 +73,15 @@ class CheckoutLineInfo(LineInfo):
if listing is not present, calculate current unit price based on
`undiscounted_unit_price` and catalogue promotion discounts.
"""
+
+ # if price_override is set, it takes precedence over any other price for
+ # further calculations
+ if self.line.price_override is not None:
+ return Money(self.line.price_override, self.line.currency)
+
if self.channel_listing and self.channel_listing.discounted_price is not None:
return self.channel_listing.discounted_price
+
catalogue_discounts = self.get_catalogue_discounts()
total_price = self.undiscounted_unit_price * self.line.quantity
for discount in catalogue_discounts:
diff --git a/saleor/checkout/tests/test_fetch.py b/saleor/checkout/tests/test_fetch.py
index 6fea8ad0fe..85aae89fbb 100644
--- a/saleor/checkout/tests/test_fetch.py
+++ b/saleor/checkout/tests/test_fetch.py
@@ -1,4 +1,7 @@
+from decimal import Decimal
+
import pytest
+from prices import Money
from ...product.models import ProductChannelListing, ProductVariantChannelListing
from ..fetch import CheckoutLineInfo, fetch_checkout_lines
@@ -217,6 +220,45 @@ def test_checkout_line_info_variant_discounted_price_when_listing_without_price(
)
+def test_checkout_line_info_variant_discounted_price_with_price_override(
+ checkout_with_item_on_promotion,
+):
+ # given
+ checkout_line = checkout_with_item_on_promotion.lines.first()
+ channel = checkout_with_item_on_promotion.channel
+ variant = checkout_line.variant
+ variant_channel_listing = variant.channel_listings.get(channel_id=channel.id)
+ product = variant.product
+ product_type = product.product_type
+ discounts = checkout_line.discounts.all()
+ checkout_line.price_override = Decimal(5)
+ checkout_line.save(update_fields=["price_override"])
+
+ expected_discounted_variant_price = checkout_line.price_override
+ assert variant_channel_listing.discounted_price != variant_channel_listing.price
+
+ # when
+ checkout_line_info = CheckoutLineInfo(
+ line=checkout_line,
+ variant=variant,
+ channel_listing=variant_channel_listing,
+ product=product,
+ product_type=product_type,
+ collections=[],
+ tax_class=product.tax_class or product_type.tax_class,
+ discounts=discounts,
+ rules_info=[],
+ channel=channel,
+ voucher=None,
+ voucher_code=None,
+ )
+
+ # then
+ assert checkout_line_info.variant_discounted_price == Money(
+ expected_discounted_variant_price, checkout_line.currency
+ )
+
+
def test_fetch_checkout_lines_info(checkout_with_item_on_promotion):
# given
lines = list(checkout_with_item_on_promotion.lines.all())
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
index 6f52b43737..50fa85efdf 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
@@ -18,7 +18,7 @@ from .....checkout.utils import (
assign_external_shipping_to_checkout,
)
from .....core.models import EventDelivery
-from .....discount import VoucherType
+from .....discount import DiscountValueType, VoucherType
from .....plugins.manager import get_plugins_manager
from .....product.models import (
Collection,
@@ -1457,3 +1457,42 @@ def test_checkout_add_voucher_triggers_webhooks(
tax_delivery = tax_delivery_call.args[0]
assert tax_delivery.webhook_id == tax_webhook.id
+
+
+def test_add_promo_code_with_price_override_set(
+ user_api_client, checkout_with_item, voucher
+):
+ # given
+ checkout_line = checkout_with_item.lines.first()
+ price_override = Decimal("5.00")
+ checkout_line.price_override = price_override
+ checkout_line.save(update_fields=["price_override"])
+ product = checkout_line.variant.product
+
+ voucher.type = VoucherType.SPECIFIC_PRODUCT
+ voucher.discount_value_type = DiscountValueType.PERCENTAGE
+ cl = voucher.channel_listings.get(channel=checkout_with_item.channel)
+ cl.discount_value = Decimal("10.00") # 10% discount
+ cl.save(update_fields=["discount_value"])
+ voucher.save(update_fields=["type", "discount_value_type"])
+ voucher.products.add(product)
+
+ # total expected discount is 10% of price override
+ expected_discount = price_override * Decimal("0.10") * checkout_line.quantity
+ expected_subtotal = price_override * checkout_line.quantity - expected_discount
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_item),
+ "promoCode": voucher.code,
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_ADD_PROMO_CODE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutAddPromoCode"]
+
+ # then
+ assert not data["errors"]
+ assert data["checkout"]["voucherCode"] == voucher.code
+ assert data["checkout"]["discount"]["amount"] == expected_discount
+ assert data["checkout"]["subtotalPrice"]["gross"]["amount"] == expected_subtotal