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
|
|
@ -16,6 +16,12 @@ routerAdd('GET', '/api/shop/slots', (e) => {
|
|||
return e.json(200, shop.bookableTimes($app, { item: query.get('item'), minutes: query.get('minutes'), from: query.get('from'), to: query.get('to') }));
|
||||
});
|
||||
|
||||
// Best sellers first (ids only), for the Shop block's "Most popular" sort.
|
||||
routerAdd('GET', '/api/shop/popular', (e) => {
|
||||
const shop = require(`${__hooks}/shop/lib.js`);
|
||||
return e.json(200, { items: shop.popularItems($app) });
|
||||
});
|
||||
|
||||
routerAdd('GET', '/api/shop/basket/{id}', (e) => {
|
||||
const shop = require(`${__hooks}/shop/lib.js`);
|
||||
shop.sweepExpiredHolds($app);
|
||||
|
|
|
|||
|
|
@ -1305,6 +1305,33 @@ function createOrder(app, basketId, payment) {
|
|||
return orderId;
|
||||
}
|
||||
|
||||
// ── Popularity ──────────────────────────────────────────────────────────
|
||||
|
||||
/** "Most popular" in the Shop block counts what sold in this many days. */
|
||||
const POPULAR_DAYS = 90;
|
||||
|
||||
/**
|
||||
* Item ids, best-selling first, by units in paid orders over the last
|
||||
* POPULAR_DAYS (refunded orders don't count). A booking paid with a code
|
||||
* counts for its service; products handed over inside a bundle (price 0, no
|
||||
* code) don't — the bundle's own line counts for it. Only the order is
|
||||
* returned, never the numbers: this is public, and sales figures aren't.
|
||||
*/
|
||||
function popularItems(app) {
|
||||
const rows = arrayOf(new DynamicModel({ item: '', sold: 0 }));
|
||||
app
|
||||
.db()
|
||||
.newQuery(
|
||||
`SELECT ol.item AS item, SUM(ol.quantity) AS sold
|
||||
FROM order_lines ol JOIN orders o ON o.id = ol.[[order]]
|
||||
WHERE o.status = 'paid' AND o.created >= {:since} AND (ol.price > 0 OR ol.pass != '')
|
||||
GROUP BY ol.item HAVING sold > 0 ORDER BY sold DESC, ol.item`
|
||||
)
|
||||
.bind({ since: pbDate(Date.now() - POPULAR_DAYS * DAY_MS) })
|
||||
.all(rows);
|
||||
return rows.map((row) => row.item);
|
||||
}
|
||||
|
||||
// ── Orders ──────────────────────────────────────────────────────────────
|
||||
|
||||
function globalData(app, key) {
|
||||
|
|
@ -1409,6 +1436,7 @@ module.exports = {
|
|||
issuePass,
|
||||
bookableTimes,
|
||||
slotDetails,
|
||||
popularItems,
|
||||
sweepExpiredHolds,
|
||||
loadActiveBasket,
|
||||
basketPayload,
|
||||
|
|
|
|||
29
pocketbase/pb_migrations/1793500000_item_priority.js
Normal file
29
pocketbase/pb_migrations/1793500000_item_priority.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
// items.priority: where an item comes in Shop blocks under "Recommended"
|
||||
// (the default order). Higher comes first; equal priorities go by name.
|
||||
// Empty reads back as 0, so every existing item starts at 0 and the shop
|
||||
// keeps its old alphabetical order until priorities are set.
|
||||
|
||||
function find(app, name) {
|
||||
try {
|
||||
return app.findCollectionByNameOrId(name);
|
||||
} catch (e) {
|
||||
return null; // fresh install: pb_schema.json (already with the field) is imported later
|
||||
}
|
||||
}
|
||||
|
||||
migrate(
|
||||
(app) => {
|
||||
const items = find(app, 'items');
|
||||
if (!items || items.fields.getByName('priority')) return;
|
||||
items.fields.add(new NumberField({ id: 'num_itempriority', name: 'priority', min: -1000, max: 1000, onlyInt: true }));
|
||||
app.save(items);
|
||||
},
|
||||
(app) => {
|
||||
const items = find(app, 'items');
|
||||
if (!items || !items.fields.getByName('priority')) return;
|
||||
items.fields.removeByName('priority');
|
||||
app.save(items);
|
||||
}
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue