create-abcart-theme 0.10.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -127
- package/LICENSE +202 -202
- package/NOTICE +2 -2
- package/README.md +65 -65
- package/bin/create-abcart-theme.mjs +4 -4
- package/package.json +49 -49
- package/src/args.mjs +113 -113
- package/src/banner.mjs +21 -21
- package/src/cli.mjs +119 -119
- package/src/create.mjs +85 -82
- package/src/package-manager.mjs +32 -32
- package/src/style.mjs +17 -17
- package/templates/default/.agents/skills/{wocart-docs → abcart-docs}/SKILL.md +146 -146
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/SKILL.md +58 -58
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/inputs-and-rights.md +47 -47
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/layout-rtl-and-phone.md +36 -36
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/measure-the-design.md +60 -60
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/tokens-sections-content.md +103 -103
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/verify-and-report.md +102 -102
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/compare-design.cjs +230 -230
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/qa-pages.cjs +219 -219
- package/templates/default/.agents/skills/shopify-theme-asset-collector/SKILL.md +125 -125
- package/templates/default/.agents/skills/shopify-theme-asset-collector/scripts/collect_public_assets.py +370 -370
- package/templates/default/.dev.vars.example +12 -12
- package/templates/default/AGENTS.md +240 -240
- package/templates/default/README.md +184 -184
- package/templates/default/_gitignore +11 -11
- package/templates/default/astro.config.mjs +85 -85
- package/templates/default/package.json +36 -36
- package/templates/default/public/fonts/OFL.txt +96 -96
- package/templates/default/src/actions/index.ts +22 -22
- package/templates/default/src/components/cart/AddToCartButton.astro +45 -45
- package/templates/default/src/components/cart/CartDrawer.astro +47 -47
- package/templates/default/src/components/cart/CartSummary.astro +11 -11
- package/templates/default/src/components/cart/FreeShipping.astro +7 -7
- package/templates/default/src/components/checkout/CheckoutMain.astro +29 -29
- package/templates/default/src/components/checkout/OrderForm.astro +280 -280
- package/templates/default/src/components/checkout/ThankYouMain.astro +68 -68
- package/templates/default/src/components/landing/LandingImages.astro +51 -51
- package/templates/default/src/components/landing/LandingOrder.astro +46 -46
- package/templates/default/src/components/landing/LandingProduct.astro +41 -41
- package/templates/default/src/components/listing/ListingMain.astro +93 -93
- package/templates/default/src/components/listing/PageMain.astro +52 -52
- package/templates/default/src/components/listing/Pagination.astro +31 -31
- package/templates/default/src/components/listing/SortControl.astro +35 -35
- package/templates/default/src/components/product/BuyBox.astro +96 -96
- package/templates/default/src/components/product/Description.astro +24 -24
- package/templates/default/src/components/product/Gallery.astro +66 -66
- package/templates/default/src/components/product/MobileBuyBar.astro +29 -29
- package/templates/default/src/components/product/OfferChoice.astro +66 -66
- package/templates/default/src/components/product/ProductMain.astro +40 -40
- package/templates/default/src/components/product/ProductReviews.astro +72 -72
- package/templates/default/src/components/product/RelatedProducts.astro +30 -30
- package/templates/default/src/components/product/blocks/BenefitsBlock.astro +13 -13
- package/templates/default/src/components/product/blocks/CollapsibleBlock.astro +20 -20
- package/templates/default/src/components/product/blocks/DescriptionBlock.astro +19 -19
- package/templates/default/src/components/product/blocks/DetailsBlock.astro +33 -33
- package/templates/default/src/components/product/blocks/GalleryBlock.astro +12 -12
- package/templates/default/src/components/product/blocks/OrderBlock.astro +30 -30
- package/templates/default/src/components/product/blocks/PriceBlock.astro +23 -23
- package/templates/default/src/components/product/blocks/RatingBlock.astro +16 -16
- package/templates/default/src/components/product/blocks/StockBlock.astro +22 -22
- package/templates/default/src/components/product/blocks/TextBlock.astro +11 -11
- package/templates/default/src/components/product/blocks/TitleBlock.astro +20 -20
- package/templates/default/src/components/reviews/ReviewCard.astro +46 -46
- package/templates/default/src/components/reviews/ReviewForm.astro +113 -113
- package/templates/default/src/components/reviews/VoiceNote.astro +60 -60
- package/templates/default/src/components/sections/CollectionCards.astro +65 -65
- package/templates/default/src/components/sections/Countdown.astro +57 -57
- package/templates/default/src/components/sections/Faq.astro +44 -44
- package/templates/default/src/components/sections/FeaturedProduct.astro +71 -71
- package/templates/default/src/components/sections/Hero.astro +99 -99
- package/templates/default/src/components/sections/ImageBanner.astro +46 -46
- package/templates/default/src/components/sections/ProductGrid.astro +47 -47
- package/templates/default/src/components/sections/RichText.astro +35 -35
- package/templates/default/src/components/sections/Steps.astro +46 -46
- package/templates/default/src/components/sections/Testimonials.astro +57 -57
- package/templates/default/src/components/shell/Footer.astro +80 -80
- package/templates/default/src/components/shell/Header.astro +89 -89
- package/templates/default/src/components/shell/LanguageMenu.astro +33 -33
- package/templates/default/src/components/shell/MobileMenu.astro +65 -65
- package/templates/default/src/components/shell/SearchDialog.astro +19 -19
- package/templates/default/src/components/shell/TopBar.astro +54 -54
- package/templates/default/src/components/shell/WhatsAppWidget.astro +111 -111
- package/templates/default/src/components/store/ContactMain.astro +79 -79
- package/templates/default/src/components/store/SearchMain.astro +56 -56
- package/templates/default/src/components/store/TrackOrderMain.astro +161 -161
- package/templates/default/src/components/surface/Area.astro +20 -20
- package/templates/default/src/components/surface/PreviewBridge.astro +24 -24
- package/templates/default/src/components/surface/Sections.astro +35 -35
- package/templates/default/src/components/ui/Breadcrumbs.astro +36 -36
- package/templates/default/src/components/ui/Button.astro +45 -45
- package/templates/default/src/components/ui/EditorPlaceholder.astro +24 -24
- package/templates/default/src/components/ui/Icon.astro +93 -93
- package/templates/default/src/components/ui/Logo.astro +36 -36
- package/templates/default/src/components/ui/Photo.astro +46 -46
- package/templates/default/src/components/ui/Price.astro +24 -24
- package/templates/default/src/components/ui/ProductCard.astro +41 -41
- package/templates/default/src/components/ui/Rating.astro +22 -22
- package/templates/default/src/components/ui/Section.astro +34 -34
- package/templates/default/src/components/ui/SectionHeading.astro +19 -19
- package/templates/default/src/env.d.ts +23 -23
- package/templates/default/src/layouts/StoreLayout.astro +145 -145
- package/templates/default/src/lib/browser-copy.ts +37 -37
- package/templates/default/src/lib/context.ts +89 -89
- package/templates/default/src/lib/coupon-notice.ts +47 -47
- package/templates/default/src/lib/i18n.ts +32 -32
- package/templates/default/src/lib/images.ts +38 -38
- package/templates/default/src/lib/listing-query.ts +41 -41
- package/templates/default/src/lib/money.ts +16 -16
- package/templates/default/src/lib/motion.ts +16 -16
- package/templates/default/src/lib/nav.ts +26 -26
- package/templates/default/src/lib/offer-tiles.ts +105 -105
- package/templates/default/src/lib/order-outcome.ts +66 -66
- package/templates/default/src/lib/otp-message.ts +21 -21
- package/templates/default/src/lib/products.ts +38 -38
- package/templates/default/src/lib/review-refusals.ts +31 -31
- package/templates/default/src/lib/store.ts +31 -31
- package/templates/default/src/lib/whatsapp.ts +29 -29
- package/templates/default/src/middleware.ts +23 -23
- package/templates/default/src/pages/404.astro +22 -22
- package/templates/default/src/pages/{wocart-preview.astro → abcart-preview.astro} +43 -43
- package/templates/default/src/pages/{wocart-revalidate.ts → abcart-revalidate.ts} +10 -10
- package/templates/default/src/pages/{wocart-styles.css.ts → abcart-styles.css.ts} +5 -5
- package/templates/default/src/pages/{wocart-theme.json.ts → abcart-theme.json.ts} +5 -5
- package/templates/default/src/pages/api/abandoned/convert.ts +4 -4
- package/templates/default/src/pages/api/abandoned/upsert.ts +4 -4
- package/templates/default/src/pages/api/cart/validate.ts +4 -4
- package/templates/default/src/pages/api/communes/[wilayaId].ts +4 -4
- package/templates/default/src/pages/api/otp/send.ts +4 -4
- package/templates/default/src/pages/api/otp/verify.ts +4 -4
- package/templates/default/src/pages/api/reviews/[productId].ts +3 -3
- package/templates/default/src/pages/api/reviews/eligibility.ts +3 -3
- package/templates/default/src/pages/api/reviews/submit.ts +3 -3
- package/templates/default/src/pages/api/reviews/upload.ts +3 -3
- package/templates/default/src/pages/category/[slug].astro +20 -20
- package/templates/default/src/pages/checkout.astro +25 -25
- package/templates/default/src/pages/collections/[handle].astro +20 -20
- package/templates/default/src/pages/contact.astro +17 -17
- package/templates/default/src/pages/index.astro +9 -9
- package/templates/default/src/pages/lp/[slug].astro +47 -47
- package/templates/default/src/pages/pages/[slug].astro +16 -16
- package/templates/default/src/pages/products/[handle].astro +37 -37
- package/templates/default/src/pages/products/index.astro +15 -15
- package/templates/default/src/pages/robots.txt.ts +9 -9
- package/templates/default/src/pages/search.astro +19 -19
- package/templates/default/src/pages/sitemap.xml.ts +12 -12
- package/templates/default/src/pages/thank-you.astro +33 -33
- package/templates/default/src/pages/track-order.astro +17 -17
- package/templates/default/src/scripts/cart.ts +371 -371
- package/templates/default/src/scripts/checkout/coupon.ts +96 -96
- package/templates/default/src/scripts/checkout/delivery.ts +86 -86
- package/templates/default/src/scripts/checkout/form.ts +54 -54
- package/templates/default/src/scripts/checkout/otp.ts +106 -106
- package/templates/default/src/scripts/checkout/picker.ts +169 -169
- package/templates/default/src/scripts/checkout/quote.ts +169 -169
- package/templates/default/src/scripts/checkout/submit.ts +113 -113
- package/templates/default/src/scripts/checkout/tracking.ts +74 -74
- package/templates/default/src/scripts/countdown.ts +29 -29
- package/templates/default/src/scripts/events.ts +4 -4
- package/templates/default/src/scripts/hero.ts +49 -49
- package/templates/default/src/scripts/listing.ts +13 -13
- package/templates/default/src/scripts/order-form.ts +100 -100
- package/templates/default/src/scripts/product.ts +306 -306
- package/templates/default/src/scripts/public-config.ts +79 -79
- package/templates/default/src/scripts/reviews.ts +358 -358
- package/templates/default/src/scripts/site.ts +131 -131
- package/templates/default/src/scripts/thank-you.ts +11 -11
- package/templates/default/src/scripts/voice-note.ts +94 -94
- package/templates/default/src/styles/base.css +1030 -1030
- package/templates/default/src/styles/checkout.css +1111 -1111
- package/templates/default/src/styles/global.css +12 -12
- package/templates/default/src/styles/product.css +871 -871
- package/templates/default/src/styles/reviews.css +533 -533
- package/templates/default/src/styles/sections.css +931 -931
- package/templates/default/src/styles/shell.css +970 -970
- package/templates/default/src/styles/tokens.css +583 -583
- package/templates/default/src/theme/api-routes.ts +10 -10
- package/templates/default/src/theme/content/catalog.ts +117 -117
- package/templates/default/src/theme/content/checkout.ts +168 -168
- package/templates/default/src/theme/content/editor.ts +21 -21
- package/templates/default/src/theme/content/index.ts +45 -45
- package/templates/default/src/theme/content/reviews.ts +76 -76
- package/templates/default/src/theme/content/shell.ts +59 -59
- package/templates/default/src/theme/content/types.ts +12 -12
- package/templates/default/src/theme/documents/catalog.json +11 -11
- package/templates/default/src/theme/documents/category.json +11 -11
- package/templates/default/src/theme/documents/checkout.json +11 -11
- package/templates/default/src/theme/documents/collection.json +11 -11
- package/templates/default/src/theme/documents/contact.json +11 -11
- package/templates/default/src/theme/documents/footer.area.json +11 -11
- package/templates/default/src/theme/documents/header.area.json +16 -16
- package/templates/default/src/theme/documents/home.json +313 -313
- package/templates/default/src/theme/documents/landing.json +8 -8
- package/templates/default/src/theme/documents/page.json +11 -11
- package/templates/default/src/theme/documents/product.json +125 -125
- package/templates/default/src/theme/documents/search.json +11 -11
- package/templates/default/src/theme/documents/thank-you.json +11 -11
- package/templates/default/src/theme/documents/track-order.json +11 -11
- package/templates/default/src/theme/fields.ts +61 -61
- package/templates/default/src/theme/key.ts +11 -11
- package/templates/default/src/theme/labels.ts +231 -231
- package/templates/default/src/theme/locales.ts +11 -11
- package/templates/default/src/theme/palette.ts +14 -14
- package/templates/default/src/theme/sections/collection-cards.ts +65 -65
- package/templates/default/src/theme/sections/faq.ts +74 -74
- package/templates/default/src/theme/sections/featured-product.ts +42 -42
- package/templates/default/src/theme/sections/hero.ts +47 -47
- package/templates/default/src/theme/sections/image-banner.ts +25 -25
- package/templates/default/src/theme/sections/index.ts +40 -40
- package/templates/default/src/theme/sections/landing.ts +150 -150
- package/templates/default/src/theme/sections/listing.ts +84 -84
- package/templates/default/src/theme/sections/product-grid.ts +42 -42
- package/templates/default/src/theme/sections/product.ts +297 -297
- package/templates/default/src/theme/sections/rich-text.ts +39 -39
- package/templates/default/src/theme/sections/shared.ts +54 -54
- package/templates/default/src/theme/sections/shell.ts +85 -85
- package/templates/default/src/theme/sections/store-pages.ts +101 -101
- package/templates/default/src/theme/sections/testimonials.ts +61 -61
- package/templates/default/src/theme/sections/thank-you.ts +35 -35
- package/templates/default/src/theme/settings.ts +124 -124
- package/templates/default/src/theme/theme.ts +65 -65
- package/templates/default/tests/fixtures/canvas-tote.json +62 -62
- package/templates/default/tests/fixtures/linen-shirt.json +128 -128
- package/templates/default/tests/unit/checkout.test.ts +241 -241
- package/templates/default/tests/unit/contract.test.ts +170 -169
- package/templates/default/tests/unit/contrast.test.ts +68 -68
- package/templates/default/tests/unit/copy.test.ts +101 -101
- package/templates/default/tests/unit/design-values.test.ts +95 -95
- package/templates/default/tests/unit/editability.test.ts +180 -180
- package/templates/default/tests/unit/fixtures.ts +91 -91
- package/templates/default/tests/unit/footer.test.ts +54 -54
- package/templates/default/tests/unit/guards.test.ts +340 -340
- package/templates/default/tests/unit/images.test.ts +26 -26
- package/templates/default/tests/unit/landing.test.ts +151 -151
- package/templates/default/tests/unit/languages.test.ts +42 -42
- package/templates/default/tests/unit/middleware.test.ts +53 -53
- package/templates/default/tests/unit/offer-tiles.test.ts +75 -75
- package/templates/default/tests/unit/render.ts +38 -38
- package/templates/default/tests/unit/reviews.test.ts +25 -25
- package/templates/default/tests/unit/rules.test.ts +59 -59
- package/templates/default/tests/unit/source.ts +75 -75
- package/templates/default/tests/unit/store-pages.test.ts +147 -147
- package/templates/default/tests/unit/studio.test.ts +202 -202
- package/templates/default/tests/unit/testimonials.test.ts +52 -52
- package/templates/default/tests/unit/thank-you.test.ts +52 -52
- package/templates/default/tests/unit/tracking.test.ts +29 -29
- package/templates/default/tests/unit/voice-note.test.ts +42 -42
- package/templates/default/tsconfig.json +13 -13
- package/templates/default/vitest.config.ts +15 -15
- package/templates/default/wrangler.jsonc +16 -16
|
@@ -1,371 +1,371 @@
|
|
|
1
|
-
import {
|
|
2
|
-
addedLines,
|
|
3
|
-
basketLineKey,
|
|
4
|
-
createBasketStore,
|
|
5
|
-
createQuoter,
|
|
6
|
-
formatMoney,
|
|
7
|
-
MAX_BASKET_LINE_QUANTITY,
|
|
8
|
-
STOREFRONT_API_PATHS,
|
|
9
|
-
type BasketLine,
|
|
10
|
-
type CartValidation,
|
|
11
|
-
type CreateOrderVariantSelection,
|
|
12
|
-
storefrontTracking,
|
|
13
|
-
} from "@abcart/storefront";
|
|
14
|
-
import { escapeHtml, readPublicConfig, translate } from "./public-config";
|
|
15
|
-
import { motionMs } from "@/lib/motion";
|
|
16
|
-
|
|
17
|
-
const config = readPublicConfig();
|
|
18
|
-
const basket = createBasketStore();
|
|
19
|
-
const quoter = createQuoter({ endpoint: STOREFRONT_API_PATHS.cartValidate });
|
|
20
|
-
const tr = (key: string, values?: Record<string, string | number>) => translate(config, key, values);
|
|
21
|
-
const money = (amount: number | null | undefined) =>
|
|
22
|
-
amount === null || amount === undefined || !Number.isFinite(amount)
|
|
23
|
-
? "—"
|
|
24
|
-
: formatMoney(Math.round(amount), { symbol: config.symbol, locale: config.localeTag, decimals: 0 });
|
|
25
|
-
|
|
26
|
-
let validation: CartValidation | null = null;
|
|
27
|
-
let validationFailed = false;
|
|
28
|
-
let refreshTimer = 0;
|
|
29
|
-
let lastFocus: HTMLElement | null = null;
|
|
30
|
-
|
|
31
|
-
// Looked up when used: the editor's preview may swap the page under this script.
|
|
32
|
-
const findDrawer = () => document.querySelector<HTMLElement>("[data-cart-drawer]");
|
|
33
|
-
let toastTimer = 0;
|
|
34
|
-
|
|
35
|
-
function lineLookup(productId: string, variantId: string | null) {
|
|
36
|
-
return validation?.lines.find((line) => line.productId === productId && (line.variantId ?? null) === (variantId ?? null)) ?? null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function lineTotal(line: BasketLine): number {
|
|
40
|
-
const server = lineLookup(line.productId, line.variantId);
|
|
41
|
-
return server ? server.lineTotal : line.unitPrice * line.quantity;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function lineBlocked(line: BasketLine): boolean {
|
|
45
|
-
return Boolean(lineLookup(line.productId, line.variantId)?.blocker);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function blockedLabel(line: BasketLine): string {
|
|
49
|
-
const blocker = lineLookup(line.productId, line.variantId)?.blocker;
|
|
50
|
-
if (blocker === "out_of_stock") return tr("cart.blockedOut");
|
|
51
|
-
if (blocker === "unlisted") return tr("cart.blockedUnlisted");
|
|
52
|
-
if (blocker === "missing") return tr("cart.blockedMissing");
|
|
53
|
-
return "";
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function renderBadges() {
|
|
57
|
-
const count = basket.getCount();
|
|
58
|
-
document.querySelectorAll<HTMLElement>("[data-cart-count]").forEach((badge) => {
|
|
59
|
-
badge.textContent = String(count);
|
|
60
|
-
badge.dataset.empty = count === 0 ? "true" : "false";
|
|
61
|
-
});
|
|
62
|
-
document.querySelectorAll<HTMLElement>("[data-cart-count-label]").forEach((label) => {
|
|
63
|
-
const lines = basket.getLines().length;
|
|
64
|
-
label.textContent = lines > 0 ? `· ${tr("cart.count", { count })}` : "";
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function renderLines() {
|
|
69
|
-
const lines = basket.getLines();
|
|
70
|
-
document.querySelectorAll<HTMLElement>("[data-cart-lines]").forEach((container) => {
|
|
71
|
-
if (lines.length === 0) {
|
|
72
|
-
container.innerHTML = "";
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
container.innerHTML = lines
|
|
76
|
-
.map((line) => {
|
|
77
|
-
const server = lineLookup(line.productId, line.variantId);
|
|
78
|
-
const blocked = Boolean(server?.blocker);
|
|
79
|
-
const max = Math.min(server?.maxQuantity ?? MAX_BASKET_LINE_QUANTITY, MAX_BASKET_LINE_QUANTITY);
|
|
80
|
-
const image = line.image ? `<img src="${escapeHtml(line.image)}" alt="" width="82" height="82" loading="lazy" />` : "";
|
|
81
|
-
const href = line.href || config.routes.catalog;
|
|
82
|
-
const serverTotal = lineTotal(line);
|
|
83
|
-
const compare = server && server.unitPrice * line.quantity > serverTotal ? `<small>${money(server.unitPrice * line.quantity)}</small>` : "";
|
|
84
|
-
return `
|
|
85
|
-
<div class="cart-line" data-cart-line data-product-id="${escapeHtml(line.productId)}" data-variant-id="${escapeHtml(line.variantId ?? "")}" data-blocked="${blocked}">
|
|
86
|
-
<a class="cart-line__image" href="${escapeHtml(href)}" aria-hidden="true" tabindex="-1">${image}</a>
|
|
87
|
-
<div class="cart-line__body">
|
|
88
|
-
<a class="cart-line__title" href="${escapeHtml(href)}">${escapeHtml(line.productName)}</a>
|
|
89
|
-
${line.variantLabel && line.variantLabel !== "Default Title" ? `<p class="cart-line__variant" dir="auto">${escapeHtml(line.variantLabel)}</p>` : ""}
|
|
90
|
-
${blocked ? `<p class="cart-line__blocked">${escapeHtml(blockedLabel(line))}</p>` : ""}
|
|
91
|
-
<div class="cart-line__footer">
|
|
92
|
-
<label class="stepper" aria-label="${escapeHtml(tr("cart.quantity"))}">
|
|
93
|
-
<button type="button" data-cart-step="-1" aria-label="${escapeHtml(tr("cart.decrease"))}">−</button>
|
|
94
|
-
<input type="number" value="${line.quantity}" min="1" max="${max}" inputmode="numeric" data-cart-qty aria-label="${escapeHtml(tr("cart.quantity"))}" />
|
|
95
|
-
<button type="button" data-cart-step="1" aria-label="${escapeHtml(tr("cart.increase"))}">+</button>
|
|
96
|
-
</label>
|
|
97
|
-
<span class="cart-line__price">${compare}${money(lineTotal(line))}</span>
|
|
98
|
-
<button class="cart-line__remove" type="button" data-cart-remove aria-label="${escapeHtml(tr("cart.remove"))}">
|
|
99
|
-
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18M8 6V4h8v2m-9 0 1 14h8l1-14M10 10v7M14 10v7"/></svg>
|
|
100
|
-
</button>
|
|
101
|
-
</div>
|
|
102
|
-
${server?.offerName ? `<p class="cart-line__variant">${escapeHtml(server.offerName)}</p>` : ""}
|
|
103
|
-
</div>
|
|
104
|
-
</div>`;
|
|
105
|
-
})
|
|
106
|
-
.join("");
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
document.querySelectorAll<HTMLElement>("[data-cart-empty]").forEach((empty) => {
|
|
110
|
-
empty.hidden = lines.length > 0;
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function renderRewards() {
|
|
115
|
-
const rewards = validation?.rewards ?? [];
|
|
116
|
-
document.querySelectorAll<HTMLElement>("[data-cart-rewards]").forEach((container) => {
|
|
117
|
-
container.innerHTML = rewards
|
|
118
|
-
.map(
|
|
119
|
-
(reward) => `<div class="cart-line" data-cart-reward>
|
|
120
|
-
<div class="cart-line__body">
|
|
121
|
-
<p class="cart-line__title">${escapeHtml(reward.productName ?? tr("cart.gift"))}${reward.variantLabel ? ` — <bdi>${escapeHtml(reward.variantLabel)}</bdi>` : ""}</p>
|
|
122
|
-
<p class="cart-line__variant">${escapeHtml(reward.offerName ?? tr("checkout.gift"))} · ${reward.quantity}</p>
|
|
123
|
-
</div>
|
|
124
|
-
<span class="cart-line__price">${escapeHtml(tr("checkout.free"))}</span>
|
|
125
|
-
</div>`,
|
|
126
|
-
)
|
|
127
|
-
.join("");
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function renderSummary() {
|
|
132
|
-
const lines = basket.getLines();
|
|
133
|
-
const subtotal = validation?.subtotal ?? basket.getSubtotal();
|
|
134
|
-
const blocked = lines.some((line) => lineBlocked(line));
|
|
135
|
-
const count = basket.getCount();
|
|
136
|
-
document.querySelectorAll<HTMLElement>("[data-cart-subtotal]").forEach((node) => (node.textContent = money(subtotal)));
|
|
137
|
-
document.querySelectorAll<HTMLElement>("[data-cart-total]").forEach((node) => (node.textContent = money(subtotal)));
|
|
138
|
-
document.querySelectorAll<HTMLElement>("[data-cart-delivery]").forEach((node) => (node.textContent = "—"));
|
|
139
|
-
document.querySelectorAll<HTMLElement>("[data-cart-count-label]").forEach((label) => {
|
|
140
|
-
label.textContent = lines.length > 0 ? `· ${tr("cart.count", { count })}` : "";
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const canCheckout = lines.length > 0 && !blocked;
|
|
144
|
-
document.querySelectorAll<HTMLAnchorElement>("[data-cart-checkout]").forEach((link) => {
|
|
145
|
-
link.setAttribute("aria-disabled", canCheckout ? "false" : "true");
|
|
146
|
-
if (canCheckout) link.removeAttribute("tabindex");
|
|
147
|
-
else link.setAttribute("tabindex", "-1");
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
document.querySelectorAll<HTMLElement>("[data-cart-validate-error]").forEach((node) => {
|
|
151
|
-
node.hidden = !validationFailed;
|
|
152
|
-
if (validationFailed) node.textContent = tr("cart.validateFailed");
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
renderFreeShipping(subtotal, lines.length > 0);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function renderFreeShipping(subtotal: number, hasLines: boolean) {
|
|
159
|
-
const free = validation?.freeDelivery;
|
|
160
|
-
const threshold = free?.threshold ?? config.freeShippingThreshold;
|
|
161
|
-
const offerFree = Boolean(free?.fromOffer && free.qualified);
|
|
162
|
-
document.querySelectorAll<HTMLElement>("[data-free-ship]").forEach((root) => {
|
|
163
|
-
if (!hasLines || (!offerFree && (!threshold || threshold <= 0))) {
|
|
164
|
-
root.hidden = true;
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
root.hidden = false;
|
|
168
|
-
const qualified = offerFree || free?.qualified || (threshold ? subtotal >= threshold : false);
|
|
169
|
-
const remaining = free?.remaining ?? (threshold ? Math.max(threshold - subtotal, 0) : 0);
|
|
170
|
-
root.dataset.qualified = qualified ? "true" : "false";
|
|
171
|
-
const text = qualified ? tr("cart.freeQualified") : tr("cart.freeProgress", { amount: money(remaining) });
|
|
172
|
-
const textNode = root.querySelector<HTMLElement>("[data-free-ship-text]");
|
|
173
|
-
if (textNode) textNode.textContent = text;
|
|
174
|
-
const bar = root.querySelector<HTMLElement>("[data-free-ship-bar]");
|
|
175
|
-
if (bar) bar.style.width = `${qualified ? 100 : threshold ? Math.min(100, Math.round((subtotal / threshold) * 100)) : 0}%`;
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function renderAll() {
|
|
180
|
-
renderBadges();
|
|
181
|
-
renderLines();
|
|
182
|
-
renderRewards();
|
|
183
|
-
renderSummary();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async function refreshValidation() {
|
|
187
|
-
const items = basket.toOrderItems();
|
|
188
|
-
if (items.length === 0 || !config.cartEnabled) {
|
|
189
|
-
validation = null;
|
|
190
|
-
validationFailed = false;
|
|
191
|
-
renderAll();
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const answer = await quoter.quote({ items });
|
|
195
|
-
// A slower, earlier answer never replaces a newer one.
|
|
196
|
-
if (answer.status === "stale") return;
|
|
197
|
-
validation = answer.status === "fresh" ? answer.quote : null;
|
|
198
|
-
validationFailed = answer.status === "failed";
|
|
199
|
-
renderAll();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function scheduleRefresh() {
|
|
203
|
-
window.clearTimeout(refreshTimer);
|
|
204
|
-
refreshTimer = window.setTimeout(() => void refreshValidation(), 250);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function openDrawer() {
|
|
208
|
-
const drawer = findDrawer();
|
|
209
|
-
if (!drawer) return;
|
|
210
|
-
lastFocus = document.activeElement as HTMLElement | null;
|
|
211
|
-
drawer.dataset.open = "true";
|
|
212
|
-
drawer.setAttribute("aria-hidden", "false");
|
|
213
|
-
drawer.inert = false;
|
|
214
|
-
document.body.classList.add("has-drawer-open");
|
|
215
|
-
window.setTimeout(() => drawer.querySelector<HTMLElement>("button[data-cart-close]")?.focus(), motionMs("--motion-drawer-focus-delay"));
|
|
216
|
-
void refreshValidation();
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function closeDrawer() {
|
|
220
|
-
const drawer = findDrawer();
|
|
221
|
-
if (!drawer) return;
|
|
222
|
-
drawer.dataset.open = "false";
|
|
223
|
-
drawer.setAttribute("aria-hidden", "true");
|
|
224
|
-
drawer.inert = true;
|
|
225
|
-
document.body.classList.remove("has-drawer-open");
|
|
226
|
-
lastFocus?.focus();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function showToast(message: string) {
|
|
230
|
-
const toast = document.querySelector<HTMLElement>("[data-toast]");
|
|
231
|
-
const toastText = toast?.querySelector<HTMLElement>("[data-toast-text]");
|
|
232
|
-
if (!toast || !toastText) return;
|
|
233
|
-
toastText.textContent = message;
|
|
234
|
-
toast.dataset.visible = "true";
|
|
235
|
-
window.clearTimeout(toastTimer);
|
|
236
|
-
toastTimer = window.setTimeout(() => (toast.dataset.visible = "false"), motionMs("--motion-toast-visible"));
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function addFromElement(button: HTMLElement) {
|
|
240
|
-
if (!config.cartEnabled) return;
|
|
241
|
-
const form = button.closest<HTMLElement>("[data-product-form]");
|
|
242
|
-
const read = (name: string) => {
|
|
243
|
-
if (form) {
|
|
244
|
-
const input = form.querySelector<HTMLInputElement>(`[name="${name}"]`);
|
|
245
|
-
if (input) return input.value;
|
|
246
|
-
}
|
|
247
|
-
return button.dataset[name] ?? "";
|
|
248
|
-
};
|
|
249
|
-
const productId = read("productId");
|
|
250
|
-
const productName = read("productName");
|
|
251
|
-
if (!productId || !productName) return;
|
|
252
|
-
const variantId = read("variantId") || null;
|
|
253
|
-
const variantLabel = read("variantLabel") || null;
|
|
254
|
-
const offerId = read("offerId") || undefined;
|
|
255
|
-
const price = Number(read("pricePerUnit")) || 0;
|
|
256
|
-
const href = read("href") || config.routes.catalog;
|
|
257
|
-
const image = read("image") || "";
|
|
258
|
-
const qty = Math.max(1, Math.min(MAX_BASKET_LINE_QUANTITY, Number(read("quantity")) || 1));
|
|
259
|
-
const line = { productId, productName, unitPrice: price, href, image, ...(offerId ? { offerId } : {}) };
|
|
260
|
-
// A variant chosen for each piece of an offer goes in as one line per variant, all or nothing.
|
|
261
|
-
let pieces: CreateOrderVariantSelection[] = [];
|
|
262
|
-
try {
|
|
263
|
-
const parsed: unknown = JSON.parse(read("variantSelections") || "[]");
|
|
264
|
-
if (Array.isArray(parsed)) pieces = parsed as CreateOrderVariantSelection[];
|
|
265
|
-
} catch {
|
|
266
|
-
pieces = [];
|
|
267
|
-
}
|
|
268
|
-
const before = basket.getLines();
|
|
269
|
-
const added = addedLines(before, pieces.length > 0 ? basket.addSelections(line, pieces) : basket.addLine({ ...line, variantId, variantLabel, quantity: qty }));
|
|
270
|
-
if (added.length === 0) {
|
|
271
|
-
showToast(tr("cart.full"));
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
// What this tap put in, never the whole basket.
|
|
275
|
-
storefrontTracking().addedToCart(added);
|
|
276
|
-
void refreshValidation();
|
|
277
|
-
if (button.dataset.buyNow === "true") {
|
|
278
|
-
window.location.assign(config.routes.checkout);
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
if (findDrawer()) openDrawer();
|
|
282
|
-
else showToast(tr("cart.added", { product: productName }));
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function parseLine(element: HTMLElement) {
|
|
286
|
-
return {
|
|
287
|
-
productId: element.dataset.productId ?? "",
|
|
288
|
-
variantId: element.dataset.variantId || null,
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
document.addEventListener("click", (event) => {
|
|
293
|
-
const target = event.target as HTMLElement;
|
|
294
|
-
const add = target.closest<HTMLElement>("[data-add-to-cart]");
|
|
295
|
-
if (add) {
|
|
296
|
-
event.preventDefault();
|
|
297
|
-
addFromElement(add);
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
const open = target.closest<HTMLElement>("[data-cart-open]");
|
|
302
|
-
if (open && findDrawer()) {
|
|
303
|
-
event.preventDefault();
|
|
304
|
-
event.stopPropagation();
|
|
305
|
-
openDrawer();
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (target.closest("[data-cart-close]")) {
|
|
310
|
-
closeDrawer();
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const lineEl = target.closest<HTMLElement>("[data-cart-line]");
|
|
315
|
-
if (lineEl) {
|
|
316
|
-
const { productId, variantId } = parseLine(lineEl);
|
|
317
|
-
const key = basketLineKey(productId, variantId);
|
|
318
|
-
const qtyInput = lineEl.querySelector<HTMLInputElement>("[data-cart-qty]");
|
|
319
|
-
const step = target.closest<HTMLElement>("[data-cart-step]");
|
|
320
|
-
if (step && qtyInput) {
|
|
321
|
-
const direction = Number(step.dataset.cartStep) || 0;
|
|
322
|
-
const next = Math.max(1, Math.min(MAX_BASKET_LINE_QUANTITY, Number(qtyInput.value || 1) + direction));
|
|
323
|
-
basket.setQuantity(key, next);
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
if (target.closest("[data-cart-remove]")) {
|
|
327
|
-
basket.removeLine(key);
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const checkout = target.closest<HTMLAnchorElement>("[data-cart-checkout]");
|
|
333
|
-
if (checkout && checkout.getAttribute("aria-disabled") === "true") {
|
|
334
|
-
event.preventDefault();
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
document.addEventListener("change", (event) => {
|
|
340
|
-
const input = (event.target as HTMLElement).closest<HTMLInputElement>("[data-cart-qty]");
|
|
341
|
-
if (!input) return;
|
|
342
|
-
const lineEl = input.closest<HTMLElement>("[data-cart-line]");
|
|
343
|
-
if (!lineEl) return;
|
|
344
|
-
const { productId, variantId } = parseLine(lineEl);
|
|
345
|
-
const max = Number(input.max) || MAX_BASKET_LINE_QUANTITY;
|
|
346
|
-
const next = Math.max(1, Math.min(max, Number(input.value) || 1));
|
|
347
|
-
basket.setQuantity(basketLineKey(productId, variantId), next);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
document.addEventListener("keydown", (event) => {
|
|
351
|
-
if (event.key === "Escape" && findDrawer()?.dataset.open === "true") closeDrawer();
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
basket.subscribe(() => {
|
|
355
|
-
renderAll();
|
|
356
|
-
scheduleRefresh();
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
if (document.querySelector("[data-clear-cart]")) {
|
|
362
|
-
basket.clear();
|
|
363
|
-
validation = null;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
renderAll();
|
|
367
|
-
if (document.querySelector("[data-cart-page]") || findDrawer()) {
|
|
368
|
-
void refreshValidation();
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
window.addEventListener("focus", () => scheduleRefresh());
|
|
1
|
+
import {
|
|
2
|
+
addedLines,
|
|
3
|
+
basketLineKey,
|
|
4
|
+
createBasketStore,
|
|
5
|
+
createQuoter,
|
|
6
|
+
formatMoney,
|
|
7
|
+
MAX_BASKET_LINE_QUANTITY,
|
|
8
|
+
STOREFRONT_API_PATHS,
|
|
9
|
+
type BasketLine,
|
|
10
|
+
type CartValidation,
|
|
11
|
+
type CreateOrderVariantSelection,
|
|
12
|
+
storefrontTracking,
|
|
13
|
+
} from "@abcart/storefront";
|
|
14
|
+
import { escapeHtml, readPublicConfig, translate } from "./public-config";
|
|
15
|
+
import { motionMs } from "@/lib/motion";
|
|
16
|
+
|
|
17
|
+
const config = readPublicConfig();
|
|
18
|
+
const basket = createBasketStore();
|
|
19
|
+
const quoter = createQuoter({ endpoint: STOREFRONT_API_PATHS.cartValidate });
|
|
20
|
+
const tr = (key: string, values?: Record<string, string | number>) => translate(config, key, values);
|
|
21
|
+
const money = (amount: number | null | undefined) =>
|
|
22
|
+
amount === null || amount === undefined || !Number.isFinite(amount)
|
|
23
|
+
? "—"
|
|
24
|
+
: formatMoney(Math.round(amount), { symbol: config.symbol, locale: config.localeTag, decimals: 0 });
|
|
25
|
+
|
|
26
|
+
let validation: CartValidation | null = null;
|
|
27
|
+
let validationFailed = false;
|
|
28
|
+
let refreshTimer = 0;
|
|
29
|
+
let lastFocus: HTMLElement | null = null;
|
|
30
|
+
|
|
31
|
+
// Looked up when used: the editor's preview may swap the page under this script.
|
|
32
|
+
const findDrawer = () => document.querySelector<HTMLElement>("[data-cart-drawer]");
|
|
33
|
+
let toastTimer = 0;
|
|
34
|
+
|
|
35
|
+
function lineLookup(productId: string, variantId: string | null) {
|
|
36
|
+
return validation?.lines.find((line) => line.productId === productId && (line.variantId ?? null) === (variantId ?? null)) ?? null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function lineTotal(line: BasketLine): number {
|
|
40
|
+
const server = lineLookup(line.productId, line.variantId);
|
|
41
|
+
return server ? server.lineTotal : line.unitPrice * line.quantity;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function lineBlocked(line: BasketLine): boolean {
|
|
45
|
+
return Boolean(lineLookup(line.productId, line.variantId)?.blocker);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function blockedLabel(line: BasketLine): string {
|
|
49
|
+
const blocker = lineLookup(line.productId, line.variantId)?.blocker;
|
|
50
|
+
if (blocker === "out_of_stock") return tr("cart.blockedOut");
|
|
51
|
+
if (blocker === "unlisted") return tr("cart.blockedUnlisted");
|
|
52
|
+
if (blocker === "missing") return tr("cart.blockedMissing");
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function renderBadges() {
|
|
57
|
+
const count = basket.getCount();
|
|
58
|
+
document.querySelectorAll<HTMLElement>("[data-cart-count]").forEach((badge) => {
|
|
59
|
+
badge.textContent = String(count);
|
|
60
|
+
badge.dataset.empty = count === 0 ? "true" : "false";
|
|
61
|
+
});
|
|
62
|
+
document.querySelectorAll<HTMLElement>("[data-cart-count-label]").forEach((label) => {
|
|
63
|
+
const lines = basket.getLines().length;
|
|
64
|
+
label.textContent = lines > 0 ? `· ${tr("cart.count", { count })}` : "";
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function renderLines() {
|
|
69
|
+
const lines = basket.getLines();
|
|
70
|
+
document.querySelectorAll<HTMLElement>("[data-cart-lines]").forEach((container) => {
|
|
71
|
+
if (lines.length === 0) {
|
|
72
|
+
container.innerHTML = "";
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
container.innerHTML = lines
|
|
76
|
+
.map((line) => {
|
|
77
|
+
const server = lineLookup(line.productId, line.variantId);
|
|
78
|
+
const blocked = Boolean(server?.blocker);
|
|
79
|
+
const max = Math.min(server?.maxQuantity ?? MAX_BASKET_LINE_QUANTITY, MAX_BASKET_LINE_QUANTITY);
|
|
80
|
+
const image = line.image ? `<img src="${escapeHtml(line.image)}" alt="" width="82" height="82" loading="lazy" />` : "";
|
|
81
|
+
const href = line.href || config.routes.catalog;
|
|
82
|
+
const serverTotal = lineTotal(line);
|
|
83
|
+
const compare = server && server.unitPrice * line.quantity > serverTotal ? `<small>${money(server.unitPrice * line.quantity)}</small>` : "";
|
|
84
|
+
return `
|
|
85
|
+
<div class="cart-line" data-cart-line data-product-id="${escapeHtml(line.productId)}" data-variant-id="${escapeHtml(line.variantId ?? "")}" data-blocked="${blocked}">
|
|
86
|
+
<a class="cart-line__image" href="${escapeHtml(href)}" aria-hidden="true" tabindex="-1">${image}</a>
|
|
87
|
+
<div class="cart-line__body">
|
|
88
|
+
<a class="cart-line__title" href="${escapeHtml(href)}">${escapeHtml(line.productName)}</a>
|
|
89
|
+
${line.variantLabel && line.variantLabel !== "Default Title" ? `<p class="cart-line__variant" dir="auto">${escapeHtml(line.variantLabel)}</p>` : ""}
|
|
90
|
+
${blocked ? `<p class="cart-line__blocked">${escapeHtml(blockedLabel(line))}</p>` : ""}
|
|
91
|
+
<div class="cart-line__footer">
|
|
92
|
+
<label class="stepper" aria-label="${escapeHtml(tr("cart.quantity"))}">
|
|
93
|
+
<button type="button" data-cart-step="-1" aria-label="${escapeHtml(tr("cart.decrease"))}">−</button>
|
|
94
|
+
<input type="number" value="${line.quantity}" min="1" max="${max}" inputmode="numeric" data-cart-qty aria-label="${escapeHtml(tr("cart.quantity"))}" />
|
|
95
|
+
<button type="button" data-cart-step="1" aria-label="${escapeHtml(tr("cart.increase"))}">+</button>
|
|
96
|
+
</label>
|
|
97
|
+
<span class="cart-line__price">${compare}${money(lineTotal(line))}</span>
|
|
98
|
+
<button class="cart-line__remove" type="button" data-cart-remove aria-label="${escapeHtml(tr("cart.remove"))}">
|
|
99
|
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18M8 6V4h8v2m-9 0 1 14h8l1-14M10 10v7M14 10v7"/></svg>
|
|
100
|
+
</button>
|
|
101
|
+
</div>
|
|
102
|
+
${server?.offerName ? `<p class="cart-line__variant">${escapeHtml(server.offerName)}</p>` : ""}
|
|
103
|
+
</div>
|
|
104
|
+
</div>`;
|
|
105
|
+
})
|
|
106
|
+
.join("");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
document.querySelectorAll<HTMLElement>("[data-cart-empty]").forEach((empty) => {
|
|
110
|
+
empty.hidden = lines.length > 0;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function renderRewards() {
|
|
115
|
+
const rewards = validation?.rewards ?? [];
|
|
116
|
+
document.querySelectorAll<HTMLElement>("[data-cart-rewards]").forEach((container) => {
|
|
117
|
+
container.innerHTML = rewards
|
|
118
|
+
.map(
|
|
119
|
+
(reward) => `<div class="cart-line" data-cart-reward>
|
|
120
|
+
<div class="cart-line__body">
|
|
121
|
+
<p class="cart-line__title">${escapeHtml(reward.productName ?? tr("cart.gift"))}${reward.variantLabel ? ` — <bdi>${escapeHtml(reward.variantLabel)}</bdi>` : ""}</p>
|
|
122
|
+
<p class="cart-line__variant">${escapeHtml(reward.offerName ?? tr("checkout.gift"))} · ${reward.quantity}</p>
|
|
123
|
+
</div>
|
|
124
|
+
<span class="cart-line__price">${escapeHtml(tr("checkout.free"))}</span>
|
|
125
|
+
</div>`,
|
|
126
|
+
)
|
|
127
|
+
.join("");
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function renderSummary() {
|
|
132
|
+
const lines = basket.getLines();
|
|
133
|
+
const subtotal = validation?.subtotal ?? basket.getSubtotal();
|
|
134
|
+
const blocked = lines.some((line) => lineBlocked(line));
|
|
135
|
+
const count = basket.getCount();
|
|
136
|
+
document.querySelectorAll<HTMLElement>("[data-cart-subtotal]").forEach((node) => (node.textContent = money(subtotal)));
|
|
137
|
+
document.querySelectorAll<HTMLElement>("[data-cart-total]").forEach((node) => (node.textContent = money(subtotal)));
|
|
138
|
+
document.querySelectorAll<HTMLElement>("[data-cart-delivery]").forEach((node) => (node.textContent = "—"));
|
|
139
|
+
document.querySelectorAll<HTMLElement>("[data-cart-count-label]").forEach((label) => {
|
|
140
|
+
label.textContent = lines.length > 0 ? `· ${tr("cart.count", { count })}` : "";
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const canCheckout = lines.length > 0 && !blocked;
|
|
144
|
+
document.querySelectorAll<HTMLAnchorElement>("[data-cart-checkout]").forEach((link) => {
|
|
145
|
+
link.setAttribute("aria-disabled", canCheckout ? "false" : "true");
|
|
146
|
+
if (canCheckout) link.removeAttribute("tabindex");
|
|
147
|
+
else link.setAttribute("tabindex", "-1");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
document.querySelectorAll<HTMLElement>("[data-cart-validate-error]").forEach((node) => {
|
|
151
|
+
node.hidden = !validationFailed;
|
|
152
|
+
if (validationFailed) node.textContent = tr("cart.validateFailed");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
renderFreeShipping(subtotal, lines.length > 0);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function renderFreeShipping(subtotal: number, hasLines: boolean) {
|
|
159
|
+
const free = validation?.freeDelivery;
|
|
160
|
+
const threshold = free?.threshold ?? config.freeShippingThreshold;
|
|
161
|
+
const offerFree = Boolean(free?.fromOffer && free.qualified);
|
|
162
|
+
document.querySelectorAll<HTMLElement>("[data-free-ship]").forEach((root) => {
|
|
163
|
+
if (!hasLines || (!offerFree && (!threshold || threshold <= 0))) {
|
|
164
|
+
root.hidden = true;
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
root.hidden = false;
|
|
168
|
+
const qualified = offerFree || free?.qualified || (threshold ? subtotal >= threshold : false);
|
|
169
|
+
const remaining = free?.remaining ?? (threshold ? Math.max(threshold - subtotal, 0) : 0);
|
|
170
|
+
root.dataset.qualified = qualified ? "true" : "false";
|
|
171
|
+
const text = qualified ? tr("cart.freeQualified") : tr("cart.freeProgress", { amount: money(remaining) });
|
|
172
|
+
const textNode = root.querySelector<HTMLElement>("[data-free-ship-text]");
|
|
173
|
+
if (textNode) textNode.textContent = text;
|
|
174
|
+
const bar = root.querySelector<HTMLElement>("[data-free-ship-bar]");
|
|
175
|
+
if (bar) bar.style.width = `${qualified ? 100 : threshold ? Math.min(100, Math.round((subtotal / threshold) * 100)) : 0}%`;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function renderAll() {
|
|
180
|
+
renderBadges();
|
|
181
|
+
renderLines();
|
|
182
|
+
renderRewards();
|
|
183
|
+
renderSummary();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function refreshValidation() {
|
|
187
|
+
const items = basket.toOrderItems();
|
|
188
|
+
if (items.length === 0 || !config.cartEnabled) {
|
|
189
|
+
validation = null;
|
|
190
|
+
validationFailed = false;
|
|
191
|
+
renderAll();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const answer = await quoter.quote({ items });
|
|
195
|
+
// A slower, earlier answer never replaces a newer one.
|
|
196
|
+
if (answer.status === "stale") return;
|
|
197
|
+
validation = answer.status === "fresh" ? answer.quote : null;
|
|
198
|
+
validationFailed = answer.status === "failed";
|
|
199
|
+
renderAll();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function scheduleRefresh() {
|
|
203
|
+
window.clearTimeout(refreshTimer);
|
|
204
|
+
refreshTimer = window.setTimeout(() => void refreshValidation(), 250);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function openDrawer() {
|
|
208
|
+
const drawer = findDrawer();
|
|
209
|
+
if (!drawer) return;
|
|
210
|
+
lastFocus = document.activeElement as HTMLElement | null;
|
|
211
|
+
drawer.dataset.open = "true";
|
|
212
|
+
drawer.setAttribute("aria-hidden", "false");
|
|
213
|
+
drawer.inert = false;
|
|
214
|
+
document.body.classList.add("has-drawer-open");
|
|
215
|
+
window.setTimeout(() => drawer.querySelector<HTMLElement>("button[data-cart-close]")?.focus(), motionMs("--motion-drawer-focus-delay"));
|
|
216
|
+
void refreshValidation();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function closeDrawer() {
|
|
220
|
+
const drawer = findDrawer();
|
|
221
|
+
if (!drawer) return;
|
|
222
|
+
drawer.dataset.open = "false";
|
|
223
|
+
drawer.setAttribute("aria-hidden", "true");
|
|
224
|
+
drawer.inert = true;
|
|
225
|
+
document.body.classList.remove("has-drawer-open");
|
|
226
|
+
lastFocus?.focus();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function showToast(message: string) {
|
|
230
|
+
const toast = document.querySelector<HTMLElement>("[data-toast]");
|
|
231
|
+
const toastText = toast?.querySelector<HTMLElement>("[data-toast-text]");
|
|
232
|
+
if (!toast || !toastText) return;
|
|
233
|
+
toastText.textContent = message;
|
|
234
|
+
toast.dataset.visible = "true";
|
|
235
|
+
window.clearTimeout(toastTimer);
|
|
236
|
+
toastTimer = window.setTimeout(() => (toast.dataset.visible = "false"), motionMs("--motion-toast-visible"));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function addFromElement(button: HTMLElement) {
|
|
240
|
+
if (!config.cartEnabled) return;
|
|
241
|
+
const form = button.closest<HTMLElement>("[data-product-form]");
|
|
242
|
+
const read = (name: string) => {
|
|
243
|
+
if (form) {
|
|
244
|
+
const input = form.querySelector<HTMLInputElement>(`[name="${name}"]`);
|
|
245
|
+
if (input) return input.value;
|
|
246
|
+
}
|
|
247
|
+
return button.dataset[name] ?? "";
|
|
248
|
+
};
|
|
249
|
+
const productId = read("productId");
|
|
250
|
+
const productName = read("productName");
|
|
251
|
+
if (!productId || !productName) return;
|
|
252
|
+
const variantId = read("variantId") || null;
|
|
253
|
+
const variantLabel = read("variantLabel") || null;
|
|
254
|
+
const offerId = read("offerId") || undefined;
|
|
255
|
+
const price = Number(read("pricePerUnit")) || 0;
|
|
256
|
+
const href = read("href") || config.routes.catalog;
|
|
257
|
+
const image = read("image") || "";
|
|
258
|
+
const qty = Math.max(1, Math.min(MAX_BASKET_LINE_QUANTITY, Number(read("quantity")) || 1));
|
|
259
|
+
const line = { productId, productName, unitPrice: price, href, image, ...(offerId ? { offerId } : {}) };
|
|
260
|
+
// A variant chosen for each piece of an offer goes in as one line per variant, all or nothing.
|
|
261
|
+
let pieces: CreateOrderVariantSelection[] = [];
|
|
262
|
+
try {
|
|
263
|
+
const parsed: unknown = JSON.parse(read("variantSelections") || "[]");
|
|
264
|
+
if (Array.isArray(parsed)) pieces = parsed as CreateOrderVariantSelection[];
|
|
265
|
+
} catch {
|
|
266
|
+
pieces = [];
|
|
267
|
+
}
|
|
268
|
+
const before = basket.getLines();
|
|
269
|
+
const added = addedLines(before, pieces.length > 0 ? basket.addSelections(line, pieces) : basket.addLine({ ...line, variantId, variantLabel, quantity: qty }));
|
|
270
|
+
if (added.length === 0) {
|
|
271
|
+
showToast(tr("cart.full"));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
// What this tap put in, never the whole basket.
|
|
275
|
+
storefrontTracking().addedToCart(added);
|
|
276
|
+
void refreshValidation();
|
|
277
|
+
if (button.dataset.buyNow === "true") {
|
|
278
|
+
window.location.assign(config.routes.checkout);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (findDrawer()) openDrawer();
|
|
282
|
+
else showToast(tr("cart.added", { product: productName }));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function parseLine(element: HTMLElement) {
|
|
286
|
+
return {
|
|
287
|
+
productId: element.dataset.productId ?? "",
|
|
288
|
+
variantId: element.dataset.variantId || null,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
document.addEventListener("click", (event) => {
|
|
293
|
+
const target = event.target as HTMLElement;
|
|
294
|
+
const add = target.closest<HTMLElement>("[data-add-to-cart]");
|
|
295
|
+
if (add) {
|
|
296
|
+
event.preventDefault();
|
|
297
|
+
addFromElement(add);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const open = target.closest<HTMLElement>("[data-cart-open]");
|
|
302
|
+
if (open && findDrawer()) {
|
|
303
|
+
event.preventDefault();
|
|
304
|
+
event.stopPropagation();
|
|
305
|
+
openDrawer();
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (target.closest("[data-cart-close]")) {
|
|
310
|
+
closeDrawer();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const lineEl = target.closest<HTMLElement>("[data-cart-line]");
|
|
315
|
+
if (lineEl) {
|
|
316
|
+
const { productId, variantId } = parseLine(lineEl);
|
|
317
|
+
const key = basketLineKey(productId, variantId);
|
|
318
|
+
const qtyInput = lineEl.querySelector<HTMLInputElement>("[data-cart-qty]");
|
|
319
|
+
const step = target.closest<HTMLElement>("[data-cart-step]");
|
|
320
|
+
if (step && qtyInput) {
|
|
321
|
+
const direction = Number(step.dataset.cartStep) || 0;
|
|
322
|
+
const next = Math.max(1, Math.min(MAX_BASKET_LINE_QUANTITY, Number(qtyInput.value || 1) + direction));
|
|
323
|
+
basket.setQuantity(key, next);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (target.closest("[data-cart-remove]")) {
|
|
327
|
+
basket.removeLine(key);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const checkout = target.closest<HTMLAnchorElement>("[data-cart-checkout]");
|
|
333
|
+
if (checkout && checkout.getAttribute("aria-disabled") === "true") {
|
|
334
|
+
event.preventDefault();
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
document.addEventListener("change", (event) => {
|
|
340
|
+
const input = (event.target as HTMLElement).closest<HTMLInputElement>("[data-cart-qty]");
|
|
341
|
+
if (!input) return;
|
|
342
|
+
const lineEl = input.closest<HTMLElement>("[data-cart-line]");
|
|
343
|
+
if (!lineEl) return;
|
|
344
|
+
const { productId, variantId } = parseLine(lineEl);
|
|
345
|
+
const max = Number(input.max) || MAX_BASKET_LINE_QUANTITY;
|
|
346
|
+
const next = Math.max(1, Math.min(max, Number(input.value) || 1));
|
|
347
|
+
basket.setQuantity(basketLineKey(productId, variantId), next);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
document.addEventListener("keydown", (event) => {
|
|
351
|
+
if (event.key === "Escape" && findDrawer()?.dataset.open === "true") closeDrawer();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
basket.subscribe(() => {
|
|
355
|
+
renderAll();
|
|
356
|
+
scheduleRefresh();
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
if (document.querySelector("[data-clear-cart]")) {
|
|
362
|
+
basket.clear();
|
|
363
|
+
validation = null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
renderAll();
|
|
367
|
+
if (document.querySelector("[data-cart-page]") || findDrawer()) {
|
|
368
|
+
void refreshValidation();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
window.addEventListener("focus", () => scheduleRefresh());
|