|
5 | 5 | import StoreProduct from "$components/StoreProduct.svelte" |
6 | 6 | import Countdown from "svelte-countdown" |
7 | 7 | import { fade, slide } from "svelte/transition" |
| 8 | + import { getProductCreemIds, isDiscountApplicableToProduct } from "$lib/storeDiscount.js" |
8 | 9 | let { data } = $props() |
9 | 10 | let currentDate = $state(new Date().toISOString()) |
10 | 11 | $effect(() => { |
|
23 | 24 | return false |
24 | 25 | } |
25 | 26 |
|
26 | | - function extractCreemProductId(url) { |
27 | | - if (!url || typeof url !== "string") return null |
28 | | - const match = url.match(/\/payment\/(prod_[^/?#]+)/) |
29 | | - return match ? match[1] : null |
30 | | - } |
31 | | -
|
32 | | - function getCheckoutProductIds(packages) { |
33 | | - if (!packages) return [] |
34 | | - const checkoutRow = packages.slice(1).find((row) => row[0] === "checkout") |
35 | | - if (!checkoutRow) return [] |
36 | | - return checkoutRow.slice(1).map(extractCreemProductId).filter(Boolean) |
37 | | - } |
38 | | -
|
39 | | - function isDiscountApplicableToProduct(discount, checkoutProductIds) { |
40 | | - const appliesTo = discount?.data?.attributes?.applies_to_products |
41 | | - if (!appliesTo?.length) return true |
42 | | - return appliesTo.some((id) => checkoutProductIds.includes(id)) |
43 | | - } |
44 | | -
|
45 | 27 | let discount = $state(null) |
46 | 28 | const fetchDiscount = (async () => { |
47 | 29 | // Fetch both discount types |
|
74 | 56 | function getProductDiscount(product) { |
75 | 57 | if (!discount?.data?.attributes?.expires_at) return null |
76 | 58 | if (new Date(discount.data.attributes.expires_at).toISOString() <= currentDate) return null |
77 | | - const checkoutProductIds = getCheckoutProductIds(product.packages) |
78 | | - if (!isDiscountApplicableToProduct(discount, checkoutProductIds)) return null |
| 59 | + const productIds = getProductCreemIds(product) |
| 60 | + if (!isDiscountApplicableToProduct(discount, productIds)) return null |
79 | 61 | return discount.data.attributes |
80 | 62 | } |
81 | 63 |
|
|
0 commit comments