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:
parent
97b1da2433
commit
e896fd3c99
14 changed files with 211 additions and 19 deletions
|
|
@ -2,7 +2,7 @@
|
|||
// the Shop block and the checkout. Which duration costs what is decided
|
||||
// (and charged) by the shop hooks in PocketBase; this only reads and shows it.
|
||||
|
||||
import type { BundlePart, ItemDuration, ItemRecord } from './types';
|
||||
import type { BundlePart, ItemDuration, ItemRecord, ShopSort } from './types';
|
||||
|
||||
/** A service's `durations` field (JSON, possibly hand-edited) → valid entries, shortest first, one per length. */
|
||||
export function parseDurations(raw: unknown): ItemDuration[] {
|
||||
|
|
@ -49,3 +49,37 @@ export function bundleListPrice(parts: BundlePart[], items: ItemRecord[]) {
|
|||
}
|
||||
return Math.round(total * 100) / 100;
|
||||
}
|
||||
|
||||
/** The Shop block's sort choices, in menu order, with what visitors see. */
|
||||
export const SHOP_SORTS: { value: ShopSort; label: string }[] = [
|
||||
{ value: 'recommended', label: 'Recommended' },
|
||||
{ value: 'popular', label: 'Most popular' },
|
||||
{ value: 'price_asc', label: 'Price: low to high' },
|
||||
{ value: 'price_desc', label: 'Price: high to low' },
|
||||
{ value: 'name', label: 'Name (A–Z)' }
|
||||
];
|
||||
|
||||
const byName = (a: ItemRecord, b: ItemRecord) => a.name.localeCompare(b.name, 'en-GB', { sensitivity: 'base' });
|
||||
const byPriority = (a: ItemRecord, b: ItemRecord) => b.priority - a.priority || byName(a, b);
|
||||
|
||||
/**
|
||||
* `items` in the Shop block's `sort` order (a new array). Ties — and, for
|
||||
* "Most popular", items that haven't sold lately — fall back to Recommended,
|
||||
* so the admin's priorities still decide whatever the numbers don't.
|
||||
* Services sort by their lowest price (what "from …" shows).
|
||||
*/
|
||||
export function sortItems(items: ItemRecord[], sort: ShopSort = 'recommended') {
|
||||
const list = [...items];
|
||||
switch (sort) {
|
||||
case 'popular':
|
||||
return list.sort((a, b) => (a.popularRank ?? Infinity) - (b.popularRank ?? Infinity) || byPriority(a, b));
|
||||
case 'price_asc':
|
||||
return list.sort((a, b) => a.price - b.price || byPriority(a, b));
|
||||
case 'price_desc':
|
||||
return list.sort((a, b) => b.price - a.price || byPriority(a, b));
|
||||
case 'name':
|
||||
return list.sort(byName);
|
||||
default:
|
||||
return list.sort(byPriority);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue