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.

Show a line as the product it is

Łukasz Adamczykbece57b7bcb1
diff --git a/packages/admin-sdk/src/types/generated/PurchaseOrderItem.ts b/packages/admin-sdk/src/types/generated/PurchaseOrderItem.ts
index af6eaf4098..53b5d2b9ec 100644
--- a/packages/admin-sdk/src/types/generated/PurchaseOrderItem.ts
+++ b/packages/admin-sdk/src/types/generated/PurchaseOrderItem.ts
@@ -1,4 +1,4 @@
-// Typelizer digest fda11dd44513672ebab0b0a20186f8af
+// Typelizer digest 98189f47915c6986cc622ddc0175ee6b
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -16,9 +16,11 @@ interface PurchaseOrderItem {
display_total_cost: string;
purchase_order_id: string | null;
variant_id: string | null;
+ product_id: string | null;
variant_name: string | null;
variant_sku: string | null;
options_text: string | null;
+ thumbnail_url: string | null;
}
export default PurchaseOrderItem;
diff --git a/packages/admin-sdk/src/types/generated/StockTransferItem.ts b/packages/admin-sdk/src/types/generated/StockTransferItem.ts
index 1f3e664561..271dca8c0e 100644
--- a/packages/admin-sdk/src/types/generated/StockTransferItem.ts
+++ b/packages/admin-sdk/src/types/generated/StockTransferItem.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 1f8e2626b5f2f91f76e9434dfc0f7e12
+// Typelizer digest 26b16f95d16e2ebb99b851d665477b3c
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -12,9 +12,11 @@ interface StockTransferItem {
updated_at: string;
stock_transfer_id: string | null;
variant_id: string | null;
+ product_id: string | null;
variant_name: string | null;
variant_sku: string | null;
options_text: string | null;
+ thumbnail_url: string | null;
}
export default StockTransferItem;
diff --git a/packages/dashboard/src/components/spree/variant-link.tsx b/packages/dashboard/src/components/spree/variant-link.tsx
new file mode 100644
index 0000000000..c7406b9b38
--- /dev/null
+++ b/packages/dashboard/src/components/spree/variant-link.tsx
@@ -0,0 +1,48 @@
+import { useStore } from '@spree/dashboard-core'
+import { Thumbnail } from '@spree/dashboard-ui'
+import { PackageIcon } from '@spree/dashboard-ui/icons'
+import { Link } from '@tanstack/react-router'
+
+/**
+ * A document line's variant — its image, its name linked to the product that
+ * owns it, and optionally its SKU underneath.
+ *
+ * Renders plain text when the line names no product. A line outlives the
+ * variant it names — the same way the movement ledger outlives the level it
+ * describes — so a discontinued SKU still has to appear on the transfer or
+ * order that shipped it.
+ */
+export function VariantLink({
+ productId,
+ name,
+ sku,
+ thumbnailUrl,
+}: {
+ productId?: string | null
+ name?: string | null
+ sku?: string | null
+ thumbnailUrl?: string | null
+}) {
+ const { storeId } = useStore()
+ const label = name ?? '—'
+
+ return (
+ <div className="flex items-center gap-3">
+ <Thumbnail src={thumbnailUrl} fallback={<PackageIcon />} />
+ <div className="min-w-0">
+ {productId ? (
+ <Link
+ to="/$storeId/products/$productId"
+ params={{ storeId, productId }}
+ className="font-medium hover:underline"
+ >
+ {label}
+ </Link>
+ ) : (
+ <span className="font-medium">{label}</span>
+ )}
+ {sku && <span className="block text-muted-foreground text-xs">{sku}</span>}
+ </div>
+ </div>
+ )
+}
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/products/purchase-orders/$purchaseOrderId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/products/purchase-orders/$purchaseOrderId.tsx
index 5e6128f2b7..34256670cb 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/products/purchase-orders/$purchaseOrderId.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/products/purchase-orders/$purchaseOrderId.tsx
@@ -1,5 +1,5 @@
import type { PurchaseOrder, PurchaseOrderItem } from '@spree/admin-sdk'
-import { Can, PageHeader, Subject } from '@spree/dashboard-core'
+import { Can, PageHeader, Subject, useStore } from '@spree/dashboard-core'
import {
Button,
Card,
@@ -16,11 +16,12 @@ import {
TableRow,
useConfirm,
} from '@spree/dashboard-ui'
-import { createFileRoute } from '@tanstack/react-router'
+import { createFileRoute, Link } from '@tanstack/react-router'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { InventoryStatusBadge } from '../../../../../components/spree/inventory-status-badge'
import { StockHistoryCard } from '../../../../../components/spree/stock-history-card'
+import { VariantLink } from '../../../../../components/spree/variant-link'
import {
useCancelPurchaseOrder,
useMarkPurchaseOrderOrdered,
@@ -65,6 +66,7 @@ function PurchaseOrderDetailPage() {
function SummaryCard({ purchaseOrder }: { purchaseOrder: PurchaseOrder }) {
const { t } = useTranslation()
+ const { storeId } = useStore()
return (
<Card>
@@ -74,10 +76,36 @@ function SummaryCard({ purchaseOrder }: { purchaseOrder: PurchaseOrder }) {
<CardContent>
<dl className="grid grid-cols-3 gap-y-2 text-sm">
<dt className="text-muted-foreground">{t('admin.purchase_orders.fields.supplier')}</dt>
- <dd className="col-span-2">{purchaseOrder.supplier?.name ?? '—'}</dd>
+ <dd className="col-span-2">
+ {purchaseOrder.supplier_id ? (
+ <Link
+ to="/$storeId/settings/suppliers"
+ params={{ storeId }}
+ search={{ edit: purchaseOrder.supplier_id }}
+ className="text-foreground hover:underline"
+ >
+ {purchaseOrder.supplier?.name ?? purchaseOrder.supplier_id}
+ </Link>
+ ) : (
+ '—'
+ )}
+ </dd>
<dt className="text-muted-foreground">{t('admin.purchase_orders.fields.destination')}</dt>
- <dd className="col-span-2">{purchaseOrder.destination_location?.name ?? '—'}</dd>
+ <dd className="col-span-2">
+ {purchaseOrder.destination_location_id ? (
+ <Link
+ to="/$storeId/settings/stock-locations"
+ params={{ storeId }}
+ search={{ edit: purchaseOrder.destination_location_id }}
+ className="text-foreground hover:underline"
+ >
+ {purchaseOrder.destination_location?.name ?? purchaseOrder.destination_location_id}
+ </Link>
+ ) : (
+ '—'
+ )}
+ </dd>
<dt className="text-muted-foreground">{t('admin.purchase_orders.fields.units')}</dt>
<dd className="col-span-2 tabular-nums">
@@ -239,9 +267,13 @@ function ItemRow({
return (
<TableRow>
- <TableCell className="font-medium">
- {item.variant_name ?? '—'}
- <span className="block text-xs text-muted-foreground">{item.variant_sku ?? ''}</span>
+ <TableCell>
+ <VariantLink
+ productId={item.product_id}
+ name={item.variant_name}
+ sku={item.variant_sku}
+ thumbnailUrl={item.thumbnail_url}
+ />
</TableCell>
<TableCell className="text-right tabular-nums">{item.quantity_ordered}</TableCell>
<TableCell className="text-right">
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/products/transfers/$transferId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/products/transfers/$transferId.tsx
index ccfaecea2d..d907d6b680 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/products/transfers/$transferId.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/products/transfers/$transferId.tsx
@@ -1,5 +1,5 @@
import type { StockTransfer, StockTransferItem } from '@spree/admin-sdk'
-import { Can, PageHeader, Subject, useStockLocations } from '@spree/dashboard-core'
+import { Can, PageHeader, Subject, useStockLocations, useStore } from '@spree/dashboard-core'
import {
Button,
Card,
@@ -23,11 +23,12 @@ import {
TableRow,
useConfirm,
} from '@spree/dashboard-ui'
-import { createFileRoute } from '@tanstack/react-router'
+import { createFileRoute, Link } from '@tanstack/react-router'
import { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { InventoryStatusBadge } from '../../../../../components/spree/inventory-status-badge'
import { StockHistoryCard } from '../../../../../components/spree/stock-history-card'
+import { VariantLink } from '../../../../../components/spree/variant-link'
import {
useCancelStockTransfer,
useMarkStockTransferInTransit,
@@ -84,8 +85,38 @@ function StockTransferDetailPage() {
)
}
+/**
+ * A warehouse, linked to the row that configures it. Stock locations have no
+ * screen of their own — they are edited from the settings list — so the link
+ * opens that list with this one's sheet showing, the way an order links its
+ * channel and its market.
+ */
+function WarehouseLink({
+ storeId,
+ id,
+ name,
+}: {
+ storeId: string
+ id: string | null
+ name: string
+}) {
+ if (!id) return <>{name}</>
+
+ return (
+ <Link
+ to="/$storeId/settings/stock-locations"
+ params={{ storeId }}
+ search={{ edit: id }}
+ className="text-foreground hover:underline"
+ >
+ {name}
+ </Link>
+ )
+}
+
function SummaryCard({ transfer }: { transfer: StockTransfer }) {
const { t } = useTranslation()
+ const { storeId } = useStore()
const { data: stockLocations } = useStockLocations({ limit: 100 })
const locationName = useMemo(() => {
@@ -102,12 +133,23 @@ function SummaryCard({ transfer }: { transfer: StockTransfer }) {
<dl className="grid grid-cols-3 gap-y-2 text-sm">
<dt className="text-muted-foreground">{t('admin.stock_transfers.fields.source')}</dt>
<dd className="col-span-2">
- {transfer.source_location?.name ?? locationName(transfer.source_location_id)}
+ <WarehouseLink
+ storeId={storeId}
+ id={transfer.source_location_id}
+ name={transfer.source_location?.name ?? locationName(transfer.source_location_id)}
+ />
</dd>
<dt className="text-muted-foreground">{t('admin.stock_transfers.fields.destination')}</dt>
<dd className="col-span-2">
- {transfer.destination_location?.name ?? locationName(transfer.destination_location_id)}
+ <WarehouseLink
+ storeId={storeId}
+ id={transfer.destination_location_id}
+ name={
+ transfer.destination_location?.name ??
+ locationName(transfer.destination_location_id)
+ }
+ />
</dd>
<dt className="text-muted-foreground">{t('admin.stock_transfers.fields.units')}</dt>
@@ -177,7 +219,13 @@ function PlannedItemsCard({ transfer }: { transfer: StockTransfer }) {
<TableBody>
{items.map((item) => (
<TableRow key={item.id}>
- <TableCell className="font-medium">{item.variant_name ?? '—'}</TableCell>
+ <TableCell>
+ <VariantLink
+ productId={item.product_id}
+ name={item.variant_name}
+ thumbnailUrl={item.thumbnail_url}
+ />
+ </TableCell>
<TableCell className="text-muted-foreground">
{item.variant_sku ?? '—'}
</TableCell>
@@ -228,7 +276,7 @@ function ReceiveCard({ transfer }: { transfer: StockTransfer }) {
return {
id: item.id,
quantity_received: received,
- // Only while the line is actually short. The select hides once the
+ // Only while the line is under-received. The select hides once the
// count is raised, but its state survives — and a fully received
// line carrying "damaged in transit" is simply wrong.
discrepancy_reason: received < item.quantity_shipped ? reason || undefined : undefined,
@@ -312,13 +360,17 @@ function ReceiveRow({
onReason: (value: string) => void
}) {
const { t } = useTranslation()
- const short = count < item.quantity_shipped
+ const underReceived = count < item.quantity_shipped
return (
<TableRow>
- <TableCell className="font-medium">
- {item.variant_name ?? '—'}
- <span className="block text-xs text-muted-foreground">{item.variant_sku ?? ''}</span>
+ <TableCell>
+ <VariantLink
+ productId={item.product_id}
+ name={item.variant_name}
+ sku={item.variant_sku}
+ thumbnailUrl={item.thumbnail_url}
+ />
</TableCell>
<TableCell className="text-right tabular-nums">{item.quantity_shipped}</TableCell>
<TableCell className="text-right">
@@ -339,7 +391,7 @@ function ReceiveRow({
)}
</TableCell>
<TableCell>
- {editable && short ? (
+ {editable && underReceived ? (
<Select value={reason} onValueChange={onReason}>
<SelectTrigger aria-label={t('admin.stock_transfers.columns.discrepancy')}>
<SelectValue placeholder={t('admin.stock_transfers.discrepancy_placeholder')}>
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/receivable_actions.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/receivable_actions.rb
index bbf6272472..4a626ff9a3 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/admin/receivable_actions.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/receivable_actions.rb
@@ -21,6 +21,14 @@ module Spree
protected
+ # What rendering a line actually reads: the variant, the product it
+ # belongs to — for the name and the link — and whichever image the
+ # thumbnail falls back to, with the blobs the URL helper needs.
+ def line_includes
+ media = { primary_media: [attachment_attachment: :blob, poster_attachment: :blob] }
+ { items: { variant: [media, { product: media }] } }
+ end
+
# The lines a create or update named, or nil when the payload said
# nothing about them — which on an update means "leave them alone".
#
diff --git a/spree/api/app/controllers/spree/api/v3/admin/purchase_orders_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/purchase_orders_controller.rb
index c659369c6f..3d4c2e81f1 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/purchase_orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/purchase_orders_controller.rb
@@ -79,7 +79,7 @@ module Spree
end
def collection_includes
- [:supplier, :destination_location, { items: :variant }]
+ [:supplier, :destination_location, line_includes]
end
def create_workflow
@@ -129,7 +129,7 @@ module Spree
end
def scope_includes
- [{ items: :variant }]
+ [line_includes]
end
private
diff --git a/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb
index 35be0a7726..157ac5b9be 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb
@@ -103,7 +103,7 @@ module Spree
end
def collection_includes
- [:source_location, :destination_location, { items: :variant }]
+ [:source_location, :destination_location, line_includes]
end
def create_workflow
@@ -156,7 +156,7 @@ module Spree
# The listing and the detail page both read a transfer's totals off
# its lines.
def scope_includes
- [{ items: :variant }]
+ [line_includes]
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/purchase_order_item_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/purchase_order_item_serializer.rb
index a911896f1b..597dbf7c19 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/purchase_order_item_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/purchase_order_item_serializer.rb
@@ -13,6 +13,8 @@ module Spree
display_total_cost: :string,
currency: 'string | null',
purchase_order_id: 'string | null',
+ thumbnail_url: 'string | null',
+ product_id: 'string | null',
variant_id: 'string | null',
variant_name: 'string | null',
variant_sku: 'string | null',
@@ -47,6 +49,12 @@ module Spree
item.variant&.prefixed_id
end
+ # The product the line's variant belongs to, so a line can link
+ # straight to the screen where that SKU's stock lives.
+ attribute :product_id do |item|
+ item.variant&.product&.prefixed_id
+ end
+
attribute :variant_name do |item|
item.variant&.product&.name
end
@@ -58,6 +66,10 @@ module Spree
attribute :options_text do |item|
item.variant&.options_text
end
+
+ attribute :thumbnail_url do |item|
+ image_url_for(item.thumbnail)
+ end
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/stock_transfer_item_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/stock_transfer_item_serializer.rb
index 8fae76499e..dabc28dfc9 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/stock_transfer_item_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/stock_transfer_item_serializer.rb
@@ -10,6 +10,8 @@ module Spree
outstanding: :number,
discrepancy_reason: 'string | null',
stock_transfer_id: 'string | null',
+ thumbnail_url: 'string | null',
+ product_id: 'string | null',
variant_id: 'string | null',
variant_name: 'string | null',
variant_sku: 'string | null',
@@ -26,6 +28,12 @@ module Spree
item.variant&.prefixed_id
end
+ # The product the line's variant belongs to, so a line can link
+ # straight to the screen where that SKU's stock lives.
+ attribute :product_id do |item|
+ item.variant&.product&.prefixed_id
+ end
+
attribute :variant_name do |item|
item.variant&.product&.name
end
@@ -37,6 +45,10 @@ module Spree
attribute :options_text do |item|
item.variant&.options_text
end
+
+ attribute :thumbnail_url do |item|
+ image_url_for(item.thumbnail)
+ end
end
end
end
diff --git a/spree/core/app/models/concerns/spree/receivable_item.rb b/spree/core/app/models/concerns/spree/receivable_item.rb
index a47e38c519..8f8e9ca54f 100644
--- a/spree/core/app/models/concerns/spree/receivable_item.rb
+++ b/spree/core/app/models/concerns/spree/receivable_item.rb
@@ -39,6 +39,14 @@ module Spree
public_send(self.class.expected_quantity_attribute).to_i
end
+ # The image for this line: the variant's own, or its product's when the
+ # variant has none — the same fallback a line item on an order uses.
+ #
+ # @return [Spree::Media, nil]
+ def thumbnail
+ variant&.primary_media || variant&.product&.primary_media
+ end
+
# How many are still owed.
#
# @return [Integer]
diff --git a/spree/core/spec/models/spree/purchase_order_spec.rb b/spree/core/spec/models/spree/purchase_order_spec.rb
index 5fda92d2ce..4f96fcc8ab 100644
--- a/spree/core/spec/models/spree/purchase_order_spec.rb
+++ b/spree/core/spec/models/spree/purchase_order_spec.rb
@@ -67,6 +67,18 @@ module Spree
expect(item).to be_under_received
end
+ # A line shows the variant's own image, and the product's when the
+ # variant has none — the fallback an order's line item already uses.
+ it 'shows the variant image, or the product image when there is none' do
+ product_image = create(:image, viewable: item.variant.product)
+
+ expect(item.reload.thumbnail).to eq(product_image)
+
+ variant_image = create(:image, viewable: item.variant)
+
+ expect(item.reload.thumbnail).to eq(variant_image)
+ end
+
it 'costs the whole line at the agreed unit price' do
expect(item.total_cost).to eq(125)
expect(item.display_unit_cost.to_s).to eq('$12.50')