Shop: item priority, block sort order and an optional "Sort by" menu

- items.priority (migration + pb_schema.json): Recommended order in Shop
  blocks, higher first, then by name. Set under Services & products, which
  now lists items in that order.
- Shop block: choose its order (Recommended, Most popular, price up/down,
  name) and optionally let visitors re-sort with a "Sort by" menu.
  sortItems() in shared/items.ts is the one place items are ordered.
- Most popular: GET /api/shop/popular returns item ids by units sold in
  paid orders over the last 90 days (ids only, no sales figures).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gWt7pNdAsocjxujY3m
This commit is contained in:
agent 2026-11-04 22:36:50 +00:00
parent 97b1da2433
commit e896fd3c99
14 changed files with 211 additions and 19 deletions

View file

@ -73,7 +73,9 @@ export function createBlock(type: BlockType): ContentBlock {
title: 'Our Services',
itemTypeFilter: 'all',
itemIds: [],
layout: 'grid'
layout: 'grid',
sort: 'recommended',
showSort: false
};
break;
case 'checkout':

View file

@ -1,5 +1,6 @@
<script lang="ts">
import type { ContentBlock, ShopBlockData } from '$lib/types';
import type { ContentBlock, ShopBlockData, ShopSort } from '$lib/types';
import { SHOP_SORTS } from '$shared/items';
import SegmentedControl from './SegmentedControl.svelte';
let {
@ -55,11 +56,28 @@
formatLabel={(v) => typeFilterLabels[String(v)] || String(v)}
/>
<label class="fieldset">
<span class="fieldset-legend">Order</span>
<select class="select w-full" value={block.data.sort || 'recommended'} onchange={(e) => updateData({ sort: e.currentTarget.value as ShopSort })}>
{#each SHOP_SORTS as choice (choice.value)}
<option value={choice.value}>{choice.label}</option>
{/each}
</select>
<span class="label whitespace-normal">
Recommended follows each item’s priority (Services & products). Most popular counts what sold in the last 90 days; items without recent sales come after, in Recommended order.
</span>
</label>
<label class="label cursor-pointer gap-3 whitespace-normal text-base-content">
<input class="toggle toggle-primary shrink-0" type="checkbox" checked={!!block.data.showSort} onchange={(e) => updateData({ showSort: e.currentTarget.checked })} />
<span>Let visitors sort <span class="text-base-content/60">— a “Sort by” menu above the items, starting at the order chosen above</span></span>
</label>
{#if items.length}
<div class="rounded-box border border-base-300 p-4">
<div class="mb-3">
<h3 class="font-semibold">Items</h3>
<p class="text-sm text-base-content/60">Leave empty to show all items of the selected type.</p>
<p class="text-sm text-base-content/60">Leave empty to show all items of the selected type. Chosen items are shown in the order above, not the order you pick them.</p>
</div>
<div class="flex flex-wrap gap-2">
{#each items as item}

View file

@ -28,6 +28,9 @@ function parseDurationRows(data: FormData): ItemDuration[] | { error: string } {
return durations.sort((a, b) => a.minutes - b.minutes);
}
/** Priorities go from -this to this (the field's limits in pb_schema.json). */
const MAX_PRIORITY = 1000;
/** Most units of one thing a bundle can hold. */
const MAX_PART_COUNT = 50;
@ -66,6 +69,9 @@ function itemPayload(data: FormData, items: Record<string, any>[]) {
const rawStock = String(data.get('stock') ?? '').trim();
const rawBuffer = String(data.get('bufferMinutes') ?? '').trim();
const rawCapacity = String(data.get('capacity') ?? '').trim();
const rawPriority = String(data.get('priority') ?? '').trim();
const priority = rawPriority === '' ? 0 : Number(rawPriority);
if (!Number.isInteger(priority) || Math.abs(priority) > MAX_PRIORITY) return { error: `Priority: a whole number from -${MAX_PRIORITY} to ${MAX_PRIORITY} (0 = normal).` };
let price: number;
let durations: ItemDuration[] = [];
@ -110,6 +116,7 @@ function itemPayload(data: FormData, items: Record<string, any>[]) {
validMonths,
bufferMinutes: Number.isNaN(bufferMinutes) ? null : bufferMinutes,
capacity: Number.isNaN(capacity) ? null : capacity,
priority,
active: String(data.get('active')) === 'true'
}
};
@ -124,7 +131,7 @@ export const load: PageServerLoad = async ({ locals }) => {
const [records, waiting] = await Promise.all([
locals.pb
.collection('items')
.getFullList({ sort: 'name', expand: 'image' })
.getFullList({ sort: '-priority,name', expand: 'image' })
.catch(() => []),
codesWaiting(locals.pb).catch(() => ({}) as Record<string, number>)
]);

View file

@ -26,6 +26,8 @@
validMonths: string;
bufferMinutes: string;
capacity: string;
/** Shop order under Recommended, as typed ('' = 0). */
priority: string;
active: boolean;
};
@ -49,6 +51,7 @@
validMonths: '12',
bufferMinutes: '',
capacity: '',
priority: '',
active: true
};
}
@ -118,6 +121,7 @@
validMonths: item.type === 'bundle' ? (item.validMonths == null ? '' : String(item.validMonths)) : '12',
bufferMinutes: item.bufferMinutes == null ? '' : String(item.bufferMinutes),
capacity: item.capacity == null ? '' : String(item.capacity),
priority: item.priority ? String(item.priority) : '',
active: item.active
};
showEditor = true;
@ -130,7 +134,7 @@
<div class="flex flex-col justify-between gap-4 md:flex-row md:items-end">
<div>
<h1 class="text-3xl font-bold">Services & products</h1>
<p class="text-base-content/60">Bookable services, products and bundles (codes for sessions booked later), shown in Shop blocks.</p>
<p class="text-base-content/60">Bookable services, products and bundles (codes for sessions booked later), shown in Shop blocks — in this order, highest priority first.</p>
</div>
<button class="btn btn-primary" type="button" onclick={() => createItem()}>Add service or product</button>
</div>
@ -145,6 +149,10 @@
{/if}
{/snippet}
{#snippet priorityNote(item: ItemRecord)}
{#if item.priority}<span class="block text-xs text-base-content/60">Priority {item.priority}</span>{/if}
{/snippet}
{#snippet typeBadge(item: ItemRecord)}
<span class="badge">{item.type === 'item_with_time' ? 'Service' : item.type === 'bundle' ? 'Bundle' : 'Product'}</span>
{/snippet}
@ -186,6 +194,7 @@
{@render thumbnail(item)}
<div class="min-w-0 flex-1">
<span class="block font-semibold break-words">{item.name}</span>
{@render priorityNote(item)}
<div class="mt-1 flex flex-wrap items-center gap-2 text-sm">
{@render typeBadge(item)}
{@render status(item, problem)}
@ -216,7 +225,10 @@
<td>
<div class="flex items-center gap-3">
{@render thumbnail(item)}
<span class="font-semibold">{item.name}</span>
<div>
<span class="font-semibold">{item.name}</span>
{@render priorityNote(item)}
</div>
</div>
</td>
<td>{@render typeBadge(item)}</td>
@ -360,6 +372,12 @@
</ImagePicker>
</div>
<label class="fieldset">
<span class="fieldset-legend">Priority in the shop</span>
<input class="input w-full md:w-40" type="number" min="-1000" max="1000" step="1" name="priority" placeholder="0" bind:value={draft.priority} />
<span class="label whitespace-normal">Shop blocks show higher numbers first (their Recommended order); equal ones go by name. Leave at 0 for normal, use a negative number to push something to the end.</span>
</label>
<label class="label mt-2 cursor-pointer gap-3 whitespace-normal text-base-content">
<input class="toggle toggle-success shrink-0" type="checkbox" bind:checked={draft.active} />
<span>Active <span class="text-base-content/60">— inactive items are hidden from the shop</span></span>