@growth-labs/store 0.1.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/README.md +41 -0
- package/dist/api.d.ts +30 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +126 -0
- package/dist/api.js.map +1 -0
- package/dist/availability.d.ts +30 -0
- package/dist/availability.d.ts.map +1 -0
- package/dist/availability.js +47 -0
- package/dist/availability.js.map +1 -0
- package/dist/cache.d.ts +34 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +80 -0
- package/dist/cache.js.map +1 -0
- package/dist/cart-client.d.ts +54 -0
- package/dist/cart-client.d.ts.map +1 -0
- package/dist/cart-client.js +347 -0
- package/dist/cart-client.js.map +1 -0
- package/dist/checkout-client.d.ts +20 -0
- package/dist/checkout-client.d.ts.map +1 -0
- package/dist/checkout-client.js +33 -0
- package/dist/checkout-client.js.map +1 -0
- package/dist/checkout-url.d.ts +25 -0
- package/dist/checkout-url.d.ts.map +1 -0
- package/dist/checkout-url.js +36 -0
- package/dist/checkout-url.js.map +1 -0
- package/dist/client.d.ts +459 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +139 -0
- package/dist/client.js.map +1 -0
- package/dist/collections.d.ts +27 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +24 -0
- package/dist/collections.js.map +1 -0
- package/dist/health.d.ts +17 -0
- package/dist/health.d.ts.map +1 -0
- package/dist/health.js +38 -0
- package/dist/health.js.map +1 -0
- package/dist/image.d.ts +25 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +43 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/options.d.ts +174 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +59 -0
- package/dist/options.js.map +1 -0
- package/dist/product-card-variant.d.ts +13 -0
- package/dist/product-card-variant.d.ts.map +1 -0
- package/dist/product-card-variant.js +33 -0
- package/dist/product-card-variant.js.map +1 -0
- package/dist/routes/cart-bootstrap.d.ts +3 -0
- package/dist/routes/cart-bootstrap.d.ts.map +1 -0
- package/dist/routes/cart-bootstrap.js +5 -0
- package/dist/routes/cart-bootstrap.js.map +1 -0
- package/dist/routes/checkout.d.ts +4 -0
- package/dist/routes/checkout.d.ts.map +1 -0
- package/dist/routes/checkout.js +14 -0
- package/dist/routes/checkout.js.map +1 -0
- package/dist/routes/webhook.d.ts +4 -0
- package/dist/routes/webhook.d.ts.map +1 -0
- package/dist/routes/webhook.js +12 -0
- package/dist/routes/webhook.js.map +1 -0
- package/dist/state.d.ts +5 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +14 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +5111 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +167 -0
- package/dist/types.js.map +1 -0
- package/dist/variant.d.ts +59 -0
- package/dist/variant.d.ts.map +1 -0
- package/dist/variant.js +81 -0
- package/dist/variant.js.map +1 -0
- package/dist/vite-plugin.d.ts +4 -0
- package/dist/vite-plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +24 -0
- package/dist/vite-plugin.js.map +1 -0
- package/package.json +59 -0
- package/src/analytics-window.d.ts +15 -0
- package/src/api.ts +184 -0
- package/src/availability.ts +65 -0
- package/src/cache.ts +118 -0
- package/src/cart-client.ts +423 -0
- package/src/checkout-client.ts +49 -0
- package/src/checkout-url.ts +52 -0
- package/src/client.ts +216 -0
- package/src/cloudflare-workers.d.ts +3 -0
- package/src/collections.ts +63 -0
- package/src/components/AddToCartButton.astro +146 -0
- package/src/components/CartDrawer.astro +276 -0
- package/src/components/CartIcon.astro +96 -0
- package/src/components/CollectionCard.astro +81 -0
- package/src/components/CurrencySelector.astro +73 -0
- package/src/components/Price.astro +34 -0
- package/src/components/ProductCard.astro +225 -0
- package/src/components/StoreCartPage.astro +219 -0
- package/src/components/StoreCollectionPage.astro +166 -0
- package/src/components/StoreIndexPage.astro +193 -0
- package/src/components/StoreProductPage.astro +296 -0
- package/src/components/VariantPicker.astro +190 -0
- package/src/health.ts +54 -0
- package/src/image.ts +58 -0
- package/src/index.ts +75 -0
- package/src/options.ts +74 -0
- package/src/product-card-variant.ts +47 -0
- package/src/routes/cart-bootstrap.ts +6 -0
- package/src/routes/checkout.ts +16 -0
- package/src/routes/webhook.ts +14 -0
- package/src/state.ts +20 -0
- package/src/types.ts +196 -0
- package/src/variant.ts +119 -0
- package/src/virtual.d.ts +6 -0
- package/src/vite-plugin.ts +27 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import type { ZodTypeAny, z } from 'zod'
|
|
2
|
+
import {
|
|
3
|
+
FourthwallError,
|
|
4
|
+
FwCartSchema,
|
|
5
|
+
FwCollectionProductsSchema,
|
|
6
|
+
FwProductSchema,
|
|
7
|
+
FwShopSchema,
|
|
8
|
+
} from './types.js'
|
|
9
|
+
|
|
10
|
+
const BASE_URL = 'https://storefront-api.fourthwall.com'
|
|
11
|
+
|
|
12
|
+
export interface FourthwallClientConfig {
|
|
13
|
+
/** Public storefront token (`ptkn_...`). Safe in browser. */
|
|
14
|
+
token: string
|
|
15
|
+
/** Optional fetch override (for tests). Defaults to globalThis.fetch. */
|
|
16
|
+
fetcher?: typeof fetch
|
|
17
|
+
/** Default currency, e.g. "USD". */
|
|
18
|
+
defaultCurrency?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type FetchOptions = {
|
|
22
|
+
method?: 'GET' | 'POST'
|
|
23
|
+
query?: Record<string, string | number | undefined>
|
|
24
|
+
body?: unknown
|
|
25
|
+
/** Single retry on transient failure. Default true. */
|
|
26
|
+
retry?: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Thin, typed client for the Fourthwall Storefront API. Every response is
|
|
31
|
+
* Zod-validated so a provider schema drift fails loudly instead of shipping
|
|
32
|
+
* an untyped payload into a page render.
|
|
33
|
+
*/
|
|
34
|
+
export function createFourthwallClient(config: FourthwallClientConfig) {
|
|
35
|
+
const fetcher = config.fetcher ?? globalThis.fetch.bind(globalThis)
|
|
36
|
+
const defaultCurrency = config.defaultCurrency ?? 'USD'
|
|
37
|
+
|
|
38
|
+
async function call<T extends ZodTypeAny>(
|
|
39
|
+
schema: T,
|
|
40
|
+
path: string,
|
|
41
|
+
opts: FetchOptions = {},
|
|
42
|
+
): Promise<z.infer<T>> {
|
|
43
|
+
const url = new URL(path, BASE_URL)
|
|
44
|
+
url.searchParams.set('storefront_token', config.token)
|
|
45
|
+
if (opts.query) {
|
|
46
|
+
for (const [k, v] of Object.entries(opts.query)) {
|
|
47
|
+
if (v === undefined || v === null || v === '') continue
|
|
48
|
+
url.searchParams.set(k, String(v))
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const init: RequestInit = {
|
|
53
|
+
method: opts.method ?? 'GET',
|
|
54
|
+
headers: {
|
|
55
|
+
'Content-Type': 'application/json',
|
|
56
|
+
Accept: 'application/json',
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
if (opts.body !== undefined) {
|
|
60
|
+
init.body = JSON.stringify(opts.body)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const urlString = url.toString()
|
|
64
|
+
let response: Response
|
|
65
|
+
try {
|
|
66
|
+
response = await fetcher(urlString, init)
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (opts.retry !== false) {
|
|
69
|
+
await sleep(200 + Math.floor(Math.random() * 200))
|
|
70
|
+
response = await fetcher(urlString, init)
|
|
71
|
+
} else {
|
|
72
|
+
throw new FourthwallError(
|
|
73
|
+
0,
|
|
74
|
+
urlString,
|
|
75
|
+
`Fetch failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (response.status === 429 && opts.retry !== false) {
|
|
81
|
+
await sleep(200 + Math.floor(Math.random() * 200))
|
|
82
|
+
response = await fetcher(urlString, init)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
let body: unknown
|
|
87
|
+
try {
|
|
88
|
+
body = await response.json()
|
|
89
|
+
} catch {
|
|
90
|
+
body = await response.text().catch(() => undefined)
|
|
91
|
+
}
|
|
92
|
+
throw new FourthwallError(
|
|
93
|
+
response.status,
|
|
94
|
+
urlString,
|
|
95
|
+
`Fourthwall ${response.status} on ${path}`,
|
|
96
|
+
body,
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let json: unknown
|
|
101
|
+
try {
|
|
102
|
+
json = await response.json()
|
|
103
|
+
} catch (err) {
|
|
104
|
+
throw new FourthwallError(
|
|
105
|
+
response.status,
|
|
106
|
+
urlString,
|
|
107
|
+
`Invalid JSON from Fourthwall: ${err instanceof Error ? err.message : String(err)}`,
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const parsed = schema.safeParse(json)
|
|
112
|
+
if (!parsed.success) {
|
|
113
|
+
throw new FourthwallError(
|
|
114
|
+
response.status,
|
|
115
|
+
urlString,
|
|
116
|
+
`Fourthwall response failed validation: ${parsed.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ')}`,
|
|
117
|
+
json,
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
return parsed.data
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
/** `GET /v1/shop` — shop metadata including checkout/public domain. */
|
|
125
|
+
getShop() {
|
|
126
|
+
return call(FwShopSchema, '/v1/shop')
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
/** `GET /v1/collections/{handle}/products` — paginated product list. */
|
|
130
|
+
getCollectionProducts(args: {
|
|
131
|
+
handle: string
|
|
132
|
+
currency?: string
|
|
133
|
+
page?: number
|
|
134
|
+
size?: number
|
|
135
|
+
}) {
|
|
136
|
+
const currency = args.currency ?? defaultCurrency
|
|
137
|
+
return call(
|
|
138
|
+
FwCollectionProductsSchema,
|
|
139
|
+
`/v1/collections/${encodeURIComponent(args.handle)}/products`,
|
|
140
|
+
{
|
|
141
|
+
query: { currency, page: args.page, size: args.size },
|
|
142
|
+
},
|
|
143
|
+
)
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
/** `GET /v1/products/{slug}` — single product by slug. */
|
|
147
|
+
getProduct(args: { slug: string; currency?: string }) {
|
|
148
|
+
const currency = args.currency ?? defaultCurrency
|
|
149
|
+
return call(FwProductSchema, `/v1/products/${encodeURIComponent(args.slug)}`, {
|
|
150
|
+
query: { currency },
|
|
151
|
+
})
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
/** `POST /v1/carts` — create a cart. */
|
|
155
|
+
createCart(args: { currency?: string } = {}) {
|
|
156
|
+
const currency = args.currency ?? defaultCurrency
|
|
157
|
+
return call(FwCartSchema, '/v1/carts', {
|
|
158
|
+
method: 'POST',
|
|
159
|
+
query: { currency },
|
|
160
|
+
body: { items: [] },
|
|
161
|
+
})
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
/** `GET /v1/carts/{cartId}`. */
|
|
165
|
+
getCart(args: { cartId: string; currency?: string }) {
|
|
166
|
+
const currency = args.currency ?? defaultCurrency
|
|
167
|
+
return call(FwCartSchema, `/v1/carts/${encodeURIComponent(args.cartId)}`, {
|
|
168
|
+
query: { currency },
|
|
169
|
+
})
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
/** `POST /v1/carts/{cartId}/add`. */
|
|
173
|
+
addToCart(args: {
|
|
174
|
+
cartId: string
|
|
175
|
+
items: Array<{ variantId: string; quantity: number }>
|
|
176
|
+
currency?: string
|
|
177
|
+
}) {
|
|
178
|
+
const currency = args.currency ?? defaultCurrency
|
|
179
|
+
return call(FwCartSchema, `/v1/carts/${encodeURIComponent(args.cartId)}/add`, {
|
|
180
|
+
method: 'POST',
|
|
181
|
+
query: { currency },
|
|
182
|
+
body: { items: args.items },
|
|
183
|
+
})
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
/** `POST /v1/carts/{cartId}/change`. */
|
|
187
|
+
changeCartItem(args: {
|
|
188
|
+
cartId: string
|
|
189
|
+
items: Array<{ variantId: string; quantity: number }>
|
|
190
|
+
currency?: string
|
|
191
|
+
}) {
|
|
192
|
+
const currency = args.currency ?? defaultCurrency
|
|
193
|
+
return call(FwCartSchema, `/v1/carts/${encodeURIComponent(args.cartId)}/change`, {
|
|
194
|
+
method: 'POST',
|
|
195
|
+
query: { currency },
|
|
196
|
+
body: { items: args.items },
|
|
197
|
+
})
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
/** `POST /v1/carts/{cartId}/remove`. */
|
|
201
|
+
removeFromCart(args: { cartId: string; itemIds: string[]; currency?: string }) {
|
|
202
|
+
const currency = args.currency ?? defaultCurrency
|
|
203
|
+
return call(FwCartSchema, `/v1/carts/${encodeURIComponent(args.cartId)}/remove`, {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
query: { currency },
|
|
206
|
+
body: { itemIds: args.itemIds },
|
|
207
|
+
})
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export type FourthwallClient = ReturnType<typeof createFourthwallClient>
|
|
213
|
+
|
|
214
|
+
function sleep(ms: number): Promise<void> {
|
|
215
|
+
return new Promise((r) => setTimeout(r, ms))
|
|
216
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export type StoreCollectionGrouping = 'division' | 'type'
|
|
2
|
+
|
|
3
|
+
export interface StoreCollection {
|
|
4
|
+
/** Fourthwall collection handle (used as URL slug). */
|
|
5
|
+
handle: string
|
|
6
|
+
/** Display name shown on cards and collection pages. */
|
|
7
|
+
name: string
|
|
8
|
+
/** Optional description for the collection hero. */
|
|
9
|
+
description?: string
|
|
10
|
+
/** Optional static cover image (public path). Preferred over the live first-product fallback when present. */
|
|
11
|
+
coverImagePath?: string
|
|
12
|
+
/** Grouping: "division" (themed line) or "type" (product form). */
|
|
13
|
+
grouping?: StoreCollectionGrouping
|
|
14
|
+
/** When true, collection stays routable but is hidden from index grids. */
|
|
15
|
+
hidden?: boolean
|
|
16
|
+
/**
|
|
17
|
+
* Extension point for sites that sell more than one kind of thing
|
|
18
|
+
* (e.g. bigbrain's physical objects alongside merch). A product page can
|
|
19
|
+
* key off this tag to mount kind-specific modules (see `kindModules` on
|
|
20
|
+
* `StoreProductPage`). Defaults to `"merch"` when unset.
|
|
21
|
+
*/
|
|
22
|
+
productKind?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getStoreCollection(
|
|
26
|
+
collections: StoreCollection[],
|
|
27
|
+
handle: string,
|
|
28
|
+
): StoreCollection | undefined {
|
|
29
|
+
return collections.find((c) => c.handle === handle)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getGroupedCollections(
|
|
33
|
+
collections: StoreCollection[],
|
|
34
|
+
grouping: StoreCollectionGrouping,
|
|
35
|
+
): StoreCollection[] {
|
|
36
|
+
return collections.filter((c) => c.grouping === grouping && !c.hidden)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function filterPublishedCollections(
|
|
40
|
+
collections: StoreCollection[],
|
|
41
|
+
getProducts: (handle: string) => Promise<readonly unknown[]>,
|
|
42
|
+
): Promise<StoreCollection[]> {
|
|
43
|
+
const results = await Promise.all(
|
|
44
|
+
collections.map(async (collection) => {
|
|
45
|
+
try {
|
|
46
|
+
const products = await getProducts(collection.handle)
|
|
47
|
+
return products.length > 0 ? collection : null
|
|
48
|
+
} catch {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
}),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
return results.filter((collection): collection is StoreCollection => collection !== null)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function resolveProductKind(
|
|
58
|
+
collections: StoreCollection[],
|
|
59
|
+
handle: string | undefined,
|
|
60
|
+
): string {
|
|
61
|
+
if (!handle) return 'merch'
|
|
62
|
+
return getStoreCollection(collections, handle)?.productKind ?? 'merch'
|
|
63
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
firstVariantId: string
|
|
4
|
+
initialAvailable: boolean
|
|
5
|
+
/** The CSS selector for the VariantPicker to read the current selection from. Defaults to the first picker in the current product root. */
|
|
6
|
+
pickerSelector?: string
|
|
7
|
+
label?: string
|
|
8
|
+
/** `"buy-now"` adds the variant and goes straight to Fourthwall checkout in one click. */
|
|
9
|
+
mode?: 'add' | 'buy-now'
|
|
10
|
+
checkoutPath?: string
|
|
11
|
+
/** Fourthwall-hosted product URL, rendered as a `noscript` fallback. */
|
|
12
|
+
fourthwallProductUrl?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
firstVariantId,
|
|
17
|
+
initialAvailable,
|
|
18
|
+
pickerSelector = '[data-variant-picker]',
|
|
19
|
+
label,
|
|
20
|
+
mode = 'add',
|
|
21
|
+
checkoutPath = '/api/store/checkout',
|
|
22
|
+
fourthwallProductUrl,
|
|
23
|
+
} = Astro.props
|
|
24
|
+
const resolvedLabel = label ?? (mode === 'buy-now' ? 'Buy now' : 'Add to cart')
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
<button
|
|
28
|
+
type="button"
|
|
29
|
+
class="w-full cursor-pointer bg-[var(--primary,var(--foreground))] px-4 py-3 text-sm font-bold uppercase tracking-[0.14em] text-[var(--primary-foreground,var(--background))] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))] disabled:cursor-not-allowed disabled:opacity-50"
|
|
30
|
+
data-add-to-cart
|
|
31
|
+
data-mode={mode}
|
|
32
|
+
data-checkout-path={checkoutPath}
|
|
33
|
+
data-first-variant-id={firstVariantId}
|
|
34
|
+
data-picker-selector={pickerSelector}
|
|
35
|
+
disabled={!initialAvailable}
|
|
36
|
+
>
|
|
37
|
+
{resolvedLabel}
|
|
38
|
+
</button>
|
|
39
|
+
|
|
40
|
+
{fourthwallProductUrl && (
|
|
41
|
+
<noscript>
|
|
42
|
+
<a href={fourthwallProductUrl} class="mt-2 block w-full text-center text-sm underline">
|
|
43
|
+
Buy on Fourthwall
|
|
44
|
+
</a>
|
|
45
|
+
</noscript>
|
|
46
|
+
)}
|
|
47
|
+
|
|
48
|
+
<script>
|
|
49
|
+
import { buyNow } from '../checkout-client.js'
|
|
50
|
+
import { cart } from '../cart-client.js'
|
|
51
|
+
import { createStoreAddToCartAvailabilityState, normalizeStoreVariantSku } from '../variant.js'
|
|
52
|
+
|
|
53
|
+
interface VariantAttrs {
|
|
54
|
+
sku?: string
|
|
55
|
+
name?: string
|
|
56
|
+
priceValue?: number
|
|
57
|
+
priceCurrency?: string
|
|
58
|
+
available?: boolean
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
document.querySelectorAll<HTMLButtonElement>('[data-add-to-cart]').forEach((button) => {
|
|
62
|
+
const availability = createStoreAddToCartAvailabilityState(!button.disabled)
|
|
63
|
+
const root = (button.closest('[data-product-root]') as HTMLElement | null) ?? null
|
|
64
|
+
const pickerSelector = button.dataset.pickerSelector ?? '[data-variant-picker]'
|
|
65
|
+
const picker = root?.querySelector<HTMLElement>(pickerSelector)
|
|
66
|
+
const mode = button.dataset.mode === 'buy-now' ? 'buy-now' : 'add'
|
|
67
|
+
const checkoutPath = button.dataset.checkoutPath ?? '/api/store/checkout'
|
|
68
|
+
|
|
69
|
+
picker?.addEventListener('fw:variant-changed', (event) => {
|
|
70
|
+
const detail = (event as CustomEvent<{ available?: boolean }>).detail
|
|
71
|
+
button.disabled = availability.recordSelection(detail.available)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
button.addEventListener('click', async () => {
|
|
75
|
+
const selected = picker?.getAttribute('data-selected') ?? button.dataset.firstVariantId ?? ''
|
|
76
|
+
if (!selected) return
|
|
77
|
+
|
|
78
|
+
let variants: Record<string, VariantAttrs> = {}
|
|
79
|
+
try {
|
|
80
|
+
variants = JSON.parse(root?.dataset.variants ?? '{}')
|
|
81
|
+
} catch {
|
|
82
|
+
variants = {}
|
|
83
|
+
}
|
|
84
|
+
const variant = variants[selected]
|
|
85
|
+
button.disabled = availability.recordSelection(variant?.available)
|
|
86
|
+
if (button.disabled) return
|
|
87
|
+
const productId = root?.dataset.productId ?? ''
|
|
88
|
+
const productName = root?.dataset.productName ?? undefined
|
|
89
|
+
const sku = normalizeStoreVariantSku(variant?.sku)
|
|
90
|
+
|
|
91
|
+
button.disabled = true
|
|
92
|
+
const originalLabel = button.textContent
|
|
93
|
+
|
|
94
|
+
if (mode === 'buy-now') {
|
|
95
|
+
button.textContent = 'Redirecting…'
|
|
96
|
+
try {
|
|
97
|
+
await buyNow(selected, { source: 'buy-now', checkoutPath })
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.error(err)
|
|
100
|
+
button.textContent = 'Try again'
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
button.textContent = originalLabel
|
|
103
|
+
button.disabled = availability.shouldDisable()
|
|
104
|
+
}, 1800)
|
|
105
|
+
}
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
button.textContent = 'Adding…'
|
|
110
|
+
try {
|
|
111
|
+
const snap = await cart.add(selected, 1, {
|
|
112
|
+
productId: productId || undefined,
|
|
113
|
+
productName,
|
|
114
|
+
...(sku ? { sku } : {}),
|
|
115
|
+
variantName: variant?.name || undefined,
|
|
116
|
+
})
|
|
117
|
+
if (productId && variant?.priceValue && variant?.priceCurrency) {
|
|
118
|
+
window.glAnalytics?.track('store_add_to_cart', {
|
|
119
|
+
productId,
|
|
120
|
+
productName,
|
|
121
|
+
...(sku ? { sku } : {}),
|
|
122
|
+
variant: variant.name || undefined,
|
|
123
|
+
price: variant.priceValue,
|
|
124
|
+
currency: variant.priceCurrency,
|
|
125
|
+
quantity: 1,
|
|
126
|
+
cartTotal: snap.totalCents / 100,
|
|
127
|
+
source: 'pdp',
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
button.textContent = 'Added'
|
|
131
|
+
setTimeout(() => {
|
|
132
|
+
button.textContent = originalLabel
|
|
133
|
+
button.disabled = availability.shouldDisable()
|
|
134
|
+
}, 1200)
|
|
135
|
+
document.dispatchEvent(new CustomEvent('fw:cart-open'))
|
|
136
|
+
} catch (err) {
|
|
137
|
+
console.error(err)
|
|
138
|
+
button.textContent = 'Try again'
|
|
139
|
+
setTimeout(() => {
|
|
140
|
+
button.textContent = originalLabel
|
|
141
|
+
button.disabled = availability.shouldDisable()
|
|
142
|
+
}, 1800)
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
</script>
|