@hszrdm/create-commerce 1.6.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.
Files changed (33) hide show
  1. package/package.json +1 -1
  2. package/templates/backend/.env.example +13 -0
  3. package/templates/backend/package.json +1 -1
  4. package/templates/storefront/src/app/[countryCode]/categories/[handle]/page.tsx +5 -1
  5. package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +14 -5
  6. package/templates/storefront/src/app/[countryCode]/products/page.tsx +10 -1
  7. package/templates/storefront/src/app/account/orders/[id]/page.tsx +17 -2
  8. package/templates/storefront/src/app/checkout/success/page.tsx +5 -0
  9. package/templates/storefront/src/app/documents/page.tsx +27 -0
  10. package/templates/storefront/src/app/layout.tsx +8 -1
  11. package/templates/storefront/src/components/add-to-cart.tsx +16 -14
  12. package/templates/storefront/src/components/arukereso-backlink.tsx +43 -0
  13. package/templates/storefront/src/components/arukereso-trusted-shop-widget.tsx +81 -0
  14. package/templates/storefront/src/components/arukereso-trusted-shop.tsx +71 -0
  15. package/templates/storefront/src/components/cart-view.tsx +4 -2
  16. package/templates/storefront/src/components/category-documents.tsx +18 -0
  17. package/templates/storefront/src/components/checkout-analytics.tsx +22 -0
  18. package/templates/storefront/src/components/document-list.tsx +47 -0
  19. package/templates/storefront/src/components/google-ads-cart-remarketing.tsx +71 -0
  20. package/templates/storefront/src/components/google-ads-purchase.tsx +37 -0
  21. package/templates/storefront/src/components/price-display.tsx +39 -0
  22. package/templates/storefront/src/components/product-card.tsx +14 -12
  23. package/templates/storefront/src/components/product-documents.tsx +18 -0
  24. package/templates/storefront/src/components/product-view-tracker.tsx +17 -2
  25. package/templates/storefront/src/components/reorder-button.tsx +74 -0
  26. package/templates/storefront/src/components/variant-price-display.tsx +7 -11
  27. package/templates/storefront/src/lib/core/catalog.ts +6 -2
  28. package/templates/storefront/src/lib/core/commerce.ts +51 -0
  29. package/templates/storefront/src/lib/core/documents.ts +37 -0
  30. package/templates/storefront/src/lib/core/google-ads.ts +154 -7
  31. package/templates/storefront/src/lib/core/reorder.ts +26 -0
  32. package/templates/storefront/src/lib/storefront-routes.ts +3 -0
  33. package/templates/storefront/src/proxy.ts +2 -0
@@ -1,4 +1,5 @@
1
1
  import { readConsentPreferences } from "./consent"
2
+ import type { EcommerceItem, EcommercePayload, EcommerceEventName } from "./tracking"
2
3
  import type { CoreConfig } from "./types"
3
4
 
4
5
  export function trackGoogleAdsPurchase(
@@ -15,18 +16,20 @@ export function trackGoogleAdsPurchase(
15
16
  const dedupeKey = `core:google-ads:purchase:${transactionId}`
16
17
  if (window.localStorage.getItem(dedupeKey) === "sent") return false
17
18
 
18
- const payload = {
19
+ const purchaseEcommerce = googleAdsRemarketingFromSource(order)
20
+ const items = purchaseEcommerce.items
21
+ .map(dynamicRemarketingItem)
22
+ .filter((item): item is NonNullable<ReturnType<typeof dynamicRemarketingItem>> => Boolean(item))
23
+
24
+ const payload = clean({
19
25
  send_to: googleAds.purchase_send_to,
20
26
  transaction_id: transactionId,
21
27
  value: finite(order.total),
22
28
  currency: text(order.currency_code).toUpperCase() || "HUF",
23
- }
29
+ items: items.length ? items : undefined,
30
+ })
24
31
 
25
- const target = window as typeof window & {
26
- dataLayer?: Array<Record<string, unknown> | unknown[]>
27
- gtag?: (...args: unknown[]) => void
28
- }
29
- target.dataLayer = target.dataLayer || []
32
+ const target = googleTarget()
30
33
 
31
34
  if (googleAds.google_tag_manager_enabled) {
32
35
  target.dataLayer.push({
@@ -46,6 +49,150 @@ export function trackGoogleAdsPurchase(
46
49
  return false
47
50
  }
48
51
 
52
+ export function trackGoogleAdsRemarketing(
53
+ googleAds: CoreConfig["marketing"]["google_ads"],
54
+ event: Extract<EcommerceEventName, "view_item" | "add_to_cart" | "begin_checkout" | "purchase">,
55
+ ecommerce: EcommercePayload,
56
+ options: { dedupeKey?: string; sessionDedupe?: boolean } = {},
57
+ ) {
58
+ if (typeof window === "undefined") return false
59
+ if (!googleAds.enabled || !googleAds.remarketing_enabled) return false
60
+ if (!readConsentPreferences()?.marketing) return false
61
+
62
+ const items = ecommerce.items
63
+ .map(dynamicRemarketingItem)
64
+ .filter((item): item is NonNullable<ReturnType<typeof dynamicRemarketingItem>> => Boolean(item))
65
+ if (!items.length) return false
66
+
67
+ const storage = options.sessionDedupe ? window.sessionStorage : window.localStorage
68
+ const dedupeKey = options.dedupeKey
69
+ ? `core:google-ads:remarketing:${event}:${options.dedupeKey}`
70
+ : null
71
+ if (dedupeKey && storage.getItem(dedupeKey) === "sent") return false
72
+
73
+ const payload = clean({
74
+ currency: normalizeCurrency(ecommerce.currency),
75
+ value: finite(ecommerce.value),
76
+ items,
77
+ })
78
+ const target = googleTarget()
79
+
80
+ if (googleAds.google_tag_manager_enabled) {
81
+ target.dataLayer.push({
82
+ event: `google_ads_${event}`,
83
+ google_ads_remarketing: payload,
84
+ })
85
+ if (dedupeKey) storage.setItem(dedupeKey, "sent")
86
+ return true
87
+ }
88
+
89
+ if (typeof target.gtag === "function") {
90
+ target.gtag("event", event, payload)
91
+ if (dedupeKey) storage.setItem(dedupeKey, "sent")
92
+ return true
93
+ }
94
+
95
+ return false
96
+ }
97
+
98
+ export function googleAdsRemarketingItemFromProduct(input: {
99
+ product: Record<string, unknown>
100
+ variant?: Record<string, unknown> | null
101
+ quantity?: number
102
+ }): EcommerceItem | null {
103
+ const variant = input.variant || null
104
+ const itemId = text(variant?.id)
105
+ if (!itemId) return null
106
+ const calculatedPrice = record(variant?.calculated_price)
107
+ const price = finite(calculatedPrice?.calculated_amount)
108
+ return clean({
109
+ item_id: itemId,
110
+ item_name: text(input.product.title) || "Termék",
111
+ item_variant: text(variant?.title),
112
+ price,
113
+ quantity: Math.max(1, Math.trunc(input.quantity || 1)),
114
+ }) as EcommerceItem
115
+ }
116
+
117
+ export function googleAdsRemarketingFromSource(source: Record<string, unknown>): EcommercePayload {
118
+ const items = sourceItems(source)
119
+ .map((line) => googleAdsRemarketingItemFromLine(line))
120
+ .filter((item): item is EcommerceItem => Boolean(item))
121
+ const currency = normalizeCurrency(source.currency_code)
122
+ const value = items.reduce(
123
+ (sum, item) => sum + (item.price || 0) * (item.quantity || 1),
124
+ 0,
125
+ )
126
+ return { currency, value: Number(value.toFixed(6)), items }
127
+ }
128
+
129
+ function googleAdsRemarketingItemFromLine(line: Record<string, unknown>): EcommerceItem | null {
130
+ const itemId = text(line.variant_id)
131
+ if (!itemId) return null
132
+ const quantity = Math.max(1, Math.trunc(finite(line.quantity) || 1))
133
+ const unitPrice =
134
+ finite(line.unit_price) ??
135
+ divide(finite(line.subtotal), finite(line.quantity)) ??
136
+ divide(finite(line.total), finite(line.quantity))
137
+ return clean({
138
+ item_id: itemId,
139
+ item_name: text(line.product_title) || text(line.title) || "Termék",
140
+ item_variant: text(line.variant_title),
141
+ price: unitPrice,
142
+ quantity,
143
+ }) as EcommerceItem
144
+ }
145
+
146
+ function dynamicRemarketingItem(item: EcommerceItem) {
147
+ const id = text(item.item_id)
148
+ if (!id) return null
149
+ return clean({
150
+ id,
151
+ item_id: id,
152
+ google_business_vertical: "retail",
153
+ price: finite(item.price),
154
+ quantity: finite(item.quantity),
155
+ })
156
+ }
157
+
158
+ function sourceItems(source: Record<string, unknown>) {
159
+ return Array.isArray(source.items)
160
+ ? source.items.filter(isRecord)
161
+ : []
162
+ }
163
+
164
+ function googleTarget() {
165
+ const target = window as typeof window & {
166
+ dataLayer?: Array<Record<string, unknown> | unknown[]>
167
+ gtag?: (...args: unknown[]) => void
168
+ }
169
+ target.dataLayer = target.dataLayer || []
170
+ return target as typeof target & { dataLayer: Array<Record<string, unknown> | unknown[]> }
171
+ }
172
+
173
+ function normalizeCurrency(value: unknown) {
174
+ return text(value).toUpperCase() || "HUF"
175
+ }
176
+
177
+ function divide(a?: number, b?: number) {
178
+ if (a === undefined || b === undefined || b === 0) return undefined
179
+ return a / b
180
+ }
181
+
182
+ function record(value: unknown): Record<string, unknown> | undefined {
183
+ return isRecord(value) ? value : undefined
184
+ }
185
+
186
+ function isRecord(value: unknown): value is Record<string, unknown> {
187
+ return Boolean(value && typeof value === "object" && !Array.isArray(value))
188
+ }
189
+
190
+ function clean<T extends Record<string, unknown>>(value: T) {
191
+ return Object.fromEntries(
192
+ Object.entries(value).filter(([, entry]) => entry !== undefined && entry !== null && entry !== ""),
193
+ )
194
+ }
195
+
49
196
  function text(value: unknown) {
50
197
  return typeof value === "string" || typeof value === "number"
51
198
  ? String(value).trim()
@@ -0,0 +1,26 @@
1
+ import { medusaFetch } from "./client"
2
+ import type { JsonRecord } from "./commerce"
3
+
4
+ export type ReorderSkippedItem = {
5
+ order_item_id: string
6
+ title: string
7
+ variant_id: string | null
8
+ quantity: number
9
+ reason: string
10
+ }
11
+
12
+ export type ReorderResult = {
13
+ cart: JsonRecord
14
+ added_count: number
15
+ skipped_items: ReorderSkippedItem[]
16
+ }
17
+
18
+ export function reorderOrder(orderId: string) {
19
+ return medusaFetch<ReorderResult>(
20
+ `/store/account/orders/${encodeURIComponent(orderId)}/reorder`,
21
+ {
22
+ method: "POST",
23
+ body: JSON.stringify({}),
24
+ },
25
+ )
26
+ }
@@ -14,6 +14,7 @@ export const STOREFRONT_ROUTES = {
14
14
  orders: "/hu/fiokom/rendelesek",
15
15
  resetPassword: "/hu/jelszo-visszaallitas",
16
16
  legal: "/hu/jogi",
17
+ documents: "/hu/dokumentumok",
17
18
  blog: "/hu/blog",
18
19
  knowledgeBase: "/hu/tudastar",
19
20
  cmsPages: "/hu/oldalak",
@@ -35,6 +36,7 @@ export const STOREFRONT_ROUTES = {
35
36
  orders: "/en/account/orders",
36
37
  resetPassword: "/en/reset-password",
37
38
  legal: "/en/legal",
39
+ documents: "/en/documents",
38
40
  blog: "/en/blog",
39
41
  knowledgeBase: "/en/knowledge-base",
40
42
  cmsPages: "/en/pages",
@@ -123,6 +125,7 @@ export function equivalentLocalePath(
123
125
  }
124
126
  if (pathname === source.resetPassword) return target.resetPassword
125
127
  if (pathname === source.legal) return target.legal
128
+ if (pathname === source.documents) return target.documents
126
129
  if (pathname === source.blog) return target.blog
127
130
  if (pathname.startsWith(`${source.blog}/`)) return `${target.blog}/${pathname.slice(source.blog.length + 1)}`
128
131
  if (pathname === source.knowledgeBase) return target.knowledgeBase
@@ -63,6 +63,7 @@ function legacyRedirect(pathname: string, locale: StorefrontLocale) {
63
63
  if (pathname.startsWith("/account/orders/")) return `${STOREFRONT_ROUTES[locale].orders}/${pathname.slice("/account/orders/".length)}`
64
64
  if (pathname === "/reset-password") return STOREFRONT_ROUTES[locale].resetPassword
65
65
  if (pathname === "/legal") return STOREFRONT_ROUTES[locale].legal
66
+ if (pathname === "/documents") return STOREFRONT_ROUTES[locale].documents
66
67
  if (pathname === "/blog") return STOREFRONT_ROUTES[locale].blog
67
68
  if (pathname.startsWith("/blog/")) return `${STOREFRONT_ROUTES[locale].blog}/${pathname.slice("/blog/".length)}`
68
69
  if (pathname === "/knowledge-base") return STOREFRONT_ROUTES[locale].knowledgeBase
@@ -114,6 +115,7 @@ function internalRewrite(pathname: string, locale: StorefrontLocale) {
114
115
  if (pathname.startsWith(`${routes.orders}/`)) return `/account/orders/${pathname.slice(routes.orders.length + 1)}`
115
116
  if (pathname === routes.resetPassword) return "/reset-password"
116
117
  if (pathname === routes.legal) return "/legal"
118
+ if (pathname === routes.documents) return "/documents"
117
119
  if (pathname === routes.blog) return "/blog"
118
120
  if (pathname.startsWith(`${routes.blog}/`)) return `/blog/${pathname.slice(routes.blog.length + 1)}`
119
121
  if (pathname === routes.knowledgeBase) return "/knowledge-base"