Shop pack: a product catalog with checkout links #12

Merged
jcoffey-dev merged 1 commits from packs/shop into main 2026-10-12 00:30:45 +00:00
14 changed files with 252 additions and 4 deletions

No files matched your search

+5 -3
View File
@@ -50,14 +50,16 @@ Content-Security-Policy as the site's own.
tags with it;
- **share-links**: plain share links for Mastodon, Bluesky, LinkedIn and
email, with no tracking;
- **shop**: a product catalog with a page per product, each Buy button
opening your payment provider's checkout (no cart);
- **table-of-contents**: a long page's headings, linked, at its top;
- **team**: a page per person, a list of everyone and a section;
- **testimonials**: quotes with a name and a role.
- `-site` picks the site folder (default: the current one).
**Search engines.** The faq, location-and-hours, team, recipes and jobs packs
are also written as schema.org data (FAQPage, LocalBusiness, Person, Recipe,
JobPosting), which search engines use for richer results. A pack can't carry
**Search engines.** The faq, location-and-hours, team, recipes, jobs and shop
packs are also written as schema.org data (FAQPage, LocalBusiness, Person,
Recipe, JobPosting, Product), which search engines use for richer results. A pack can't carry
the `<script type="application/ld+json">` that holds it, so HotDog CMS
builds it (`pageData` in templates) and the starter's `partials/schema.html`
writes it. Sites started before HotDog CMS 0.2 can add the same two lines to
+12
View File
@@ -344,3 +344,15 @@ func TestHeadingsAndMinutes(t *testing.T) {
}
}
}
func TestMoney(t *testing.T) {
for _, c := range []struct {
v any
cur string
want string
}{{24, "USD", "$24.00"}, {24.5, "eur", "€24.50"}, {"1200", "JPY", "¥1200"}, {9, "SEK", "9.00 SEK"}, {7, "", "$7.00"}, {"Ask us", "USD", "Ask us"}} {
if got := money(c.v, c.cur); got != c.want {
t.Errorf("money(%v, %q) = %q, want %q", c.v, c.cur, got, c.want)
}
}
}
+2
View File
@@ -260,6 +260,8 @@ func funcs(s *site.Site, a *Assets, md *site.Markdown, icons Icons) template.Fun
// pageData: schema.org objects for what packs put on the page (an
// FAQ, a business, a person, a recipe, a job), for search engines.
"pageData": func(p *site.Page) []map[string]any { return pageData(s, p) },
// money 24 "USD" -> "$24.00": a price as people read it.
"money": money,
// closed "2026-11-01": whether that day is over, in the site's time
// zone, when the site is built (a job's closing date, say).
"closed": func(v any) bool { return closed(s, v) },
+74
View File
@@ -46,6 +46,8 @@ func pageData(s *site.Site, p *site.Page) []map[string]any {
out = append(out, personData(s, p))
case "recipe":
out = append(out, recipeData(s, p))
case "product":
out = append(out, productData(s, p))
case "job":
// A closed opening isn't advertised: search engines drop listings
// whose validThrough has passed, and say so.
@@ -423,3 +425,75 @@ func headings(content template.HTML) []Heading {
}
}
}
// amount reads a price written as a number (24, 24.5, "24.50").
func amount(v any) (float64, bool) {
switch t := v.(type) {
case int:
return float64(t), true
case float64:
return t, true
case string:
f, err := strconv.ParseFloat(strings.TrimSpace(strings.TrimLeft(t, "$€£¥")), 64)
return f, err == nil
}
return 0, false
}
var currencySigns = map[string]string{"USD": "$", "CAD": "CA$", "AUD": "A$", "NZD": "NZ$", "EUR": "€", "GBP": "£", "JPY": "¥", "INR": "₹", "MXN": "MX$", "BRL": "R$"}
// money writes a price the way people read it: 24 "USD" is $24.00, 24 "SEK"
// is 24.00 SEK. Whole yen have no decimals.
func money(v any, currency string) string {
f, ok := amount(v)
if !ok {
return str(v)
}
cur := strings.ToUpper(strings.TrimSpace(currency))
if cur == "" {
cur = "USD"
}
n := strconv.FormatFloat(f, 'f', 2, 64)
if cur == "JPY" {
n = strconv.FormatFloat(f, 'f', 0, 64)
}
if sign, ok := currencySigns[cur]; ok {
return sign + n
}
return n + " " + cur
}
func productData(s *site.Site, p *site.Page) map[string]any {
out := ldBase("Product")
out["name"] = p.Title
out["url"] = p.Permalink
if p.Summary != "" {
out["description"] = p.Summary
}
var images []string
for _, ph := range list(p.Params["photos"]) {
m, _ := ph.(map[string]any)
if im := str(m["image"]); im != "" {
images = append(images, abs(s, im))
}
}
if len(images) > 0 {
out["image"] = images
}
if v := str(p.Params["sku"]); v != "" {
out["sku"] = v
}
if f, ok := amount(p.Params["price"]); ok {
cur := strings.ToUpper(str(p.Params["currency"]))
if cur == "" {
cur = "USD"
}
avail := "https://schema.org/InStock"
if str(p.Params["sold_out"]) == "true" {
avail = "https://schema.org/OutOfStock"
}
out["offers"] = map[string]any{"@type": "Offer", "price": strconv.FormatFloat(f, 'f', 2, 64), "priceCurrency": cur, "availability": avail, "url": p.Permalink}
}
out["brand"] = map[string]any{"@type": "Brand", "name": s.Config.Name}
return out
}
+9
View File
@@ -154,6 +154,15 @@ func TestBundledPacksBuildAndPass(t *testing.T) {
}, map[string][]string{
"shop/index.html": {`href="https://buy.stripe.com/test_123" rel="noopener">Buy now`, `alt="A gift card"`, "Support the bakery", `href="https://liberapay.com/bakery" rel="noopener">Donate`, ">$5<"},
}},
{"shop", []page{
{"content/products/_index.md", "---\ntitle: Shop\nsummary: Things to buy.\n---\n"},
{"content/products/kit.md", "---\ntitle: Starter kit\nsummary: A jar of starter.\nprice: 24\ncurrency: USD\nweight: 1\nphotos:\n - { image: /media/a.png, alt: A jar }\n - { image: /media/b.png, alt: The card }\nbuy: \"https://buy.stripe.com/test_1\"\noptions:\n - { name: With a basket, price: 39.5, buy: \"https://buy.stripe.com/test_2\" }\n---\nWhat's in the box.\n"},
{"content/products/mug.md", "---\ntitle: Mug\nsummary: A mug.\nprice: 12\ncurrency: EUR\nweight: 2\nsold_out: true\n---\n"},
}, map[string][]string{
"products/index.html": {`href="/products/kit/"`, "$24.00", "€12.00 · <em>Sold out</em>"},
"products/kit/index.html": {`href="https://buy.stripe.com/test_1" rel="noopener">Buy`, "With a basket · $39.50", `alt="The card"`, `"@type":"Product"`, `"priceCurrency":"USD"`, `"price":"24.00"`, "InStock"},
"products/mug/index.html": {"Sold out", "OutOfStock"},
}},
}
for _, c := range cases {
t.Run(c.pack, func(t *testing.T) {
+1 -1
View File
@@ -5,5 +5,5 @@ package packs
import "embed"
//go:embed all:calendar all:docs all:downloads all:faq all:gallery all:jobs all:location-and-hours all:menu-and-prices all:payment-links all:podcast all:portfolio all:recipes all:related-posts all:share-links all:table-of-contents all:team all:testimonials
//go:embed all:calendar all:docs all:downloads all:faq all:gallery all:jobs all:location-and-hours all:menu-and-prices all:payment-links all:podcast all:portfolio all:recipes all:related-posts all:share-links all:shop all:table-of-contents all:team all:testimonials
var FS embed.FS
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Coffey Labs LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+15
View File
@@ -0,0 +1,15 @@
# Shop pack
A small online shop for a HotDog CMS site: a page for each product with its
photos, price, description and options, a shop page at /products/, and a
"Products" section for any page. Each Buy button opens your payment
provider's own checkout (a Stripe Payment Link, a PayPal or Square checkout
link), which takes the payment, the address, any tax and sends the receipt.
hotdog-cms pack add shop
There's no cart: each product (or option) has its own checkout, which suits
a shop with a handful of things to sell. Card details never touch your site,
and nothing from the provider loads until someone chooses Buy. Products are
also written as schema.org Product data, with the price and whether they're
in stock, for search engines.
+17
View File
@@ -0,0 +1,17 @@
/* The shop pack. Colors come from the theme's variables. */
.shop-grid { list-style: none; padding: 0; margin: 0 0 20px; display: grid; gap: 22px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.shop-card a { display: grid; gap: 10px; color: var(--text); text-decoration: none; }
.shop-card a:hover strong { color: var(--accent); }
.shop-photo { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: var(--radius); background: var(--accent-soft); display: block; }
.shop-price { color: var(--muted); }
.product-main { display: grid; gap: 32px; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); align-items: start; margin-bottom: 28px; }
@media (max-width: 760px) { .product-main { grid-template-columns: minmax(0, 1fr); } }
.product-photos { display: grid; gap: 10px; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.product-photo-main { grid-column: 1 / -1; width: 100%; border-radius: var(--radius); }
.product-photo { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: calc(var(--radius) - 4px); }
.product-buy h1 { margin: 0 0 6px; }
.product-price { font-size: 1.6rem; font-weight: 750; margin: 0 0 12px; }
.product-summary { color: var(--muted); margin: 0 0 18px; }
.product-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.product-secure { margin: 12px 0 0; color: var(--muted); font-size: .88rem; }
.product-sold-out { display: inline-block; padding: 8px 14px; border-radius: var(--radius); background: var(--accent-soft); font-weight: 650; }
+27
View File
@@ -0,0 +1,27 @@
{{ define "main" }}
<link rel="stylesheet" href="{{ asset "packs/shop/shop.css" }}">
{{ $soldOut := eq (printf "%v" .Params.sold_out) "true" }}{{ $cur := .Params.currency }}
<article class="wrap page product">
{{ with index .Site.Lists .Collection }}<p class="eyebrow"><a href="{{ .Path }}">{{ .Title }}</a></p>{{ end }}
<div class="product-main">
<div class="product-photos">
{{ range $i, $p := .Params.photos }}<img class="{{ if eq $i 0 }}product-photo-main{{ else }}product-photo{{ end }}" src="{{ $p.image }}" alt="{{ $p.alt }}"{{ if gt $i 0 }} loading="lazy"{{ end }}>{{ end }}
</div>
<div class="product-buy">
<h1>{{ .Title }}</h1>
{{ with .Params.price }}<p class="product-price">{{ money . $cur }}</p>{{ end }}
{{ with .Summary }}<p class="product-summary">{{ . }}</p>{{ end }}
{{ if $soldOut }}
<p class="product-sold-out">Sold out</p>
{{ else }}
<div class="product-actions">
{{ with .Params.buy }}<a class="btn primary" href="{{ . }}" rel="noopener">{{ default "Buy" $.Params.button }}</a>{{ end }}
{{ range .Params.options }}<a class="btn" href="{{ .buy }}" rel="noopener">{{ .name }}{{ with .price }} · {{ money . $cur }}{{ end }}</a>{{ end }}
</div>
<p class="product-secure">You'll pay on our payment provider's secure page.</p>
{{ end }}
</div>
</div>
{{ with .Content }}<div class="prose product-details">{{ . }}</div>{{ end }}
</article>
{{ end }}
+9
View File
@@ -0,0 +1,9 @@
{{ define "main" }}
<link rel="stylesheet" href="{{ asset "packs/shop/shop.css" }}">
<section class="wrap page shop">
<h1>{{ .Title }}</h1>
{{ with .Content }}<div class="prose">{{ . }}</div>{{ end }}
<ul class="shop-grid">{{ range .Paginator.Items }}{{ template "product-card" . }}{{ end }}</ul>
{{ template "pagination" . }}
</section>
{{ end }}
+33
View File
@@ -0,0 +1,33 @@
name: shop
version: 1.0.0
title: Shop
description: A product catalog with a page per product (photos, price, options), where each Buy button opens your payment provider's checkout. No cart, no card details on your site, no script.
license: MIT
author: Coffey Labs LLC
collections:
products:
layout: product
list_layout: shop
sort: weight
notes: |
Make a payment link for each product in your provider's dashboard (Stripe
Payment Links can ask for a quantity, a shipping address and sales tax),
then add each product as content/products/<name>.md:
---
title: Sourdough starter kit
summary: Our 20-year-old starter, a jar and the instructions.
price: 24
currency: USD
weight: 1
photos:
- { image: /media/shop/kit.jpg, alt: A jar of starter beside a recipe card }
buy: https://buy.stripe.com/your-link
options:
- { name: Kit and a proving basket, price: 39, buy: "https://buy.stripe.com/another-link" }
---
What's in the box, how it ships, and anything else, in Markdown.
sold_out: true shows Sold out instead of the buttons. weight sets the
order (lowest first). /products/ lists them; the "Products" section shows
some on any page. Mention your payment provider in your privacy notice.
+8
View File
@@ -0,0 +1,8 @@
{{/* One product in a grid: its first photo, name and price. */}}
<li class="shop-card">
<a href="{{ .Href }}">
{{ with .Params.photos }}{{ with index . 0 }}<img class="shop-photo" src="{{ .image }}" alt="{{ .alt }}" loading="lazy">{{ end }}{{ else }}<span class="shop-photo shop-none" aria-hidden="true"></span>{{ end }}
<strong>{{ .Title }}</strong>
</a>
<span class="shop-price">{{ with .Params.price }}{{ money . $.Params.currency }}{{ end }}{{ if eq (printf "%v" .Params.sold_out) "true" }} · <em>Sold out</em>{{ end }}</span>
</li>
+19
View File
@@ -0,0 +1,19 @@
{{/* section
label: Products
about: Products from the shop, with their photos and prices.
fields:
- { name: heading, type: text, default: Shop }
- { name: intro, type: markdown }
- { name: collection, type: collection, default: products, label: Products from }
- { name: count, type: number, default: 4, label: How many }
*/}}
{{ $c := default "products" .Data.collection }}
<link rel="stylesheet" href="{{ asset "packs/shop/shop.css" }}">
<section class="block shop">
<div class="wrap">
{{ with .Data.heading }}<h2>{{ . }}</h2>{{ end }}
{{ with .Data.intro }}<div class="intro">{{ markdownify . }}</div>{{ end }}
<ul class="shop-grid">{{ range limit (default 4 .Data.count) (collection $c) }}{{ template "product-card" . }}{{ end }}</ul>
<p><a class="more" href="/{{ $c }}/">Everything in the shop</a></p>
</div>
</section>