Initialize Checkout Session with Items
Learn how to programmatically create a new checkout session with line items using theinit() function from useCheckout().
Use Case: This is useful when you want to create a checkout without a pre-existing
checkoutToken, such as:- Landing pages that add products directly to checkout
- BOGO funnels that start with a specific bundle
- Custom product selectors that initialize checkout on-the-fly
Basic Example
Complete Example with Store ID
With Static Resources (CRM-Configured Variants)
This example shows how to use variant IDs from funnel static resources:Multiple Line Items
With Specific Price ID (Subscription)
Key Points
When to use init()
When to use init()
Use
init() when:- ✅ No
checkoutTokenis provided in the URL - ✅ You want to programmatically create a checkout
- ✅ Building a landing page that adds products to cart
- ✅ Creating custom bundle selectors
init() when:- ❌ You have a
checkoutTokenfrom the URL - ❌ The checkout already exists (checkout data loaded)
Preventing duplicate initialization
Preventing duplicate initialization
Always use a ref to prevent multiple init calls:
Wait for dependencies
Wait for dependencies
Make sure to wait for:
- Funnel to be initialized (
isInitialized) - Context to be available (
context) - Static resources to load (if using them)
Error handling
Error handling
Always handle initialization errors:
