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.

Add a case-pack minimum to wholesale pricing (seed + admin editor) (#14341)

Damian Legawiecdc332372b918
diff --git a/.changeset/wholesale-volume-rule-editor.md b/.changeset/wholesale-volume-rule-editor.md
new file mode 100644
index 0000000000..1cf229c9ab
--- /dev/null
+++ b/.changeset/wholesale-volume-rule-editor.md
@@ -0,0 +1,6 @@
+---
+"@spree/dashboard": minor
+"@spree/dashboard-core": minor
+---
+
+Improve editing of quantity-bounded price rules. A blank upper-bound preference (`max_quantity`, `max_uses`, `maximum_amount`, …) now shows "Unlimited" instead of an empty required-looking field across every preferences form. The Volume price rule gains a dedicated editor that renders minimum quantity before maximum, so a case-pack minimum reads in a natural order.
diff --git a/packages/dashboard-core/src/components/preferences-form.tsx b/packages/dashboard-core/src/components/preferences-form.tsx
index 392867af80..49cabf711b 100644
--- a/packages/dashboard-core/src/components/preferences-form.tsx
+++ b/packages/dashboard-core/src/components/preferences-form.tsx
@@ -173,6 +173,11 @@ export function PreferenceField({
id={id}
type="number"
step={field.type === 'integer' ? 1 : 'any'}
+ // An empty upper-bound field means "no limit" — surface that
+ // rather than leaving it looking like a required blank.
+ placeholder={
+ isMaxKey(field.key) ? t('admin.components.preferences_form.unlimited') : undefined
+ }
value={value === null || value === undefined ? '' : String(value)}
onChange={(e) => {
const raw = e.target.value
@@ -247,6 +252,15 @@ function isCurrencyKey(key: string): boolean {
return key === 'currency' || key.endsWith('_currency')
}
+// Upper-bound preferences (`max_quantity`, `max_uses`, `usage_max`,
+// `maximum_amount`, …) read "Unlimited" when left blank. Matched on the key
+// name because the wire schema doesn't carry a nullable flag — safe since the
+// placeholder only shows for an empty value, and an empty max legitimately
+// means no ceiling.
+function isMaxKey(key: string): boolean {
+ return /(?:^|_)max(?:imum)?(?:_|$)/.test(key)
+}
+
interface TierRowState {
/** Stable across edits — used for React keys so input focus survives reorder. */
uid: string
diff --git a/packages/dashboard-core/src/locales/ar.json b/packages/dashboard-core/src/locales/ar.json
index 04a9efd27b..5df862a225 100644
--- a/packages/dashboard-core/src/locales/ar.json
+++ b/packages/dashboard-core/src/locales/ar.json
@@ -406,7 +406,8 @@
"empty": "لا توجد مستويات بعد. ينطبق السعر الأساسي حتى تضيف مستوى.",
"add_tier": "إضافة مستوى",
"remove_tier": "إزالة المستوى"
- }
+ },
+ "unlimited": "غير محدود"
},
"confirm_dialog": {
"title": "هل أنت متأكد؟"
diff --git a/packages/dashboard-core/src/locales/de.json b/packages/dashboard-core/src/locales/de.json
index ba84e15b11..d331e9ef86 100644
--- a/packages/dashboard-core/src/locales/de.json
+++ b/packages/dashboard-core/src/locales/de.json
@@ -406,7 +406,8 @@
"empty": "Noch keine Stufen. Der Basissatz gilt, bis Sie eine Stufe hinzufügen.",
"add_tier": "Stufe hinzufügen",
"remove_tier": "Stufe entfernen"
- }
+ },
+ "unlimited": "Unbegrenzt"
},
"confirm_dialog": {
"title": "Sind Sie sicher?"
diff --git a/packages/dashboard-core/src/locales/en.json b/packages/dashboard-core/src/locales/en.json
index fb037f95e0..3f927de3ef 100644
--- a/packages/dashboard-core/src/locales/en.json
+++ b/packages/dashboard-core/src/locales/en.json
@@ -406,7 +406,8 @@
"empty": "No tiers yet. The base rate applies until you add a tier.",
"add_tier": "Add tier",
"remove_tier": "Remove tier"
- }
+ },
+ "unlimited": "Unlimited"
},
"confirm_dialog": {
"title": "Are you sure?"
diff --git a/packages/dashboard-core/src/locales/fr.json b/packages/dashboard-core/src/locales/fr.json
index e71c51be04..1b0ef547ee 100644
--- a/packages/dashboard-core/src/locales/fr.json
+++ b/packages/dashboard-core/src/locales/fr.json
@@ -406,7 +406,8 @@
"empty": "Aucun palier pour le moment. Le taux de base s’applique jusqu’à l’ajout d’un palier.",
"add_tier": "Ajouter un palier",
"remove_tier": "Supprimer le palier"
- }
+ },
+ "unlimited": "Illimité"
},
"confirm_dialog": {
"title": "Êtes-vous sûr ?"
diff --git a/packages/dashboard-core/src/locales/pl.json b/packages/dashboard-core/src/locales/pl.json
index 75573506c4..a565db8318 100644
--- a/packages/dashboard-core/src/locales/pl.json
+++ b/packages/dashboard-core/src/locales/pl.json
@@ -406,7 +406,8 @@
"empty": "Brak progów. Do momentu dodania progu obowiązuje stawka podstawowa.",
"add_tier": "Dodaj próg",
"remove_tier": "Usuń próg"
- }
+ },
+ "unlimited": "Bez limitu"
},
"confirm_dialog": {
"title": "Czy na pewno?"
diff --git a/packages/dashboard-core/src/locales/zh-CN.json b/packages/dashboard-core/src/locales/zh-CN.json
index fcdca58a12..6548ec1242 100644
--- a/packages/dashboard-core/src/locales/zh-CN.json
+++ b/packages/dashboard-core/src/locales/zh-CN.json
@@ -406,7 +406,8 @@
"empty": "暂无层级。在添加层级之前,将应用基础费率。",
"add_tier": "添加层级",
"remove_tier": "移除层级"
- }
+ },
+ "unlimited": "无限制"
},
"confirm_dialog": {
"title": "确定吗?"
diff --git a/packages/dashboard/e2e/price-lists.spec.ts b/packages/dashboard/e2e/price-lists.spec.ts
index a157532ea7..7447c24460 100644
--- a/packages/dashboard/e2e/price-lists.spec.ts
+++ b/packages/dashboard/e2e/price-lists.spec.ts
@@ -142,7 +142,7 @@ test.describe('price lists', () => {
})
await page
.getByRole('dialog')
- .getByLabel(/min quantity/i)
+ .getByLabel(/minimum quantity/i)
.fill('10')
await saveEditor(page)
@@ -260,7 +260,7 @@ test.describe('price lists', () => {
await pickRule(page, /^volume rule\b/i)
await page
.getByRole('dialog')
- .getByLabel(/min quantity/i)
+ .getByLabel(/minimum quantity/i)
.fill('5')
await saveEditor(page)
await saveForm(page)
diff --git a/packages/dashboard/src/components/spree/price-list-editors/register.ts b/packages/dashboard/src/components/spree/price-list-editors/register.ts
index 0db63ede61..88125f7fbf 100644
--- a/packages/dashboard/src/components/spree/price-list-editors/register.ts
+++ b/packages/dashboard/src/components/spree/price-list-editors/register.ts
@@ -3,6 +3,7 @@ import { ChannelRuleEditor } from './rule-channel'
import { CustomerRuleEditor } from './rule-customer'
import { CustomerGroupRuleEditor } from './rule-customer-group'
import { MarketRuleEditor } from './rule-market'
+import { VolumeRuleEditor } from './rule-volume'
import { ruleFormSlot } from './types'
/**
@@ -11,10 +12,6 @@ import { ruleFormSlot } from './types'
* side effects from the price-list detail route — extensions can
* call `removeSlot(name, 'builtin')` to replace any of these with
* their own.
- *
- * VolumeRule's two integer preferences (`min_quantity`, `max_quantity`)
- * render fine via the generic `<PreferencesForm>` fallback, so it
- * doesn't need a custom editor here.
*/
// `user_rule` is the legacy wire shorthand for Spree::PriceRules::UserRule —
@@ -39,3 +36,10 @@ registerSlot(ruleFormSlot('channel_rule'), {
id: 'builtin',
component: ChannelRuleEditor,
})
+
+// Custom editor solely to fix field order: the preference registry serializes
+// max before min, and the generic form would render it that way.
+registerSlot(ruleFormSlot('volume_rule'), {
+ id: 'builtin',
+ component: VolumeRuleEditor,
+})
diff --git a/packages/dashboard/src/components/spree/price-list-editors/rule-volume.tsx b/packages/dashboard/src/components/spree/price-list-editors/rule-volume.tsx
new file mode 100644
index 0000000000..d08c0602aa
--- /dev/null
+++ b/packages/dashboard/src/components/spree/price-list-editors/rule-volume.tsx
@@ -0,0 +1,79 @@
+import { Field, FieldGroup, FieldLabel, Input } from '@spree/dashboard-ui'
+import { useState } from 'react'
+import { useTranslation } from 'react-i18next'
+import { EditorShell } from '../promotion-editors/editor-shell'
+import type { PriceRuleEditorContext } from './types'
+
+/**
+ * Min/max quantity editor for the Volume price rule. A custom editor is needed
+ * for one reason the generic `<PreferencesForm>` can't handle: `defined_preferences`
+ * serializes the two fields max-first (the preference registry isn't source-ordered),
+ * and "maximum" reading before "minimum" is backwards for the reader. This editor
+ * fixes the order (min first, max last) and — like the generic form's `isMaxKey`
+ * handling — shows a blank maximum as "Unlimited".
+ */
+function toIntOrUndefined(value: string): number | undefined {
+ const trimmed = value.trim()
+ if (trimmed === '') return undefined
+ const n = Number.parseInt(trimmed, 10)
+ return Number.isFinite(n) ? n : undefined
+}
+
+export function VolumeRuleEditor({ draft, onSave, onClose }: PriceRuleEditorContext) {
+ const { t } = useTranslation()
+
+ const [minQuantity, setMinQuantity] = useState<string>(() =>
+ draft.preferences.min_quantity != null ? String(draft.preferences.min_quantity) : '',
+ )
+ const [maxQuantity, setMaxQuantity] = useState<string>(() =>
+ draft.preferences.max_quantity != null ? String(draft.preferences.max_quantity) : '',
+ )
+
+ function handleSave() {
+ onSave({
+ ...draft,
+ preferences: {
+ ...draft.preferences,
+ min_quantity: toIntOrUndefined(minQuantity) ?? 1,
+ // Blank → unlimited upper bound (null clears the server-side preference).
+ max_quantity: toIntOrUndefined(maxQuantity) ?? null,
+ },
+ })
+ onClose()
+ }
+
+ return (
+ <EditorShell onSave={handleSave} onCancel={onClose} pending={false}>
+ <FieldGroup>
+ <Field>
+ <FieldLabel htmlFor="volume-rule-min">{t('admin.preferences.min_quantity')}</FieldLabel>
+ <Input
+ id="volume-rule-min"
+ type="number"
+ min={1}
+ value={minQuantity}
+ onChange={(e) => setMinQuantity(e.target.value)}
+ />
+ <p className="text-xs text-muted-foreground">
+ {t('admin.fields.price_rule.min_quantity.help')}
+ </p>
+ </Field>
+
+ <Field>
+ <FieldLabel htmlFor="volume-rule-max">{t('admin.preferences.max_quantity')}</FieldLabel>
+ <Input
+ id="volume-rule-max"
+ type="number"
+ min={1}
+ value={maxQuantity}
+ placeholder={t('admin.components.preferences_form.unlimited')}
+ onChange={(e) => setMaxQuantity(e.target.value)}
+ />
+ <p className="text-xs text-muted-foreground">
+ {t('admin.fields.price_rule.max_quantity.help')}
+ </p>
+ </Field>
+ </FieldGroup>
+ </EditorShell>
+ )
+}
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index 9dee12fb39..93e6c38f3b 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -374,6 +374,12 @@
"channels": {
"label": "قنوات البيع",
"help": "اتركه فارغًا لتطبيق هذه القاعدة على كل قناة بيع."
+ },
+ "min_quantity": {
+ "help": "يُطبَّق هذا السعر فقط عندما يطلب العميل هذا العدد على الأقل من صنف واحد."
+ },
+ "max_quantity": {
+ "help": "اتركه فارغًا لعدم وجود حد أقصى."
}
},
"stock_location": {
@@ -2500,7 +2506,9 @@
"base_amount": "Base Amount",
"base_percent": "Base Percent",
"preferred_amount": "كمية",
- "preferred_percent": "نسبة مئوية"
+ "preferred_percent": "نسبة مئوية",
+ "min_quantity": "الحد الأدنى للكمية",
+ "max_quantity": "الحد الأقصى للكمية"
},
"channels": {
"messages": {
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index c87d572fa6..ece38a7418 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -326,6 +326,12 @@
"channels": {
"label": "Vertriebskanäle",
"help": "Leer lassen, um diese Regel auf jeden Vertriebskanal anzuwenden."
+ },
+ "min_quantity": {
+ "help": "Dieser Preis gilt nur, wenn ein Kunde mindestens so viele Einheiten eines einzelnen Artikels bestellt."
+ },
+ "max_quantity": {
+ "help": "Leer lassen für keine Obergrenze."
}
},
"stock_location": {
@@ -2500,7 +2506,9 @@
"base_amount": "Grundbetrag",
"base_percent": "Grundprozentsatz",
"preferred_amount": "Betrag",
- "preferred_percent": "Prozent"
+ "preferred_percent": "Prozent",
+ "min_quantity": "Mindestmenge",
+ "max_quantity": "Höchstmenge"
},
"channels": {
"messages": {
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index 997af23776..2fe840a2a9 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -377,6 +377,12 @@
"channels": {
"label": "Sales channels",
"help": "Leave empty to apply this rule on every sales channel."
+ },
+ "min_quantity": {
+ "help": "This price applies only when a customer orders at least this many of a single item."
+ },
+ "max_quantity": {
+ "help": "Leave empty for no upper limit."
}
},
"stock_location": {
@@ -2500,7 +2506,9 @@
"base_amount": "Base Amount",
"base_percent": "Base Percent",
"preferred_amount": "Amount",
- "preferred_percent": "Percent"
+ "preferred_percent": "Percent",
+ "min_quantity": "Minimum quantity",
+ "max_quantity": "Maximum quantity"
},
"channels": {
"messages": {
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index 5ad4cb11f7..cfcb224f38 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -326,6 +326,12 @@
"channels": {
"label": "Canaux de vente",
"help": "Laissez vide pour appliquer cette règle à tous les canaux de vente."
+ },
+ "min_quantity": {
+ "help": "Ce prix s’applique uniquement lorsqu’un client commande au moins cette quantité d’un même article."
+ },
+ "max_quantity": {
+ "help": "Laissez vide pour ne pas fixer de limite supérieure."
}
},
"stock_location": {
@@ -2500,7 +2506,9 @@
"base_amount": "Montant de base",
"base_percent": "Pourcentage de base",
"preferred_amount": "Montant",
- "preferred_percent": "Pourcentage"
+ "preferred_percent": "Pourcentage",
+ "min_quantity": "Quantité minimale",
+ "max_quantity": "Quantité maximale"
},
"channels": {
"messages": {
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 0d452284d5..aeeecf2f5c 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -326,6 +326,12 @@
"channels": {
"label": "Kanały sprzedaży",
"help": "Pozostaw puste, aby zastosować tę regułę do każdego kanału sprzedaży."
+ },
+ "min_quantity": {
+ "help": "Ta cena obowiązuje tylko wtedy, gdy klient zamówi co najmniej tyle sztuk jednego produktu."
+ },
+ "max_quantity": {
+ "help": "Pozostaw puste, aby nie ustawiać górnego limitu."
}
},
"stock_location": {
@@ -2532,7 +2538,9 @@
"base_amount": "Kwota podstawowa",
"base_percent": "Procent podstawowy",
"preferred_amount": "Kwota",
- "preferred_percent": "Procent"
+ "preferred_percent": "Procent",
+ "min_quantity": "Minimalna ilość",
+ "max_quantity": "Maksymalna ilość"
},
"channels": {
"messages": {
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 90435f3d2d..6c6f8c8637 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -374,6 +374,12 @@
"channels": {
"label": "销售渠道",
"help": "留空则此规则适用于所有销售渠道。"
+ },
+ "min_quantity": {
+ "help": "仅当客户订购单个商品达到此数量时,才应用此价格。"
+ },
+ "max_quantity": {
+ "help": "留空表示没有上限。"
}
},
"stock_location": {
@@ -2500,7 +2506,9 @@
"base_amount": "基础金额",
"base_percent": "基础百分比",
"preferred_amount": "金额",
- "preferred_percent": "百分比"
+ "preferred_percent": "百分比",
+ "min_quantity": "最小数量",
+ "max_quantity": "最大数量"
},
"channels": {
"messages": {
diff --git a/spree/core/db/sample_data/wholesale.rb b/spree/core/db/sample_data/wholesale.rb
index c8d4997ed8..580d58ac88 100644
--- a/spree/core/db/sample_data/wholesale.rb
+++ b/spree/core/db/sample_data/wholesale.rb
@@ -20,7 +20,19 @@ end
wholesale_group.add_customers([buyer.id])
price_list = store.price_lists.find_or_create_by!(name: 'Wholesale') do |list|
- list.description = 'Wholesale pricing for approved B2B buyers (40% off retail)'
+ list.description = 'Wholesale pricing for approved B2B buyers (40% off retail) on orders of 10+ per item'
+end
+
+# Case-pack minimum: the trade price applies only when a buyer orders at least
+# WHOLESALE_MIN_QUANTITY of a single item (VolumeRule matches per line item, not
+# per order). Below the threshold the buyer falls back to the retail price — no
+# hard checkout block. This is what makes the demo feel like real wholesale.
+wholesale_min_quantity = 10
+
+unless price_list.price_rules.any? { |rule| rule.is_a?(Spree::PriceRules::VolumeRule) }
+ volume_rule = Spree::PriceRules::VolumeRule.new(price_list: price_list)
+ volume_rule.preferred_min_quantity = wholesale_min_quantity
+ volume_rule.save!
end
unless price_list.price_rules.any? { |rule| rule.is_a?(Spree::PriceRules::CustomerGroupRule) }
diff --git a/spree/core/spec/services/spree/sample_data/loader_spec.rb b/spree/core/spec/services/spree/sample_data/loader_spec.rb
index eab0c7222d..696a1ceb66 100644
--- a/spree/core/spec/services/spree/sample_data/loader_spec.rb
+++ b/spree/core/spec/services/spree/sample_data/loader_spec.rb
@@ -47,12 +47,19 @@ RSpec.describe Spree::SampleData::Loader, type: :service, without_global_store:
expect(group.customers).to include(buyer)
end
- it 'creates an active wholesale price list keyed to the group' do
+ it 'creates an active wholesale price list keyed to the group with a case-pack minimum' do
price_list = store.price_lists.find_by(name: 'Wholesale')
expect(price_list.status).to eq('active')
- expect(price_list.price_rules.first).to be_a(Spree::PriceRules::CustomerGroupRule)
+ expect(price_list.price_rules.map(&:class)).to include(
+ Spree::PriceRules::CustomerGroupRule,
+ Spree::PriceRules::VolumeRule
+ )
+ expect(price_list.match_policy).to eq('all')
expect(price_list.prices.count).to be > 50
+
+ volume_rule = price_list.price_rules.find { |rule| rule.is_a?(Spree::PriceRules::VolumeRule) }
+ expect(volume_rule.preferred_min_quantity).to eq(10)
end
it 'mints a wholesale-bound publishable key' do