Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Preview the computed amount for percent manual discounts

Damian Legawiecdc6bcb907f94
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index fd0500eb62..54bee470a6 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -1361,6 +1361,7 @@
"kind_field": "الفئة",
"kind_promotion": "عرض ترويجي",
"label_field": "التسمية",
+ "percent_preview": "الخصم المقدر: {{amount}}",
"target_field": "ينطبق على",
"target_order": "الطلب بأكمله",
"taxes": "الضرائب",
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index 55acd6aa64..03c5a0353c 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -1361,6 +1361,7 @@
"kind_field": "Art",
"kind_promotion": "Aktion",
"label_field": "Bezeichnung",
+ "percent_preview": "Voraussichtlicher Rabatt: {{amount}}",
"target_field": "Gilt für",
"target_order": "Gesamte Bestellung",
"taxes": "Steuern",
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index ded1c6ef88..6a76ca6c04 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -1361,6 +1361,7 @@
"kind_field": "Kind",
"kind_promotion": "Promotion",
"label_field": "Label",
+ "percent_preview": "Estimated discount: {{amount}}",
"target_field": "Applies to",
"target_order": "Whole order",
"taxes": "Taxes",
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index 447d1b2635..f248662c01 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -1361,6 +1361,7 @@
"kind_field": "Nature",
"kind_promotion": "Promotion",
"label_field": "Libellé",
+ "percent_preview": "Remise estimée : {{amount}}",
"target_field": "S'applique à",
"target_order": "Commande entière",
"taxes": "Taxes",
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 4da763bfff..c00b075c43 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -1367,6 +1367,7 @@
"kind_field": "Rodzaj",
"kind_promotion": "Promocja",
"label_field": "Etykieta",
+ "percent_preview": "Szacowany rabat: {{amount}}",
"target_field": "Dotyczy",
"target_order": "Całe zamówienie",
"taxes": "Podatki",
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 7ab0ba1bdf..c38c4cc05c 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -1361,6 +1361,7 @@
"kind_field": "种类",
"kind_promotion": "促销",
"label_field": "名称",
+ "percent_preview": "预计折扣:{{amount}}",
"target_field": "适用于",
"target_order": "整个订单",
"taxes": "税费",
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx
index 283951b14b..8cbf23c1fa 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx
@@ -1492,10 +1492,11 @@ function AddDiscountDialog({
open: boolean
onOpenChange: (open: boolean) => void
}) {
- const { t } = useTranslation()
+ const { t, i18n } = useTranslation()
const { orderId } = Route.useParams()
const [target, setTarget] = useState('order')
const [valueType, setValueType] = useState('flat')
+ const [value, setValue] = useState('')
const mutation = useOrderAdjustmentLinesMutation(
orderId,
@@ -1522,14 +1523,38 @@ function AddDiscountDialog({
mutation.mutate(
{
label: fd.get('label') as string,
- value: fd.get('value') as string,
+ value,
value_type: valueType as 'flat' | 'percent',
...(target !== 'order' ? { line_item_id: target } : {}),
},
- { onSuccess: () => onOpenChange(false) },
+ {
+ onSuccess: () => {
+ setValue('')
+ onOpenChange(false)
+ },
+ },
)
}
+ // Mirrors Orders::AddManualDiscount: percent applies to the already-
+ // discounted line amounts, clamped so no line goes below zero.
+ const numericValue = Number.parseFloat(value)
+ let previewAmount: number | null = null
+ if (valueType === 'percent' && Number.isFinite(numericValue) && numericValue > 0) {
+ const items = order.items ?? []
+ const base =
+ target === 'order'
+ ? items.reduce(
+ (sum, item) => sum + Math.max(Number.parseFloat(item.discounted_amount), 0),
+ 0,
+ )
+ : Math.max(
+ Number.parseFloat(items.find((item) => item.id === target)?.discounted_amount ?? '0'),
+ 0,
+ )
+ if (base > 0) previewAmount = Math.min((base * numericValue) / 100, base)
+ }
+
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
@@ -1559,6 +1584,8 @@ function AddDiscountDialog({
type="number"
step="0.01"
min="0.01"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
required
/>
</Field>
@@ -1603,6 +1630,16 @@ function AddDiscountDialog({
</SelectContent>
</Select>
</Field>
+ {previewAmount !== null && (
+ <p className="text-sm text-muted-foreground">
+ {t('admin.orders.detail.adjustment_lines.percent_preview', {
+ amount: new Intl.NumberFormat(i18n.language, {
+ style: 'currency',
+ currency: order.currency,
+ }).format(previewAmount),
+ })}
+ </p>
+ )}
</FieldGroup>
</DialogBody>
<DialogFooter>