Skip to content

Commit 135e2b8

Browse files
committed
feat: dynamically show product checkout buttons
Use useProducts hook to fetch available products and conditionally render product checkout buttons. Single product button shows product name, multi-product button appears only when 2+ products exist. Gracefully falls back to amount-only checkout if no products.
1 parent 95a2d57 commit 135e2b8

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

mdk-nextjs-demo/app/page.tsx

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useCheckout } from "@moneydevkit/nextjs";
3+
import { useCheckout, useProducts } from "@moneydevkit/nextjs";
44
import Link from "next/link";
55
import { useMemo, useState } from "react";
66

@@ -21,6 +21,7 @@ export default function HomePage() {
2121
const [customerName, setCustomerName] = useState("Satoshi Nakamoto");
2222
const [note, setNote] = useState("Fast IBD snapshot with hosted checkout.");
2323
const { navigate, isNavigating } = useCheckout();
24+
const { products, isLoading: productsLoading } = useProducts();
2425

2526
const metadata = useMemo(
2627
() => ({
@@ -43,6 +44,26 @@ export default function HomePage() {
4344
});
4445
};
4546

47+
const handleProductCheckout = () => {
48+
if (products.length === 0) return;
49+
navigate({
50+
// Single product checkout - uses first available product
51+
productId: products[0].id,
52+
metadata,
53+
checkoutPath: "/checkout",
54+
});
55+
};
56+
57+
const handleMultiProductCheckout = () => {
58+
if (products.length < 2) return;
59+
navigate({
60+
// Multiple products checkout - uses first two available products
61+
products: [products[0].id, products[1].id],
62+
metadata,
63+
checkoutPath: "/checkout",
64+
});
65+
};
66+
4667
return (
4768
<main className="page">
4869
<div className="container">
@@ -95,8 +116,30 @@ export default function HomePage() {
95116
disabled={isNavigating}
96117
data-test="start-checkout"
97118
>
98-
{isNavigating ? "Creating checkout…" : "Launch checkout"}
119+
{isNavigating ? "Creating checkout…" : "Launch checkout (Amount)"}
99120
</button>
121+
{products.length >= 1 && (
122+
<button
123+
type="button"
124+
className="button"
125+
onClick={handleProductCheckout}
126+
disabled={isNavigating || productsLoading}
127+
style={{ marginTop: "0.5rem", background: "#2563eb" }}
128+
>
129+
{isNavigating ? "Creating checkout…" : `Launch checkout (${products[0].name})`}
130+
</button>
131+
)}
132+
{products.length >= 2 && (
133+
<button
134+
type="button"
135+
className="button"
136+
onClick={handleMultiProductCheckout}
137+
disabled={isNavigating || productsLoading}
138+
style={{ marginTop: "0.5rem", background: "#7c3aed" }}
139+
>
140+
{isNavigating ? "Creating checkout…" : "Launch checkout (2 Products)"}
141+
</button>
142+
)}
100143
<p className="hint">
101144
We create a checkout session with the values above and redirect to
102145
{" /checkout/[id] "} using <code>useCheckout</code>.

0 commit comments

Comments
 (0)