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 the seller order page's post-sale cards

Damian Legawiec9f67fc664ceb
diff --git a/docs/api-reference/seller.yaml b/docs/api-reference/seller.yaml
index df00f8e9fd..bf95e1c179 100644
--- a/docs/api-reference/seller.yaml
+++ b/docs/api-reference/seller.yaml
@@ -17316,6 +17316,9 @@ components:
nullable: true
variant:
"$ref": "#/components/schemas/Variant"
+ name:
+ type: string
+ nullable: true
required:
- id
- quantity
@@ -17327,6 +17330,7 @@ components:
- variant_id
- replacement_variant_id
- line_item_id
+ - name
x-typelizer: true
Claim:
type: object
@@ -17433,6 +17437,12 @@ components:
"$ref": "#/components/schemas/Variant"
new_variant:
"$ref": "#/components/schemas/Variant"
+ name:
+ type: string
+ nullable: true
+ new_variant_name:
+ type: string
+ nullable: true
required:
- id
- quantity
@@ -17445,6 +17455,8 @@ components:
- new_variant_id
- line_item_id
- fulfillment_item_id
+ - name
+ - new_variant_name
x-typelizer: true
Exchange:
type: object
@@ -18510,6 +18522,9 @@ components:
nullable: true
variant:
"$ref": "#/components/schemas/Variant"
+ name:
+ type: string
+ nullable: true
required:
- id
- quantity
@@ -18520,6 +18535,7 @@ components:
- variant_id
- line_item_id
- fulfillment_item_id
+ - name
x-typelizer: true
Return:
type: object
diff --git a/packages/seller-dashboard/src/components/orders/fulfillment-fulfill-form.tsx b/packages/seller-dashboard/src/components/orders/fulfillment-fulfill-form.tsx
index a8c9f3b499..9c507eaa98 100644
--- a/packages/seller-dashboard/src/components/orders/fulfillment-fulfill-form.tsx
+++ b/packages/seller-dashboard/src/components/orders/fulfillment-fulfill-form.tsx
@@ -20,14 +20,37 @@ import { useTrackingCarriers } from '../../hooks/use-reasons'
type Row = { itemId: string; label: string; available: number; quantity: number }
+/**
+ * One row per line item, not per fulfillment item.
+ *
+ * A parcel routinely holds two rows for the same line item — the packer
+ * splits a partly-stocked line into an on-hand unit and a backordered one —
+ * and the server addresses what to ship by line item. Left unmerged, the two
+ * rows share a React key, one stepper silently moves the other, and the
+ * payload sends the same `item_id` twice, where last-wins makes a seller who
+ * selected everything ship only half of it.
+ */
function initialRows(fulfillment: Fulfillment): Row[] {
- return (fulfillment.fulfillment_items ?? []).map((item) => ({
- // The server addresses what to ship by line item, not by fulfillment item.
- itemId: item.line_item_id ?? item.id,
- label: [item.name, item.options_text].filter(Boolean).join(' — ') || item.id,
- available: item.quantity,
- quantity: item.quantity,
- }))
+ const byLineItem = new Map<string, Row>()
+
+ for (const item of fulfillment.fulfillment_items ?? []) {
+ const itemId = item.line_item_id ?? item.id
+ const existing = byLineItem.get(itemId)
+
+ if (existing) {
+ existing.available += item.quantity
+ existing.quantity += item.quantity
+ } else {
+ byLineItem.set(itemId, {
+ itemId,
+ label: [item.name, item.options_text].filter(Boolean).join(' — ') || item.id,
+ available: item.quantity,
+ quantity: item.quantity,
+ })
+ }
+ }
+
+ return [...byLineItem.values()]
}
/**
@@ -50,11 +73,23 @@ export function FulfillmentFulfillForm({
const { fulfill } = useFulfillmentActions(orderId)
const { data: carriersData } = useTrackingCarriers()
- const [rows, setRows] = useState<Row[]>(() => initialRows(fulfillment))
const [tracking, setTracking] = useState('')
const [carrier, setCarrier] = useState('')
const [notifyCustomer, setNotifyCustomer] = useState(true)
+ // What the seller has chosen to ship, keyed by line item — empty until they
+ // change something, which is when a row starts differing from the default.
+ const [chosen, setChosen] = useState<Record<string, number>>({})
+
+ // Shipping part of a parcel splits the rest onto a new one, and the source
+ // keeps its id with fewer items. Deriving the rows on every render rather
+ // than seeding state once is what keeps them true after a partial ship —
+ // `useState` would still be showing the pre-split quantities.
+ const rows = initialRows(fulfillment).map((row) => ({
+ ...row,
+ quantity: chosen[row.itemId] ?? row.quantity,
+ }))
+
const carrierOptions = [
{ value: '', label: t('orders.fulfillments.carrier_auto') },
...(carriersData?.data ?? []).map((option) => ({ value: option.id, label: option.name })),
@@ -65,13 +100,12 @@ export function FulfillmentFulfillForm({
const partial = selectedUnits > 0 && selectedUnits < totalUnits
function setQuantity(itemId: string, quantity: number) {
- setRows((current) =>
- current.map((row) =>
- row.itemId === itemId
- ? { ...row, quantity: Math.max(0, Math.min(row.available, quantity)) }
- : row,
- ),
- )
+ const available = rows.find((row) => row.itemId === itemId)?.available ?? 0
+
+ setChosen((current) => ({
+ ...current,
+ [itemId]: Math.max(0, Math.min(available, quantity)),
+ }))
}
async function handleShip() {
diff --git a/packages/seller-dashboard/src/components/orders/post-sale-cards.tsx b/packages/seller-dashboard/src/components/orders/post-sale-cards.tsx
index da97b854a3..768aa9f483 100644
--- a/packages/seller-dashboard/src/components/orders/post-sale-cards.tsx
+++ b/packages/seller-dashboard/src/components/orders/post-sale-cards.tsx
@@ -46,16 +46,16 @@ import {
import { CreateClaimDialog } from './post-sale-create-dialogs'
/**
- * How a variant reads on a post-sale line. A seller's variant serializer
- * carries the SKU and its option values, never a name — the name lives on the
- * product — so the label is built from what is actually there.
+ * How a post-sale line reads. The product name comes from the line itself —
+ * a variant carries a SKU and its option values but never a name — falling
+ * back to the SKU and then the raw id.
*/
-function variantLabel(
+function lineLabel(
+ name: string | null | undefined,
variant: { sku?: string | null; options_text?: string | null } | undefined,
fallback: string | null | undefined,
): string {
- const parts = [variant?.sku, variant?.options_text].filter(Boolean)
- return parts.length > 0 ? parts.join(' · ') : (fallback ?? '')
+ return [name, variant?.options_text].filter(Boolean).join(' · ') || variant?.sku || fallback || ''
}
const EXCHANGE_ACTIONABLE = ['requested', 'approved', 'received']
@@ -155,8 +155,8 @@ export function ExchangesCard({ order }: { order: Order }) {
{exchange.exchange_line_items?.map((line) => (
<div key={line.id} className="flex items-center justify-between gap-3 text-sm">
<span className="truncate">
- {variantLabel(line.original_variant, line.original_variant_id)} →{' '}
- {variantLabel(line.new_variant, line.new_variant_id)}
+ {lineLabel(line.name, line.original_variant, line.original_variant_id)} →{' '}
+ {lineLabel(line.new_variant_name, line.new_variant, line.new_variant_id)}
</span>
<span className="shrink-0 text-muted-foreground">× {line.quantity}</span>
</div>
@@ -271,7 +271,7 @@ export function ClaimsCard({ order }: { order: Order }) {
{claim.claim_line_items?.map((line) => (
<div key={line.id} className="flex items-center justify-between gap-3 text-sm">
<span className="min-w-0 truncate">
- {variantLabel(line.variant, line.variant_id)}
+ {lineLabel(line.name, line.variant, line.variant_id)}
{line.description && (
<span className="text-muted-foreground"> — {line.description}</span>
)}
diff --git a/packages/seller-dashboard/src/components/orders/returns-card.tsx b/packages/seller-dashboard/src/components/orders/returns-card.tsx
index 2a500f68bc..4f37875206 100644
--- a/packages/seller-dashboard/src/components/orders/returns-card.tsx
+++ b/packages/seller-dashboard/src/components/orders/returns-card.tsx
@@ -40,6 +40,20 @@ import { CreateReturnDialog, fulfilledUnits } from './post-sale-create-dialogs'
// open onto nothing.
const ACTIONABLE = ['requested', 'approved', 'received']
+/**
+ * How a returned line reads. The product name comes from the line itself —
+ * a variant carries a SKU and its options but never a name — with the
+ * option values after it so two sizes of one product are distinguishable.
+ */
+function postSaleLabel(line: ReturnLineItem): string {
+ return (
+ [line.name, line.variant?.options_text].filter(Boolean).join(' · ') ||
+ line.variant?.sku ||
+ line.variant_id ||
+ ''
+ )
+}
+
/**
* Goods coming back on this order.
*
@@ -179,10 +193,7 @@ export function ReturnsCard({ order }: { order: Order }) {
function ReturnLineRow({ line }: { line: ReturnLineItem }) {
const { t } = useTranslation()
- const label =
- [line.variant?.sku, line.variant?.options_text].filter(Boolean).join(' · ') ||
- line.variant_id ||
- ''
+ const label = postSaleLabel(line)
return (
<div className="flex items-center justify-between gap-3 text-sm">
@@ -251,10 +262,7 @@ function ReceiveDialog({
<div className="flex flex-col gap-3">
{lines.map((line) => (
<div key={line.id} className="flex items-center justify-between gap-3">
- <span className="min-w-0 truncate text-sm">
- {[line.variant?.sku, line.variant?.options_text].filter(Boolean).join(' · ') ||
- line.variant_id}
- </span>
+ <span className="min-w-0 truncate text-sm">{postSaleLabel(line)}</span>
<div className="flex shrink-0 items-center gap-3">
<label className="flex items-center gap-2 text-xs" htmlFor={`resell-${line.id}`}>
{t('orders.post_sale.returns.resellable')}
diff --git a/packages/seller-dashboard/src/locales/en.json b/packages/seller-dashboard/src/locales/en.json
index 1d56310fe9..3f22ab5775 100644
--- a/packages/seller-dashboard/src/locales/en.json
+++ b/packages/seller-dashboard/src/locales/en.json
@@ -268,11 +268,14 @@
"items": "Items",
"not_found": "That order could not be found.",
"payment_statuses": {
- "balance_due": "Balance due",
- "credit_owed": "Credit owed",
- "failed": "Failed",
+ "none": "Unpaid",
+ "authorized": "Authorized",
+ "partially_paid": "Partially paid",
"paid": "Paid",
- "void": "Void"
+ "partially_refunded": "Partially refunded",
+ "refunded": "Refunded",
+ "overcharged": "Overcharged",
+ "voided": "Voided"
},
"search_placeholder": "Search by order number…",
"shipping_address": "Ships to",
diff --git a/packages/seller-dashboard/src/pages/order.tsx b/packages/seller-dashboard/src/pages/order.tsx
index 0319eda0fc..feadacc608 100644
--- a/packages/seller-dashboard/src/pages/order.tsx
+++ b/packages/seller-dashboard/src/pages/order.tsx
@@ -78,13 +78,17 @@ export function OrderPage() {
{order.fulfillment_status && (
<StatusBadge
status={order.fulfillment_status}
- label={t(`orders.fulfillment_statuses.${order.fulfillment_status}`)}
+ label={t(`orders.fulfillment_statuses.${order.fulfillment_status}`, {
+ defaultValue: order.fulfillment_status,
+ })}
/>
)}
{order.payment_status && (
<StatusBadge
status={order.payment_status}
- label={t(`orders.payment_statuses.${order.payment_status}`)}
+ label={t(`orders.payment_statuses.${order.payment_status}`, {
+ defaultValue: order.payment_status,
+ })}
/>
)}
</>
diff --git a/packages/seller-dashboard/src/tables/orders.tsx b/packages/seller-dashboard/src/tables/orders.tsx
index 9ae4b10f96..d080d6d9a2 100644
--- a/packages/seller-dashboard/src/tables/orders.tsx
+++ b/packages/seller-dashboard/src/tables/orders.tsx
@@ -13,7 +13,16 @@ const FULFILLMENT_STATUSES = [
'delivered',
'canceled',
] as const
-const PAYMENT_STATUSES = ['balance_due', 'paid', 'credit_owed', 'failed', 'void'] as const
+const PAYMENT_STATUSES = [
+ 'none',
+ 'authorized',
+ 'partially_paid',
+ 'paid',
+ 'partially_refunded',
+ 'refunded',
+ 'overcharged',
+ 'voided',
+] as const
defineTable<Order>('seller-orders', {
title: i18n.t('orders.title'),
diff --git a/packages/seller-sdk/src/seller-client.ts b/packages/seller-sdk/src/seller-client.ts
index eea2131d06..640c97622b 100644
--- a/packages/seller-sdk/src/seller-client.ts
+++ b/packages/seller-sdk/src/seller-client.ts
@@ -573,10 +573,18 @@ export class SellerClient {
*/
returns: {
list: (orderId: string, options?: RequestOptions): Promise<{ data: Return[] }> =>
- this.request<{ data: Return[] }>('GET', `/orders/${orderId}/returns`, options),
+ this.request<{ data: Return[] }>('GET', `/orders/${orderId}/returns`, {
+ ...options,
+ // The nested collections are expand-gated server-side, so a bare
+ // list answers with the record and none of its contents.
+ params: { expand: ['return_line_items', 'return_line_items.variant', 'reason'] },
+ }),
get: (orderId: string, id: string, options?: RequestOptions): Promise<Return> =>
- this.request<Return>('GET', `/orders/${orderId}/returns/${id}`, options),
+ this.request<Return>('GET', `/orders/${orderId}/returns/${id}`, {
+ ...options,
+ params: { expand: ['return_line_items', 'return_line_items.variant', 'reason'] },
+ }),
create: (
orderId: string,
@@ -634,10 +642,32 @@ export class SellerClient {
/** Goods swapped for different ones. */
exchanges: {
list: (orderId: string, options?: RequestOptions): Promise<{ data: Exchange[] }> =>
- this.request<{ data: Exchange[] }>('GET', `/orders/${orderId}/exchanges`, options),
+ this.request<{ data: Exchange[] }>('GET', `/orders/${orderId}/exchanges`, {
+ ...options,
+ // The nested collections are expand-gated server-side, so a bare
+ // list answers with the record and none of its contents.
+ params: {
+ expand: [
+ 'exchange_line_items',
+ 'exchange_line_items.original_variant',
+ 'exchange_line_items.new_variant',
+ 'reason',
+ ],
+ },
+ }),
get: (orderId: string, id: string, options?: RequestOptions): Promise<Exchange> =>
- this.request<Exchange>('GET', `/orders/${orderId}/exchanges/${id}`, options),
+ this.request<Exchange>('GET', `/orders/${orderId}/exchanges/${id}`, {
+ ...options,
+ params: {
+ expand: [
+ 'exchange_line_items',
+ 'exchange_line_items.original_variant',
+ 'exchange_line_items.new_variant',
+ 'reason',
+ ],
+ },
+ }),
create: (
orderId: string,
@@ -700,10 +730,18 @@ export class SellerClient {
*/
claims: {
list: (orderId: string, options?: RequestOptions): Promise<{ data: Claim[] }> =>
- this.request<{ data: Claim[] }>('GET', `/orders/${orderId}/claims`, options),
+ this.request<{ data: Claim[] }>('GET', `/orders/${orderId}/claims`, {
+ ...options,
+ // The nested collections are expand-gated server-side, so a bare
+ // list answers with the record and none of its contents.
+ params: { expand: ['claim_line_items', 'claim_line_items.variant', 'reason'] },
+ }),
get: (orderId: string, id: string, options?: RequestOptions): Promise<Claim> =>
- this.request<Claim>('GET', `/orders/${orderId}/claims/${id}`, options),
+ this.request<Claim>('GET', `/orders/${orderId}/claims/${id}`, {
+ ...options,
+ params: { expand: ['claim_line_items', 'claim_line_items.variant', 'reason'] },
+ }),
create: (
orderId: string,
diff --git a/packages/seller-sdk/src/types/generated/ClaimLineItem.ts b/packages/seller-sdk/src/types/generated/ClaimLineItem.ts
index 9e95e2d55c..291bafaee8 100644
--- a/packages/seller-sdk/src/types/generated/ClaimLineItem.ts
+++ b/packages/seller-sdk/src/types/generated/ClaimLineItem.ts
@@ -1,4 +1,4 @@
-// Typelizer digest f35f7fc79f41f512821ba3ea89e38351
+// Typelizer digest 2e8fa5aff783d0a1e9ff8d0b02767104
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {Variant} from '@/types'
@@ -15,6 +15,7 @@ type ClaimLineItem = {
replacement_variant_id: string | null;
line_item_id: string | null;
variant?: Variant;
+ name: string | null;
}
export default ClaimLineItem;
diff --git a/packages/seller-sdk/src/types/generated/ExchangeLineItem.ts b/packages/seller-sdk/src/types/generated/ExchangeLineItem.ts
index d35cc1aa4f..6d51a4db9a 100644
--- a/packages/seller-sdk/src/types/generated/ExchangeLineItem.ts
+++ b/packages/seller-sdk/src/types/generated/ExchangeLineItem.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 6230936c0a851435292a2454ad40dd94
+// Typelizer digest 85f8bbd9e4f980dd089ca763846fc65d
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {Variant} from '@/types'
@@ -17,6 +17,8 @@ type ExchangeLineItem = {
fulfillment_item_id: string | null;
original_variant?: Variant;
new_variant?: Variant;
+ name: string | null;
+ new_variant_name: string | null;
}
export default ExchangeLineItem;
diff --git a/packages/seller-sdk/src/types/generated/ReturnLineItem.ts b/packages/seller-sdk/src/types/generated/ReturnLineItem.ts
index d70a4964cd..e21c41b146 100644
--- a/packages/seller-sdk/src/types/generated/ReturnLineItem.ts
+++ b/packages/seller-sdk/src/types/generated/ReturnLineItem.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 78bd739c8744e19f6a15a92d7dcc2fe5
+// Typelizer digest 262eef30439d5a4a271a8efff3bfa498
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {Variant} from '@/types'
@@ -14,6 +14,7 @@ type ReturnLineItem = {
line_item_id: string | null;
fulfillment_item_id: string | null;
variant?: Variant;
+ name: string | null;
}
export default ReturnLineItem;
diff --git a/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb
index d6c8d21f75..4943a3de34 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb
@@ -176,7 +176,7 @@ module Spree
if result.success?
render json: serialize(result.value)
else
- render_service_error(@fulfillment.errors.presence || result.error)
+ render_result_error(result)
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/seller/claim_line_item_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/claim_line_item_serializer.rb
index 45a22e4513..67a062ebcd 100644
--- a/spree/api/app/serializers/spree/api/v3/seller/claim_line_item_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/seller/claim_line_item_serializer.rb
@@ -7,6 +7,12 @@ module Spree
# One line of a claim — what went wrong, and what is being sent or
# refunded to put it right.
class ClaimLineItemSerializer < V3::ClaimLineItemSerializer
+ typelize name: [:string, nullable: true]
+
+ attribute :name do |line|
+ line.line_item&.name || line.variant&.name
+ end
+
one :variant, resource: proc { Spree.api.seller_variant_serializer }, if: proc { expand?('variant') }
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/seller/exchange_line_item_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/exchange_line_item_serializer.rb
index 2e1ecbbb60..5539256b8e 100644
--- a/spree/api/app/serializers/spree/api/v3/seller/exchange_line_item_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/seller/exchange_line_item_serializer.rb
@@ -7,6 +7,17 @@ module Spree
# One line of an exchange — what came back and what goes out in its
# place. Both variants expand through the seller's own serializer.
class ExchangeLineItemSerializer < V3::ExchangeLineItemSerializer
+ typelize name: [:string, nullable: true],
+ new_variant_name: [:string, nullable: true]
+
+ attribute :name do |line|
+ line.line_item&.name || line.original_variant&.name
+ end
+
+ attribute :new_variant_name do |line|
+ line.new_variant&.name
+ end
+
one :original_variant, resource: proc { Spree.api.seller_variant_serializer },
if: proc { expand?('original_variant') }
one :new_variant, resource: proc { Spree.api.seller_variant_serializer },
diff --git a/spree/api/app/serializers/spree/api/v3/seller/return_line_item_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/return_line_item_serializer.rb
index 5c88765268..2a173edb3c 100644
--- a/spree/api/app/serializers/spree/api/v3/seller/return_line_item_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/seller/return_line_item_serializer.rb
@@ -4,9 +4,18 @@ module Spree
module Api
module V3
module Seller
- # One line of a return. The variant expands through the seller's own
- # variant serializer, so nothing operator-only rides along with it.
+ # One line of a return.
+ #
+ # Names what came back on the line itself: a variant carries a SKU and
+ # its option values but no product name, so a card built from the
+ # variant alone would show a bare id for anything unSKU'd.
class ReturnLineItemSerializer < V3::ReturnLineItemSerializer
+ typelize name: [:string, nullable: true]
+
+ attribute :name do |line|
+ line.line_item&.name || line.variant&.name
+ end
+
one :variant, resource: proc { Spree.api.seller_variant_serializer }, if: proc { expand?('variant') }
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders/returns_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders/returns_controller_spec.rb
index 3c2a85a700..806e3ffefa 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders/returns_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders/returns_controller_spec.rb
@@ -169,4 +169,29 @@ RSpec.describe Spree::Api::V3::Seller::Orders::ReturnsController, type: :control
expect(response).to have_http_status(:forbidden)
end
end
+
+ # The cards render a return's contents, and every nested collection is
+ # expand-gated — a bare list answers with the record and nothing in it.
+ describe 'expanding the contents' do
+ it 'returns the line items and their variants when asked' do
+ open_return
+
+ get :index, params: {
+ order_id: order.prefixed_id,
+ expand: 'return_line_items,return_line_items.variant'
+ }, as: :json
+
+ row = json_response['data'].first
+ expect(row['return_line_items']).to be_present
+ expect(row['return_line_items'].first['variant']).to be_present
+ end
+
+ it 'omits them when not asked' do
+ open_return
+
+ get :index, params: { order_id: order.prefixed_id }, as: :json
+
+ expect(json_response['data'].first).not_to have_key('return_line_items')
+ end
+ end
end