@hszrdm/create-commerce 1.7.0 → 1.8.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/package.json +1 -1
- package/templates/backend/.env.example +13 -0
- package/templates/backend/package.json +1 -1
- package/templates/storefront/src/app/[countryCode]/categories/[handle]/page.tsx +5 -1
- package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +14 -5
- package/templates/storefront/src/app/[countryCode]/products/page.tsx +10 -1
- package/templates/storefront/src/app/account/orders/[id]/page.tsx +17 -2
- package/templates/storefront/src/app/checkout/success/page.tsx +5 -0
- package/templates/storefront/src/app/documents/page.tsx +27 -0
- package/templates/storefront/src/app/layout.tsx +8 -1
- package/templates/storefront/src/components/add-to-cart.tsx +16 -14
- package/templates/storefront/src/components/arukereso-backlink.tsx +43 -0
- package/templates/storefront/src/components/arukereso-trusted-shop-widget.tsx +81 -0
- package/templates/storefront/src/components/arukereso-trusted-shop.tsx +71 -0
- package/templates/storefront/src/components/cart-view.tsx +4 -2
- package/templates/storefront/src/components/category-documents.tsx +18 -0
- package/templates/storefront/src/components/checkout-analytics.tsx +22 -0
- package/templates/storefront/src/components/document-list.tsx +47 -0
- package/templates/storefront/src/components/google-ads-cart-remarketing.tsx +71 -0
- package/templates/storefront/src/components/google-ads-purchase.tsx +37 -0
- package/templates/storefront/src/components/price-display.tsx +39 -0
- package/templates/storefront/src/components/product-card.tsx +7 -6
- package/templates/storefront/src/components/product-documents.tsx +18 -0
- package/templates/storefront/src/components/product-view-tracker.tsx +17 -2
- package/templates/storefront/src/components/reorder-button.tsx +74 -0
- package/templates/storefront/src/components/variant-price-display.tsx +7 -11
- package/templates/storefront/src/lib/core/catalog.ts +6 -2
- package/templates/storefront/src/lib/core/commerce.ts +51 -0
- package/templates/storefront/src/lib/core/documents.ts +37 -0
- package/templates/storefront/src/lib/core/google-ads.ts +154 -7
- package/templates/storefront/src/lib/core/reorder.ts +26 -0
- package/templates/storefront/src/lib/storefront-routes.ts +3 -0
- package/templates/storefront/src/proxy.ts +2 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { StoreDocument } from "@/lib/core/documents"
|
|
2
|
+
|
|
3
|
+
export function DocumentList({
|
|
4
|
+
documents,
|
|
5
|
+
locale = "hu-HU",
|
|
6
|
+
emptyLabel,
|
|
7
|
+
}: {
|
|
8
|
+
documents: StoreDocument[]
|
|
9
|
+
locale?: string
|
|
10
|
+
emptyLabel?: string
|
|
11
|
+
}) {
|
|
12
|
+
const en = locale.toLowerCase().startsWith("en")
|
|
13
|
+
if (!documents.length) {
|
|
14
|
+
return <p className="muted">{emptyLabel || (en ? "No documents are available." : "Nincs elérhető dokumentum.")}</p>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<div className="stack">
|
|
19
|
+
{documents.map((document) => (
|
|
20
|
+
<article className="card stack" key={document.id}>
|
|
21
|
+
<div className="stack" style={{ gap: ".35rem" }}>
|
|
22
|
+
<div className="button-row" style={{ justifyContent: "space-between", alignItems: "baseline" }}>
|
|
23
|
+
<strong>{document.title}</strong>
|
|
24
|
+
<span className="muted">{document.document_type}</span>
|
|
25
|
+
</div>
|
|
26
|
+
{document.description ? <p className="muted">{document.description}</p> : null}
|
|
27
|
+
<div className="product-meta">
|
|
28
|
+
{document.version ? <span>{en ? "Version" : "Verzió"}: {document.version}</span> : null}
|
|
29
|
+
{document.locale ? <span>{document.locale}</span> : null}
|
|
30
|
+
{document.file_size ? <span>{formatBytes(document.file_size)}</span> : null}
|
|
31
|
+
</div>
|
|
32
|
+
</div>
|
|
33
|
+
<a className="button secondary" href={document.file_url} target="_blank" rel="noreferrer">
|
|
34
|
+
{en ? "Open document" : "Dokumentum megnyitása"}
|
|
35
|
+
</a>
|
|
36
|
+
</article>
|
|
37
|
+
))}
|
|
38
|
+
</div>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function formatBytes(value: number) {
|
|
43
|
+
if (!Number.isFinite(value) || value <= 0) return ""
|
|
44
|
+
if (value < 1024) return `${Math.round(value)} B`
|
|
45
|
+
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`
|
|
46
|
+
return `${(value / (1024 * 1024)).toFixed(1)} MB`
|
|
47
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef } from "react"
|
|
4
|
+
import { useCart } from "./cart-provider"
|
|
5
|
+
import {
|
|
6
|
+
googleAdsRemarketingFromSource,
|
|
7
|
+
trackGoogleAdsRemarketing,
|
|
8
|
+
} from "@/lib/core/google-ads"
|
|
9
|
+
import type { CoreConfig } from "@/lib/core/types"
|
|
10
|
+
|
|
11
|
+
type Line = Record<string, unknown>
|
|
12
|
+
|
|
13
|
+
export function GoogleAdsCartRemarketing({
|
|
14
|
+
googleAds,
|
|
15
|
+
}: {
|
|
16
|
+
googleAds: CoreConfig["marketing"]["google_ads"]
|
|
17
|
+
}) {
|
|
18
|
+
const { cart, loading } = useCart()
|
|
19
|
+
const previous = useRef<Map<string, number> | null>(null)
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (loading || !cart) return
|
|
23
|
+
|
|
24
|
+
const lines = Array.isArray(cart.items)
|
|
25
|
+
? cart.items.filter(isRecord)
|
|
26
|
+
: []
|
|
27
|
+
const current = new Map<string, number>()
|
|
28
|
+
|
|
29
|
+
for (const line of lines) {
|
|
30
|
+
const variantId = text(line.variant_id)
|
|
31
|
+
if (!variantId) continue
|
|
32
|
+
current.set(variantId, quantity(line.quantity))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (previous.current) {
|
|
36
|
+
for (const line of lines) {
|
|
37
|
+
const variantId = text(line.variant_id)
|
|
38
|
+
if (!variantId) continue
|
|
39
|
+
const nextQuantity = quantity(line.quantity)
|
|
40
|
+
const previousQuantity = previous.current.get(variantId) || 0
|
|
41
|
+
const added = nextQuantity - previousQuantity
|
|
42
|
+
if (added <= 0) continue
|
|
43
|
+
|
|
44
|
+
const ecommerce = googleAdsRemarketingFromSource({
|
|
45
|
+
currency_code: cart.currency_code,
|
|
46
|
+
items: [{ ...line, quantity: added }],
|
|
47
|
+
})
|
|
48
|
+
trackGoogleAdsRemarketing(googleAds, "add_to_cart", ecommerce)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
previous.current = current
|
|
53
|
+
}, [cart, googleAds, loading])
|
|
54
|
+
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function quantity(value: unknown) {
|
|
59
|
+
const parsed = typeof value === "number" ? value : Number(value)
|
|
60
|
+
return Number.isFinite(parsed) ? Math.max(0, Math.trunc(parsed)) : 0
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function text(value: unknown) {
|
|
64
|
+
return typeof value === "string" || typeof value === "number"
|
|
65
|
+
? String(value).trim()
|
|
66
|
+
: ""
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isRecord(value: unknown): value is Line {
|
|
70
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value))
|
|
71
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect } from "react"
|
|
4
|
+
import { COOKIE_CONSENT_EVENT } from "@/lib/core/consent"
|
|
5
|
+
import {
|
|
6
|
+
googleAdsRemarketingFromSource,
|
|
7
|
+
trackGoogleAdsPurchase,
|
|
8
|
+
trackGoogleAdsRemarketing,
|
|
9
|
+
} from "@/lib/core/google-ads"
|
|
10
|
+
import type { CoreConfig } from "@/lib/core/types"
|
|
11
|
+
|
|
12
|
+
type GoogleAdsPurchaseProps = {
|
|
13
|
+
googleAds: CoreConfig["marketing"]["google_ads"]
|
|
14
|
+
order: Record<string, unknown>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function GoogleAdsPurchase({ googleAds, order }: GoogleAdsPurchaseProps) {
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const transactionId = String(order.id || order.display_id || "").trim()
|
|
20
|
+
const send = () => {
|
|
21
|
+
trackGoogleAdsPurchase(googleAds, order)
|
|
22
|
+
if (!transactionId) return
|
|
23
|
+
trackGoogleAdsRemarketing(
|
|
24
|
+
googleAds,
|
|
25
|
+
"purchase",
|
|
26
|
+
googleAdsRemarketingFromSource(order),
|
|
27
|
+
{ dedupeKey: transactionId },
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
send()
|
|
32
|
+
window.addEventListener(COOKIE_CONSENT_EVENT, send)
|
|
33
|
+
return () => window.removeEventListener(COOKIE_CONSENT_EVENT, send)
|
|
34
|
+
}, [googleAds, order])
|
|
35
|
+
|
|
36
|
+
return null
|
|
37
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { formatMoney, type PriceBreakdown } from "@/lib/core/commerce"
|
|
2
|
+
|
|
3
|
+
export function PriceDisplay({
|
|
4
|
+
price,
|
|
5
|
+
locale,
|
|
6
|
+
className = "product-price",
|
|
7
|
+
compact = false,
|
|
8
|
+
}: {
|
|
9
|
+
price: PriceBreakdown
|
|
10
|
+
locale: string
|
|
11
|
+
className?: string
|
|
12
|
+
compact?: boolean
|
|
13
|
+
}) {
|
|
14
|
+
if (!Number.isFinite(price.amount)) return null
|
|
15
|
+
|
|
16
|
+
const english = locale.toLowerCase().startsWith("en")
|
|
17
|
+
const rate = Number.isFinite(price.taxRate) ? Math.round(price.taxRate * 100) / 100 : Number.NaN
|
|
18
|
+
const showBreakdown = price.hasTaxBreakdown
|
|
19
|
+
&& Number.isFinite(price.netAmount)
|
|
20
|
+
&& Number.isFinite(price.grossAmount)
|
|
21
|
+
&& price.grossAmount >= price.netAmount
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<div className={className}>
|
|
25
|
+
<strong>{formatMoney(price.grossAmount, price.currency, locale)}</strong>
|
|
26
|
+
{showBreakdown ? (
|
|
27
|
+
<span className="muted small">
|
|
28
|
+
{english ? "Net" : "Nettó"}: {formatMoney(price.netAmount, price.currency, locale)}
|
|
29
|
+
{Number.isFinite(rate) && rate > 0 ? ` + ${formatTaxRate(rate)} ${english ? "VAT" : "ÁFA"}` : ""}
|
|
30
|
+
{!compact ? ` · ${english ? "gross price" : "bruttó ár"}` : ""}
|
|
31
|
+
</span>
|
|
32
|
+
) : null}
|
|
33
|
+
</div>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function formatTaxRate(rate: number) {
|
|
38
|
+
return `${Number.isInteger(rate) ? rate.toFixed(0) : rate.toFixed(2).replace(/0+$/, "").replace(/\.$/, "")}%`
|
|
39
|
+
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import Link from "next/link"
|
|
2
|
+
import { PriceDisplay } from "./price-display"
|
|
2
3
|
import {
|
|
3
4
|
asRecords,
|
|
4
|
-
formatMoney,
|
|
5
5
|
isRecord,
|
|
6
|
-
|
|
7
|
-
productVariantPrice,
|
|
6
|
+
productVariantPriceBreakdown,
|
|
8
7
|
stringValue,
|
|
9
8
|
} from "@/lib/core/commerce"
|
|
10
9
|
import type { CoreConfig } from "@/lib/core/types"
|
|
@@ -22,10 +21,10 @@ export function ProductCard({
|
|
|
22
21
|
}) {
|
|
23
22
|
const variants = asRecords(product.variants)
|
|
24
23
|
const variant = variants[0] || null
|
|
25
|
-
const price =
|
|
26
|
-
const currency = productVariantCurrency(variant, config.store.default_currency)
|
|
24
|
+
const price = productVariantPriceBreakdown(variant, config.store.default_currency)
|
|
27
25
|
const handle = stringValue(product.handle)
|
|
28
26
|
const activeLocale = locale || storefrontLocale(config.store.locale)
|
|
27
|
+
const formatLocale = activeLocale === "en" ? "en-US" : "hu-HU"
|
|
29
28
|
const copy = storefrontCopy(activeLocale)
|
|
30
29
|
const href = productPath(activeLocale, handle)
|
|
31
30
|
const thumbnail = stringValue(product.thumbnail)
|
|
@@ -40,7 +39,9 @@ export function ProductCard({
|
|
|
40
39
|
<div className="product-card-body">
|
|
41
40
|
{categories[0] && isRecord(categories[0]) ? <span className="eyebrow">{stringValue(categories[0].name)}</span> : null}
|
|
42
41
|
<h2>{title}</h2>
|
|
43
|
-
{Number.isFinite(price)
|
|
42
|
+
{Number.isFinite(price.amount)
|
|
43
|
+
? <PriceDisplay price={price} locale={formatLocale} compact />
|
|
44
|
+
: <span className="muted">{copy.priceOnProductPage}</span>}
|
|
44
45
|
</div>
|
|
45
46
|
</Link>
|
|
46
47
|
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DocumentList } from "@/components/document-list"
|
|
2
|
+
import { getDocuments } from "@/lib/core/documents"
|
|
3
|
+
|
|
4
|
+
export async function ProductDocuments({ productId, locale = "hu-HU" }: { productId: string; locale?: string }) {
|
|
5
|
+
const en = locale.toLowerCase().startsWith("en")
|
|
6
|
+
const payload = await getDocuments({ productId, locale }).catch(() => ({ documents: [], count: 0 }))
|
|
7
|
+
if (!payload.documents.length) return null
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<section className="stack" aria-labelledby="product-documents-title">
|
|
11
|
+
<div>
|
|
12
|
+
<p className="eyebrow">{en ? "Downloads" : "Letöltések"}</p>
|
|
13
|
+
<h2 id="product-documents-title">{en ? "Documents" : "Dokumentumok"}</h2>
|
|
14
|
+
</div>
|
|
15
|
+
<DocumentList documents={payload.documents} locale={locale} />
|
|
16
|
+
</section>
|
|
17
|
+
)
|
|
18
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useEffect } from "react"
|
|
4
4
|
import { COOKIE_CONSENT_EVENT } from "@/lib/core/consent"
|
|
5
|
+
import { trackGoogleAdsRemarketing } from "@/lib/core/google-ads"
|
|
5
6
|
import {
|
|
6
7
|
trackViewItem,
|
|
7
8
|
type EcommerceItem,
|
|
@@ -10,28 +11,42 @@ import type { CoreConfig } from "@/lib/core/types"
|
|
|
10
11
|
|
|
11
12
|
export function ProductViewTracker({
|
|
12
13
|
analytics,
|
|
14
|
+
googleAds,
|
|
13
15
|
item,
|
|
16
|
+
googleAdsItem,
|
|
14
17
|
currency,
|
|
15
18
|
value,
|
|
16
19
|
}: {
|
|
17
20
|
analytics: CoreConfig["analytics"]
|
|
21
|
+
googleAds: CoreConfig["marketing"]["google_ads"]
|
|
18
22
|
item: EcommerceItem
|
|
23
|
+
googleAdsItem: EcommerceItem
|
|
19
24
|
currency: string
|
|
20
25
|
value?: number
|
|
21
26
|
}) {
|
|
22
27
|
useEffect(() => {
|
|
23
|
-
const send = () =>
|
|
28
|
+
const send = () => {
|
|
24
29
|
trackViewItem(analytics, {
|
|
25
30
|
item,
|
|
26
31
|
currency,
|
|
27
32
|
value,
|
|
28
33
|
dedupeKey: `${item.item_id}:${window.location.pathname}`,
|
|
29
34
|
})
|
|
35
|
+
trackGoogleAdsRemarketing(
|
|
36
|
+
googleAds,
|
|
37
|
+
"view_item",
|
|
38
|
+
{ currency, value, items: [googleAdsItem] },
|
|
39
|
+
{
|
|
40
|
+
dedupeKey: `${googleAdsItem.item_id}:${window.location.pathname}`,
|
|
41
|
+
sessionDedupe: true,
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
}
|
|
30
45
|
|
|
31
46
|
send()
|
|
32
47
|
window.addEventListener(COOKIE_CONSENT_EVENT, send)
|
|
33
48
|
return () => window.removeEventListener(COOKIE_CONSENT_EVENT, send)
|
|
34
|
-
}, [analytics, currency, item, value])
|
|
49
|
+
}, [analytics, currency, googleAds, googleAdsItem, item, value])
|
|
35
50
|
|
|
36
51
|
return null
|
|
37
52
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { usePathname, useRouter } from "next/navigation"
|
|
4
|
+
import { useState } from "react"
|
|
5
|
+
import { reorderOrder } from "@/lib/core/reorder"
|
|
6
|
+
import { localeFromPathname, STOREFRONT_ROUTES } from "@/lib/storefront-routes"
|
|
7
|
+
import { useCart } from "./cart-provider"
|
|
8
|
+
import { useToast } from "./toast-provider"
|
|
9
|
+
|
|
10
|
+
export function ReorderButton({ orderId }: { orderId: string }) {
|
|
11
|
+
const pathname = usePathname()
|
|
12
|
+
const router = useRouter()
|
|
13
|
+
const locale = localeFromPathname(pathname)
|
|
14
|
+
const routes = STOREFRONT_ROUTES[locale]
|
|
15
|
+
const { adoptCart } = useCart()
|
|
16
|
+
const { showToast } = useToast()
|
|
17
|
+
const [busy, setBusy] = useState(false)
|
|
18
|
+
|
|
19
|
+
const reorder = async () => {
|
|
20
|
+
setBusy(true)
|
|
21
|
+
try {
|
|
22
|
+
const result = await reorderOrder(orderId)
|
|
23
|
+
if (!result.added_count || !result.cart?.id) {
|
|
24
|
+
showToast({
|
|
25
|
+
tone: "error",
|
|
26
|
+
message: locale === "en"
|
|
27
|
+
? "None of the items from this order are currently available to reorder."
|
|
28
|
+
: "A rendelés egyik tétele sem rendelhető újra jelenleg.",
|
|
29
|
+
})
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await adoptCart(String(result.cart.id))
|
|
34
|
+
|
|
35
|
+
if (result.skipped_items.length) {
|
|
36
|
+
showToast({
|
|
37
|
+
tone: "info",
|
|
38
|
+
durationMs: 7000,
|
|
39
|
+
message: locale === "en"
|
|
40
|
+
? `${result.skipped_items.length} previous item(s) could not be added. The cart contains the available items at current prices.`
|
|
41
|
+
: `${result.skipped_items.length} korábbi tételt nem lehetett kosárba tenni. A kosár az elérhető tételeket tartalmazza, aktuális árakon.`,
|
|
42
|
+
})
|
|
43
|
+
} else {
|
|
44
|
+
showToast({
|
|
45
|
+
tone: "success",
|
|
46
|
+
message: locale === "en"
|
|
47
|
+
? "The order has been rebuilt in a new cart using current prices and availability."
|
|
48
|
+
: "A rendelés új kosárba került, aktuális árakkal és készlettel.",
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
router.push(routes.cart)
|
|
53
|
+
} catch (cause) {
|
|
54
|
+
showToast({
|
|
55
|
+
tone: "error",
|
|
56
|
+
message: cause instanceof Error
|
|
57
|
+
? cause.message
|
|
58
|
+
: locale === "en"
|
|
59
|
+
? "The order could not be reordered."
|
|
60
|
+
: "Az újrarendelés nem sikerült.",
|
|
61
|
+
})
|
|
62
|
+
} finally {
|
|
63
|
+
setBusy(false)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<button className="button" type="button" disabled={busy} onClick={() => void reorder()}>
|
|
69
|
+
{busy
|
|
70
|
+
? locale === "en" ? "Preparing cart…" : "Kosár készítése…"
|
|
71
|
+
: locale === "en" ? "Reorder" : "Újrarendelés"}
|
|
72
|
+
</button>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
3
|
import { useEffect, useState } from "react"
|
|
4
|
-
import {
|
|
4
|
+
import { PriceDisplay } from "./price-display"
|
|
5
|
+
import type { PriceBreakdown } from "@/lib/core/commerce"
|
|
5
6
|
|
|
6
7
|
export function VariantPriceDisplay({
|
|
7
8
|
initialPrice,
|
|
8
|
-
initialCurrency,
|
|
9
9
|
locale,
|
|
10
10
|
}: {
|
|
11
|
-
initialPrice:
|
|
12
|
-
initialCurrency: string
|
|
11
|
+
initialPrice: PriceBreakdown
|
|
13
12
|
locale: string
|
|
14
13
|
}) {
|
|
15
14
|
const [price, setPrice] = useState(initialPrice)
|
|
16
|
-
const [currency, setCurrency] = useState(initialCurrency)
|
|
17
15
|
|
|
18
16
|
useEffect(() => {
|
|
19
17
|
const listener = (event: Event) => {
|
|
20
|
-
const detail = (event as CustomEvent<{ price?:
|
|
21
|
-
|
|
22
|
-
if (Number.isFinite(nextPrice)) setPrice(nextPrice)
|
|
23
|
-
if (detail?.currency) setCurrency(detail.currency)
|
|
18
|
+
const detail = (event as CustomEvent<{ price?: PriceBreakdown }>).detail
|
|
19
|
+
if (detail?.price && Number.isFinite(detail.price.amount)) setPrice(detail.price)
|
|
24
20
|
}
|
|
25
21
|
window.addEventListener("hszrdm:product-variant-change", listener)
|
|
26
22
|
return () => window.removeEventListener("hszrdm:product-variant-change", listener)
|
|
27
23
|
}, [])
|
|
28
24
|
|
|
29
|
-
if (!Number.isFinite(price)) return null
|
|
30
|
-
return <div
|
|
25
|
+
if (!Number.isFinite(price.amount)) return null
|
|
26
|
+
return <div aria-live="polite"><PriceDisplay price={price} locale={locale} /></div>
|
|
31
27
|
}
|
|
@@ -5,6 +5,7 @@ export type ProductListQuery = {
|
|
|
5
5
|
categoryId?: string
|
|
6
6
|
order?: string
|
|
7
7
|
regionId?: string
|
|
8
|
+
countryCode?: string
|
|
8
9
|
locale?: string
|
|
9
10
|
limit?: number
|
|
10
11
|
offset?: number
|
|
@@ -28,6 +29,7 @@ export function listProducts(query: ProductListQuery = {}) {
|
|
|
28
29
|
if (query.categoryId?.trim()) params.set("category_id", query.categoryId.trim())
|
|
29
30
|
if (query.order?.trim()) params.set("order", query.order.trim())
|
|
30
31
|
if (query.regionId?.trim()) params.set("region_id", query.regionId.trim())
|
|
32
|
+
if (query.countryCode?.trim()) params.set("country_code", query.countryCode.trim().toLowerCase())
|
|
31
33
|
if (query.locale?.trim()) params.set("locale", query.locale.trim())
|
|
32
34
|
return medusaFetch<{
|
|
33
35
|
products: Array<Record<string, unknown>>
|
|
@@ -73,13 +75,14 @@ export function getCategoryByHandle(handle: string, locale?: string) {
|
|
|
73
75
|
)
|
|
74
76
|
}
|
|
75
77
|
|
|
76
|
-
export function getProductByHandle(handle: string, regionId?: string, locale?: string) {
|
|
78
|
+
export function getProductByHandle(handle: string, regionId?: string, locale?: string, countryCode?: string) {
|
|
77
79
|
const params = new URLSearchParams({
|
|
78
80
|
handle,
|
|
79
81
|
limit: "1",
|
|
80
82
|
fields: "+variants.calculated_price,+categories,+images",
|
|
81
83
|
})
|
|
82
84
|
if (regionId) params.set("region_id", regionId)
|
|
85
|
+
if (countryCode?.trim()) params.set("country_code", countryCode.trim().toLowerCase())
|
|
83
86
|
if (locale?.trim()) params.set("locale", locale.trim())
|
|
84
87
|
return medusaFetch<{ products: Array<Record<string, unknown>> }>(
|
|
85
88
|
`/store/products?${params.toString()}`,
|
|
@@ -87,11 +90,12 @@ export function getProductByHandle(handle: string, regionId?: string, locale?: s
|
|
|
87
90
|
)
|
|
88
91
|
}
|
|
89
92
|
|
|
90
|
-
export function getProductById(productId: string, regionId?: string, locale?: string) {
|
|
93
|
+
export function getProductById(productId: string, regionId?: string, locale?: string, countryCode?: string) {
|
|
91
94
|
const params = new URLSearchParams({
|
|
92
95
|
fields: "+variants.calculated_price,+categories,+images",
|
|
93
96
|
})
|
|
94
97
|
if (regionId) params.set("region_id", regionId)
|
|
98
|
+
if (countryCode?.trim()) params.set("country_code", countryCode.trim().toLowerCase())
|
|
95
99
|
if (locale?.trim()) params.set("locale", locale.trim())
|
|
96
100
|
return medusaFetch<{ product: Record<string, unknown> }>(
|
|
97
101
|
`/store/products/${encodeURIComponent(productId)}?${params.toString()}`,
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
export type JsonRecord = Record<string, unknown>
|
|
2
2
|
|
|
3
|
+
export type PriceBreakdown = {
|
|
4
|
+
amount: number
|
|
5
|
+
grossAmount: number
|
|
6
|
+
netAmount: number
|
|
7
|
+
taxAmount: number
|
|
8
|
+
taxRate: number
|
|
9
|
+
currency: string
|
|
10
|
+
hasTaxBreakdown: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
3
13
|
export function isRecord(value: unknown): value is JsonRecord {
|
|
4
14
|
return Boolean(value && typeof value === "object" && !Array.isArray(value))
|
|
5
15
|
}
|
|
@@ -107,6 +117,23 @@ export function lineTotal(item: JsonRecord) {
|
|
|
107
117
|
return lineUnitPrice(item) * numberValue(item.quantity, 1)
|
|
108
118
|
}
|
|
109
119
|
|
|
120
|
+
export function linePriceBreakdown(item: JsonRecord, currency: string): PriceBreakdown {
|
|
121
|
+
const grossAmount = lineTotal(item)
|
|
122
|
+
const taxAmount = numberValue(item.tax_total, 0)
|
|
123
|
+
const netAmount = Number.isFinite(grossAmount) ? grossAmount - taxAmount : Number.NaN
|
|
124
|
+
const hasTaxBreakdown = Number.isFinite(netAmount) && taxAmount > 0
|
|
125
|
+
const taxRate = hasTaxBreakdown && netAmount > 0 ? (taxAmount / netAmount) * 100 : Number.NaN
|
|
126
|
+
return {
|
|
127
|
+
amount: grossAmount,
|
|
128
|
+
grossAmount,
|
|
129
|
+
netAmount,
|
|
130
|
+
taxAmount,
|
|
131
|
+
taxRate,
|
|
132
|
+
currency: currency.toUpperCase(),
|
|
133
|
+
hasTaxBreakdown,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
110
137
|
export function formatMoney(
|
|
111
138
|
amount: number,
|
|
112
139
|
currency: string,
|
|
@@ -133,6 +160,30 @@ export function productVariantCurrency(variant: JsonRecord | null | undefined, f
|
|
|
133
160
|
return stringValue(price?.currency_code || fallback).toUpperCase()
|
|
134
161
|
}
|
|
135
162
|
|
|
163
|
+
export function productVariantPriceBreakdown(
|
|
164
|
+
variant: JsonRecord | null | undefined,
|
|
165
|
+
fallbackCurrency: string,
|
|
166
|
+
): PriceBreakdown {
|
|
167
|
+
const price = isRecord(variant?.calculated_price) ? variant.calculated_price : null
|
|
168
|
+
const amount = price ? numberValue(price.calculated_amount, Number.NaN) : Number.NaN
|
|
169
|
+
const withTax = price ? numberValue(price.calculated_amount_with_tax, Number.NaN) : Number.NaN
|
|
170
|
+
const withoutTax = price ? numberValue(price.calculated_amount_without_tax, Number.NaN) : Number.NaN
|
|
171
|
+
const grossAmount = Number.isFinite(withTax) ? withTax : amount
|
|
172
|
+
const netAmount = Number.isFinite(withoutTax) ? withoutTax : amount
|
|
173
|
+
const hasTaxBreakdown = Number.isFinite(withTax) && Number.isFinite(withoutTax)
|
|
174
|
+
const taxAmount = hasTaxBreakdown ? Math.max(0, withTax - withoutTax) : Number.NaN
|
|
175
|
+
const taxRate = hasTaxBreakdown && withoutTax > 0 ? (taxAmount / withoutTax) * 100 : Number.NaN
|
|
176
|
+
return {
|
|
177
|
+
amount: grossAmount,
|
|
178
|
+
grossAmount,
|
|
179
|
+
netAmount,
|
|
180
|
+
taxAmount,
|
|
181
|
+
taxRate,
|
|
182
|
+
currency: productVariantCurrency(variant, fallbackCurrency),
|
|
183
|
+
hasTaxBreakdown,
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
136
187
|
export function orderDisplayId(order: JsonRecord) {
|
|
137
188
|
return stringValue(order.display_id || order.id)
|
|
138
189
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { medusaFetch } from "./client"
|
|
2
|
+
|
|
3
|
+
export type StoreDocument = {
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
description?: string | null
|
|
7
|
+
document_type: string
|
|
8
|
+
file_url: string
|
|
9
|
+
file_name: string
|
|
10
|
+
mime_type?: string | null
|
|
11
|
+
file_size?: number | null
|
|
12
|
+
locale?: string | null
|
|
13
|
+
version?: string | null
|
|
14
|
+
valid_from?: string | null
|
|
15
|
+
valid_until?: string | null
|
|
16
|
+
product_ids: string[]
|
|
17
|
+
category_ids: string[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function getDocuments(input: {
|
|
21
|
+
productId?: string
|
|
22
|
+
categoryId?: string
|
|
23
|
+
documentType?: string
|
|
24
|
+
locale?: string
|
|
25
|
+
limit?: number
|
|
26
|
+
} = {}) {
|
|
27
|
+
const params = new URLSearchParams()
|
|
28
|
+
if (input.productId) params.set("product_id", input.productId)
|
|
29
|
+
if (input.categoryId) params.set("category_id", input.categoryId)
|
|
30
|
+
if (input.documentType) params.set("document_type", input.documentType)
|
|
31
|
+
if (input.locale) params.set("locale", input.locale)
|
|
32
|
+
params.set("limit", String(input.limit || 200))
|
|
33
|
+
return medusaFetch<{ documents: StoreDocument[]; count: number }>(
|
|
34
|
+
`/store/documents?${params.toString()}`,
|
|
35
|
+
{ next: { revalidate: 60 } },
|
|
36
|
+
)
|
|
37
|
+
}
|