Skip to content

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

Fix missing currency in voucher discount objects (#17059)

Piotr Zabieglik9162bc8c2cca
diff --git a/saleor/discount/migrations/0084_set_missing_currency.py b/saleor/discount/migrations/0084_set_missing_currency.py
new file mode 100644
index 0000000000..dca0c45423
--- /dev/null
+++ b/saleor/discount/migrations/0084_set_missing_currency.py
@@ -0,0 +1,49 @@
+# Generated by Django 4.2.16 on 2025-01-14 14:28
+
+from django.db import migrations
+from django.db.models import OuterRef, Subquery
+
+# The batch of size 5000 takes ~0.1 second and consumes ~4MB memory at peak
+BATCH_SIZE = 5000
+
+
+def set_discount_currency(apps, _schema_editor):
+ OrderDiscount = apps.get_model("discount", "OrderDiscount")
+ qs = (
+ OrderDiscount.objects.filter(currency="")
+ .select_related("order")
+ .order_by("id")
+ .only("id", "currency", "order__currency")
+ )
+ for ids in queryset_in_batches(qs):
+ OrderDiscount.objects.filter(id__in=ids).update(
+ currency=Subquery(
+ OrderDiscount.objects.filter(pk=OuterRef("id")).values(
+ "order__currency"
+ )[:1]
+ )
+ )
+
+
+def queryset_in_batches(queryset):
+ start_pk = 0
+ while True:
+ qs = queryset.filter(pk__gt=start_pk)[:BATCH_SIZE]
+ pks = list(qs.values_list("pk", flat=True))
+ if not pks:
+ break
+ yield pks
+ start_pk = pks[-1]
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0083_auto_20240510_0838"),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ set_discount_currency,
+ reverse_code=migrations.RunPython.noop,
+ )
+ ]
diff --git a/saleor/discount/utils/voucher.py b/saleor/discount/utils/voucher.py
index e64850ff40..49e457c964 100644
--- a/saleor/discount/utils/voucher.py
+++ b/saleor/discount/utils/voucher.py
@@ -361,6 +361,7 @@ def create_or_update_discount_object_from_order_level_voucher(
"value_type": voucher.discount_value_type,
"value": voucher_channel_listing.discount_value,
"amount_value": discount_amount.amount,
+ "currency": order.currency,
"reason": discount_reason,
"name": discount_name,
"type": DiscountType.VOUCHER,
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_update.py b/saleor/graphql/order/tests/mutations/test_draft_order_update.py
index e233b43ea5..4888e99996 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_update.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_update.py
@@ -98,6 +98,7 @@ DRAFT_ORDER_UPDATE_MUTATION = """
discounts {
amount {
amount
+ currency
}
valueType
type
@@ -203,6 +204,7 @@ def test_draft_order_update_with_voucher_entire_order(
):
# given
order = draft_order
+ currency = order.currency
assert not order.voucher
assert not order.voucher_code
assert not order.customer_note
@@ -248,6 +250,13 @@ def test_draft_order_update_with_voucher_entire_order(
== order_total - voucher_listing.discount_value
)
+ assert len(data["order"]["discounts"]) == 1
+ assert (
+ data["order"]["discounts"][0]["amount"]["amount"]
+ == voucher_listing.discount_value
+ )
+ assert data["order"]["discounts"][0]["amount"]["currency"] == currency
+
assert not data["errors"]
order.refresh_from_db()
assert order.billing_address.metadata == stored_metadata
diff --git a/saleor/order/tests/test_fetch_order_prices.py b/saleor/order/tests/test_fetch_order_prices.py
index 7540006d80..4e797ad44e 100644
--- a/saleor/order/tests/test_fetch_order_prices.py
+++ b/saleor/order/tests/test_fetch_order_prices.py
@@ -2391,6 +2391,7 @@ def test_fetch_order_prices_voucher_entire_order_fixed(
assert discount.value_type == voucher.discount_value_type
assert discount.value == discount_amount
assert discount.amount_value == discount_amount
+ assert discount.currency == currency
assert discount.reason == f"Voucher code: {code}"
assert discount.name == voucher.name
assert discount.type == DiscountType.VOUCHER
@@ -2510,6 +2511,7 @@ def test_fetch_order_prices_voucher_entire_order_multiple_lines(
assert discount.value_type == voucher.discount_value_type
assert discount.value == discount_amount
assert discount.amount_value == discount_amount
+ assert discount.currency == currency
assert discount.reason == f"Voucher code: {code}"
assert discount.name == voucher.name
assert discount.type == DiscountType.VOUCHER
@@ -2603,6 +2605,7 @@ def test_fetch_order_prices_voucher_entire_order_percentage(
assert discount.value_type == voucher.discount_value_type
assert discount.value == discount_value
assert discount.amount_value == Decimal(subtotal.amount / 2)
+ assert discount.currency == currency
assert discount.reason == f"Voucher code: {code}"
assert discount.name == voucher.name
assert discount.type == DiscountType.VOUCHER