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.

Fix: saving a product overwrote its base price with a price-list amount (#14688)

Łukasz Adamczyk71db62165f62
diff --git a/docs/developer/core-concepts/pricing.mdx b/docs/developer/core-concepts/pricing.mdx
index 0c7a5cde05..a3cb202fca 100644
--- a/docs/developer/core-concepts/pricing.mdx
+++ b/docs/developer/core-concepts/pricing.mdx
@@ -98,6 +98,12 @@ spree api post /prices/bulk_upsert -d '{
</CodeGroup>
+`?expand=variants.prices` returns every price a variant has, Price List amounts
+and quantity breaks included; each row carries its `price_list_id`, which is
+`null` for a base price. Writing `prices` back on a variant sets **base prices
+only** — a currency left out is removed — so filter the rows you read before
+sending them back. To write what a Price List charges, use `/prices`.
+
## Price Lists
Price Lists allow you to create different pricing strategies based on various conditions. This enables advanced pricing scenarios like:
diff --git a/docs/plans/6.0-volume-pricing.md b/docs/plans/6.0-volume-pricing.md
index 845d5c0fc3..134de20eef 100644
--- a/docs/plans/6.0-volume-pricing.md
+++ b/docs/plans/6.0-volume-pricing.md
@@ -257,6 +257,15 @@ bands on the adjustment cover it with the implemented semantics).
rule for the next such change: a column that changes row identity is not
finished at the writers and the resolver — the readers that never asked the
resolver are where it leaks.**
+- **One reader was missed, and it was in the dashboard (2026-09-22).**
+ `expand=variants.prices` serves every row a variant has, which is correct;
+ the product form took the lot and posted it back as the variant's base
+ prices, so saving a product on a price list overwrote its shop price with
+ the list's amount and deleted the currencies the list only held
+ placeholders for (V-3661). The fix filters in `variantToFormValues`, which
+ both panels share. Narrowing the serializers was tried and rejected on
+ review: a published read is not where a writer's constraint belongs. A form
+ that posts a collection back owes that collection a filter.
- The break cap lives in `Prices::BulkUpsert` rather than in a controller,
because three write paths reach that service and none of them run model
validations. Counted the same way in the model and the service: breaks above
diff --git a/docs/plans/decisions.md b/docs/plans/decisions.md
index 7bc3821544..40806af2be 100644
--- a/docs/plans/decisions.md
+++ b/docs/plans/decisions.md
@@ -10,6 +10,20 @@ Rejected: walking `order.returns/claims/exchanges` to their credits, which needs
**Plans amended:** `6.0-returns-exchanges-claims.md` (the `Return#refunded_total` replacement bullet), `6.0-cart-order-split.md` (the derive-then-persist rules).
+## 2026-09-22: The form that writes base prices filters for them itself; the variant `prices` expand stays whole
+
+**Context:** Saving a product in the dashboard destroyed prices nobody had touched. On a store with a second market and a sale list, Save rewrote the USD shop price from $549.99 to the list's $329.99 and soft-deleted the EUR price outright; with a volume ladder on the same list the shop price became the 50-unit rate of $249.99, a 55% cut charged to every single-unit customer (V-3661). `expand=variants.prices` serves every row a variant has — base rows, price-list rows and every quantity break — and `variantToFormValues` took the lot, dropping `price_list_id` as it went. The card then showed the first row of the picked currency, whichever kind the database happened to return first, so which currency was destroyed and which was overwritten varied between saves. The form posts that array back under the same key, where `Spree::Variant#prices=` reads every entry as a base price; a null-amount placeholder, which is how a price list records membership (2026-09-08), arrived as a blank amount and took `set_price`'s delete branch.
+
+**Decision:** The form filters. `variantToFormValues` keeps only rows with a null `price_list_id`, so what the product form holds is what it is entitled to write. It lives in `@spree/dashboard-core`, which both the operator dashboard and the seller panel build their product form from, so one filter covers both panels.
+
+Rejected: scoping the serializers' `many :prices` to base rows, which was built and reverted on review. `expand=variants.prices` means "this variant's prices" to every client, not "the subset one form may write", and narrowing a published read to protect one writer takes the price-list amounts and quantity breaks away from everyone else reading them in a single call. The rule it breaks is the general one: a read contract is not the place to encode a writer's constraint. Also rejected: hardening `Spree::Variant#prices=` against duplicate currencies, which treats the symptom. Whether a payload naming one currency twice should be a 422, and whether a blank amount should keep meaning "delete this price", are both still open.
+
+**Consequences:** No API change, no serializer change, no generated types move — the fix is four lines of TypeScript and a `price_list_id` on the panel's price type. The Prices card now shows the shop price rather than whichever row sorted first. Any *other* client that reads `variants.prices` and posts it back still meets the same trap, and the setter is what would have to refuse it; that is the open question above rather than something the read should have absorbed.
+
+This is the 2026-09-07 sweep ("a column that changes row identity is not finished at the writers") reaching a reader it missed — but the reader is in the dashboard, not the API. The rule to carry forward: when a form hydrates from a collection it will post back, it owes that collection a filter, because the setter on the other side cannot tell which rows the form was entitled to touch.
+
+**Plans amended:** `6.0-volume-pricing.md` (Notes from the build — the reader list).
+
## 2026-09-18: A return is owed what the warehouse counted, and the line works that out
**Context:** A return requested for three units and received as two offered the merchant a refund of all three. The dashboard pre-fills the refund dialog from `refundable_total` and always submits what is in the box, so the workflow's own correct default (`amount || received_total`) was never reached, and the only ceiling was the same announced-quantity figure — nothing refused it. On a $269.97 order line the customer was paid $89.99 too much and the seller clawed back $242.97 where $161.98 was owed, because `SellerTransfers::Reverse` scales the seller's share by the refund amount. Reproduced from a clean install, then twice more here: identical orders refunded with and without an `amount` came out $269.97 and $179.98 (V-3654). The same received-based arithmetic existed in three places — `Returns::Refund#received_total`, `Return#refunded_line_amounts` and nowhere else that anyone reading `refund_total` would find — which is how the offered figure and the owed figure came to disagree.
diff --git a/packages/dashboard-core/src/products/product-form-mapping.test.ts b/packages/dashboard-core/src/products/product-form-mapping.test.ts
new file mode 100644
index 0000000000..66528e09fd
--- /dev/null
+++ b/packages/dashboard-core/src/products/product-form-mapping.test.ts
@@ -0,0 +1,72 @@
+import { describe, expect, it } from 'vitest'
+import { variantToFormValues, variantToWirePayload } from './product-form-mapping'
+import type { PanelVariant } from './product-types'
+
+// variantToFormValues reads a handful of fields; cast a minimal fixture rather
+// than fabricating the whole serialized variant it ignores.
+function variantWith(prices: PanelVariant['prices']): PanelVariant {
+ return { id: 'variant_1', sku: 'ESP-SEMI', option_values: [], prices } as unknown as PanelVariant
+}
+
+describe('variantToFormValues prices', () => {
+ it('keeps the shop price in each currency', () => {
+ const values = variantToFormValues(
+ variantWith([
+ { currency: 'USD', amount: '549.99', price_list_id: null },
+ { currency: 'EUR', amount: '509.99', price_list_id: null },
+ ]),
+ 0,
+ )
+
+ expect(values.prices).toEqual([
+ { currency: 'USD', amount: '549.99', compare_at_amount: null },
+ { currency: 'EUR', amount: '509.99', compare_at_amount: null },
+ ])
+ })
+
+ // The form posts this array straight back as the variant's base prices, so a
+ // price-list row taken in here returns as the shop price and overwrites it —
+ // and a list's null-amount placeholder returns as "no price" and deletes it.
+ it('drops what a price list charges, whatever order the rows arrive in', () => {
+ const values = variantToFormValues(
+ variantWith([
+ { currency: 'USD', amount: '329.99', price_list_id: 'pl_1' },
+ { currency: 'USD', amount: '549.99', price_list_id: null },
+ { currency: 'EUR', amount: null, price_list_id: 'pl_1' },
+ { currency: 'EUR', amount: '509.99', price_list_id: null },
+ { currency: 'USD', amount: '249.99', price_list_id: 'pl_1' },
+ ]),
+ 0,
+ )
+
+ expect(values.prices).toEqual([
+ { currency: 'USD', amount: '549.99', compare_at_amount: null },
+ { currency: 'EUR', amount: '509.99', compare_at_amount: null },
+ ])
+ })
+
+ it('ships only the shop prices back to the API', () => {
+ const values = variantToFormValues(
+ variantWith([
+ { currency: 'USD', amount: '549.99', price_list_id: null },
+ { currency: 'USD', amount: '329.99', price_list_id: 'pl_1' },
+ ]),
+ 0,
+ )
+
+ expect(variantToWirePayload(values, 0).prices).toEqual([
+ { currency: 'USD', amount: '549.99', compare_at_amount: null },
+ ])
+ })
+
+ // A variant a price list prices but the shop does not has no shop price, and
+ // an empty array is how the form says so.
+ it('leaves no price when only a price list prices the variant', () => {
+ const values = variantToFormValues(
+ variantWith([{ currency: 'USD', amount: '89.99', price_list_id: 'pl_1' }]),
+ 0,
+ )
+
+ expect(values.prices).toEqual([])
+ })
+})
diff --git a/packages/dashboard-core/src/products/product-form-mapping.ts b/packages/dashboard-core/src/products/product-form-mapping.ts
index b236905f9f..f72d77716c 100644
--- a/packages/dashboard-core/src/products/product-form-mapping.ts
+++ b/packages/dashboard-core/src/products/product-form-mapping.ts
@@ -44,8 +44,10 @@ export function variantToFormValues(variant: PanelVariant, position: number): Va
preorder_ships_at: variant.preorder_ships_at ?? null,
backorder_limit: variant.backorder_limit ?? null,
tax_category_id: variant.tax_category_id ?? null,
+ // Base prices only: this array is posted back as the variant's base
+ // prices, so a price-list row taken in here returns as the shop price.
prices: (variant.prices ?? [])
- .filter((p) => p.currency != null)
+ .filter((p) => p.currency != null && p.price_list_id == null)
.map((p) => ({
currency: p.currency as string,
// Keep amounts as the canonical decimal strings the API returns.
diff --git a/packages/dashboard-core/src/products/product-types.ts b/packages/dashboard-core/src/products/product-types.ts
index 89ed96911a..8323ba3f75 100644
--- a/packages/dashboard-core/src/products/product-types.ts
+++ b/packages/dashboard-core/src/products/product-types.ts
@@ -44,6 +44,7 @@ export interface PanelVariant {
amount?: string | null
currency?: string | null
compare_at_amount?: string | null
+ price_list_id?: string | null
}>
cost_price?: string | null
cost_currency?: string | null