diff --git a/src/lib/components/checkout/OrderConfirmationItem.svelte b/src/lib/components/checkout/OrderConfirmationItem.svelte index 67381a4..633d591 100644 --- a/src/lib/components/checkout/OrderConfirmationItem.svelte +++ b/src/lib/components/checkout/OrderConfirmationItem.svelte @@ -2,7 +2,7 @@ import { formatCurrency } from '$lib/utils/currency'; export let imgAlt = ''; export let imgSrc = ''; - // export let itemCategoryDescription = ''; + export let itemCategoryDescription = ''; export let itemName = ''; export let price = 0; export let quantity = 0; @@ -20,11 +20,11 @@
{itemName}
- +

{ return { products: products as ProductWithPrices[], count }; }; +const getProductById = async (productId: string) => { + const { data: product, error } = await supabaseAdmin + .from('products') + .select('*') + .eq('id', productId) + .single(); + + if (error) { + throw new Error(`Error fetching product: ${error.message}`); + } + + return product as Product; +}; + export { upsertProductRecord, upsertPriceRecord, @@ -479,6 +493,7 @@ export { deductCredits, getUserCredits, getActiveProductsWithPrices, + getProductById, upsertCustomerToSupabase, getStripeCustomerId }; diff --git a/src/routes/checkout/success/+page.ts b/src/routes/checkout/success/+page.server.ts similarity index 66% rename from src/routes/checkout/success/+page.ts rename to src/routes/checkout/success/+page.server.ts index 19e3544..8d1ee5e 100644 --- a/src/routes/checkout/success/+page.ts +++ b/src/routes/checkout/success/+page.server.ts @@ -1,8 +1,8 @@ -import type { PageLoad } from './$types'; import { redirect } from '@sveltejs/kit'; import Stripe from 'stripe'; +import { getProductById } from '$lib/utils/supabase/admin'; -export const load: PageLoad = async ({ fetch }) => { +export const load = async ({ fetch }) => { const response = await fetch('/api/checkout/status'); if (!response.ok) { console.error('Failed to fetch checkout status'); @@ -18,6 +18,12 @@ export const load: PageLoad = async ({ fetch }) => { throw redirect(303, '/checkout/cancelled'); } + for (const lineItem of lineItems as LineItemForCheckout[]) { + const product = await getProductById(lineItem.price?.product.toString() ?? ''); + lineItem.imgSrc = product.images ? product.images[0] : ''; + lineItem.productDescription = product.description ?? ''; + } + // Save the stripe customer id in the customers table const stripeCustomerId = checkoutSession.customer as string; const createCustomerResponse = await fetch( @@ -29,7 +35,11 @@ export const load: PageLoad = async ({ fetch }) => { return { checkoutSession, - lineItems, - paymentStatus + lineItems }; }; + +type LineItemForCheckout = Stripe.LineItem & { + imgSrc: string; + productDescription: string; +}; diff --git a/src/routes/checkout/success/+page.svelte b/src/routes/checkout/success/+page.svelte index b4ff15f..40cd632 100644 --- a/src/routes/checkout/success/+page.svelte +++ b/src/routes/checkout/success/+page.svelte @@ -11,7 +11,7 @@ localBasket = value; }); - let { checkoutSession, lineItems, paymentStatus } = data; + let { checkoutSession, lineItems } = data; onDestroy(unsubscribe); @@ -64,13 +64,15 @@ >
-->
- {#if lineItems.length == 0} + {#if lineItems !== undefined && lineItems.length == 0}

No items in the basket

- {:else} + {:else if lineItems !== undefined} {#each lineItems as item (item.id)}