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: exchange replacements show the product actually being sent (#14721)

Damian Legawiec5f2b412966a3
diff --git a/.changeset/dashboard-exchange-replacement-names.md b/.changeset/dashboard-exchange-replacement-names.md
new file mode 100644
index 0000000000..b317295a67
--- /dev/null
+++ b/.changeset/dashboard-exchange-replacement-names.md
@@ -0,0 +1,6 @@
+---
+"@spree/dashboard": patch
+"@spree/dashboard-core": patch
+---
+
+An exchange's replacement fulfillment, its packing slip and the Exchanges card now name the replacement product instead of the original one.
diff --git a/packages/dashboard-core/src/lib/fulfillment-items.test.ts b/packages/dashboard-core/src/lib/fulfillment-items.test.ts
index 52a7da1c40..00809d1063 100644
--- a/packages/dashboard-core/src/lib/fulfillment-items.test.ts
+++ b/packages/dashboard-core/src/lib/fulfillment-items.test.ts
@@ -82,6 +82,35 @@ describe('fulfillmentItemRows', () => {
})
})
+ it('names an exchange replacement by its own variant, not the original line item', () => {
+ const rows = fulfillmentItemRows(
+ fulfillment({
+ fulfillment_items: [
+ {
+ line_item_id: 'li_1',
+ variant_id: 'variant_2',
+ name: 'Blender',
+ options_text: 'White',
+ quantity: 1,
+ },
+ ],
+ }),
+ [lineItem({ variant_id: 'variant_1' })],
+ )
+
+ expect(rows).toEqual([
+ {
+ key: 'li_1:variant_2',
+ lineItem: expect.objectContaining({ id: 'li_1' }),
+ name: 'Blender',
+ optionsText: 'White',
+ thumbnailUrl: null,
+ displayPrice: null,
+ quantity: 1,
+ },
+ ])
+ })
+
it('keys an item with no line item by its variant so it still renders', () => {
const rows = fulfillmentItemRows(
fulfillment({
diff --git a/packages/dashboard-core/src/lib/fulfillment-items.ts b/packages/dashboard-core/src/lib/fulfillment-items.ts
index 421e6801b2..10437b89e0 100644
--- a/packages/dashboard-core/src/lib/fulfillment-items.ts
+++ b/packages/dashboard-core/src/lib/fulfillment-items.ts
@@ -9,6 +9,7 @@
/** The line-item fields the grouping reads. */
export interface GroupableLineItem {
id: string
+ variant_id?: string | null
name: string
options_text?: string | null
quantity: number
@@ -37,7 +38,10 @@ export interface GroupableFulfillment {
* line item it descends from, plus how many of its units this group holds.
*/
export interface FulfillmentItemRow {
- /** Stable within one group — a line item appears at most once per group. */
+ /**
+ * Stable within one group — a line item appears at most once per group,
+ * plus once per replacement variant an exchange ships against it.
+ */
key: string
lineItem: GroupableLineItem | null
/** Falls back to the fulfillment item's own copy when the line item is gone. */
@@ -94,20 +98,39 @@ export function fulfilledQuantities(fulfillments: GroupableFulfillment[]): Map<s
return fulfilled
}
-/** Builds a row, preferring the line item's own copy over the fulfillment item's. */
+/**
+ * An exchange ships its replacement against the original line item, so the
+ * unit's variant is the only thing that names what is actually in the box.
+ */
+function isReplacement(
+ lineItem: GroupableLineItem | undefined,
+ item: Pick<GroupableFulfillmentItem, 'variant_id'> | null,
+): boolean {
+ return Boolean(
+ lineItem?.variant_id && item?.variant_id && lineItem.variant_id !== item.variant_id,
+ )
+}
+
+/**
+ * Builds a row, preferring the line item's own copy over the fulfillment
+ * item's — unless the unit is a replacement, whose name, image and price the
+ * line item does not describe.
+ */
function buildRow(
key: string,
lineItem: GroupableLineItem | undefined,
- fallback: Pick<GroupableFulfillmentItem, 'name' | 'options_text'> | null,
+ fallback: Pick<GroupableFulfillmentItem, 'variant_id' | 'name' | 'options_text'> | null,
quantity: number,
): FulfillmentItemRow {
+ const copy = isReplacement(lineItem, fallback) ? undefined : lineItem
+
return {
key,
lineItem: lineItem ?? null,
- name: lineItem?.name ?? fallback?.name ?? '',
- optionsText: lineItem?.options_text ?? fallback?.options_text ?? null,
- thumbnailUrl: lineItem?.thumbnail_url ?? null,
- displayPrice: lineItem?.display_price ?? null,
+ name: copy?.name ?? fallback?.name ?? '',
+ optionsText: copy?.options_text ?? fallback?.options_text ?? null,
+ thumbnailUrl: copy?.thumbnail_url ?? null,
+ displayPrice: copy?.display_price ?? null,
quantity,
}
}
@@ -128,7 +151,10 @@ export function fulfillmentItemRows(
for (const item of fulfillment.fulfillment_items ?? []) {
// A fulfillment item with no line item still ships; key it by variant so
// it renders rather than vanishing.
- const key = item.line_item_id ?? item.variant_id
+ const lineItem = item.line_item_id ? byId.get(item.line_item_id) : undefined
+ const key = isReplacement(lineItem, item)
+ ? `${item.line_item_id}:${item.variant_id}`
+ : (item.line_item_id ?? item.variant_id)
if (!key) continue
const existing = rows.get(key)
@@ -137,15 +163,7 @@ export function fulfillmentItemRows(
continue
}
- rows.set(
- key,
- buildRow(
- key,
- item.line_item_id ? byId.get(item.line_item_id) : undefined,
- item,
- item.quantity,
- ),
- )
+ rows.set(key, buildRow(key, lineItem, item, item.quantity))
}
return [...rows.values()]
diff --git a/packages/dashboard/src/components/spree/order-post-sale-cards.tsx b/packages/dashboard/src/components/spree/order-post-sale-cards.tsx
index 217b98d1f6..fc6725e124 100644
--- a/packages/dashboard/src/components/spree/order-post-sale-cards.tsx
+++ b/packages/dashboard/src/components/spree/order-post-sale-cards.tsx
@@ -36,6 +36,7 @@ import {
useOrderClaims,
useOrderExchanges,
} from '../../hooks/use-post-sale'
+import { variantLabel } from '../../lib/variant-label'
import {
CreateClaimDialog,
CreateExchangeDialog,
@@ -167,11 +168,9 @@ export function OrderExchangesCard({ order }: { order: Order }) {
{(exchange.exchange_line_items ?? []).map((line) => (
<div key={line.id} className="flex items-center justify-between text-sm">
<span className="truncate">
- {line.original_variant?.product_name ?? line.original_variant_id}
+ {variantLabel(line.original_variant, line.original_variant_id)}
{' → '}
- {line.new_variant?.options_text ||
- line.new_variant?.sku ||
- line.new_variant_id}
+ {variantLabel(line.new_variant, line.new_variant_id)}
</span>
<span className="text-muted-foreground">×{line.quantity}</span>
</div>
diff --git a/packages/dashboard/src/components/spree/order-returns-card.tsx b/packages/dashboard/src/components/spree/order-returns-card.tsx
index aced6656b9..557da79d10 100644
--- a/packages/dashboard/src/components/spree/order-returns-card.tsx
+++ b/packages/dashboard/src/components/spree/order-returns-card.tsx
@@ -34,6 +34,7 @@ import i18n from 'i18next'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useOrderReturns, useReturnActions } from '../../hooks/use-returns'
+import { variantLabel } from '../../lib/variant-label'
import { ShippingDocuments } from './orders/shipping-documents'
import { ShippingLabelRow } from './orders/shipping-label-row'
import { CreateReturnDialog, fulfilledUnits } from './post-sale-create-dialogs'
@@ -42,15 +43,6 @@ import { CreateReturnDialog, fulfilledUnits } from './post-sale-create-dialogs'
// would render a menu button that opens onto nothing.
const RETURN_ACTIONABLE = ['requested', 'approved', 'received']
-/** "Product — Small / Blue", falling back to the SKU or the raw id. */
-function variantLabel(line: ReturnLineItem): string {
- const variant = line.variant
- if (!variant) return line.variant_id ?? ''
-
- const parts = [variant.product_name, variant.options_text].filter(Boolean)
- return parts.length > 0 ? parts.join(' — ') : (variant.sku ?? line.variant_id ?? '')
-}
-
/**
* Returns on an order. Each status change is a distinct action rather than an
* editable status field, because each one carries its own arguments —
@@ -323,7 +315,7 @@ function ReturnLineRow({ line, status }: { line: ReturnLineItem; status: string
return (
<div className="flex items-center justify-between text-sm">
- <span className="truncate">{variantLabel(line)}</span>
+ <span className="truncate">{variantLabel(line.variant, line.variant_id)}</span>
<span className="flex items-center gap-2 text-muted-foreground">
{received && line.received_quantity !== line.quantity && (
<Badge variant="outline">
@@ -362,7 +354,7 @@ function ReceiveDialog({
<ReturnReceiveDialog
lines={(returnRecord.return_line_items ?? []).map((line) => ({
id: line.id,
- label: variantLabel(line),
+ label: variantLabel(line.variant, line.variant_id),
quantity: line.quantity,
}))}
onClose={onClose}
diff --git a/packages/dashboard/src/lib/variant-label.ts b/packages/dashboard/src/lib/variant-label.ts
new file mode 100644
index 0000000000..0a2ddd1fbd
--- /dev/null
+++ b/packages/dashboard/src/lib/variant-label.ts
@@ -0,0 +1,13 @@
+/** "Product — Small / Blue", falling back to the SKU or the raw id. */
+export function variantLabel(
+ variant:
+ | { product_name?: string | null; options_text?: string | null; sku?: string | null }
+ | null
+ | undefined,
+ variantId: string | null | undefined,
+): string {
+ if (!variant) return variantId ?? ''
+
+ const parts = [variant.product_name, variant.options_text].filter(Boolean)
+ return parts.length > 0 ? parts.join(' — ') : (variant.sku ?? variantId ?? '')
+}