|
| 1 | +# Guest Checkout SPA Example |
| 2 | + |
| 3 | +This example showcases how to implement a **guest checkout** flow with **Elastic Path Commerce Cloud** in a Single-Page Application (SPA) written in React + Vite. |
| 4 | + |
| 5 | +> **Heads-up:** This project **extends** the [Shopping Cart Management Example](../spa-cart). We only cover the additional checkout logic here—cart creation, item manipulation, promotions, etc. are documented in that README. |
| 6 | +
|
| 7 | +Key capabilities demonstrated: |
| 8 | + |
| 9 | +1. **Cart Persistence** – automatically creates / retrieves a cart via the Shopper SDK. |
| 10 | +2. **Guest Checkout Form** – captures billing & shipping addresses in a single component with: |
| 11 | + • _Same as billing_ toggle |
| 12 | + • Optional _Delivery Instructions_ field (shipping only) |
| 13 | +3. **Order Creation** – converts a cart to an order using `checkoutApi` _without_ requiring a customer account or payment integration. |
| 14 | +4. **Event-driven Cart Refresh** – broadcasts a `cart:updated` custom event so any part of the app can update after checkout. |
| 15 | + |
| 16 | +> The example purposefully omits payment collection to keep the focus on the guest checkout mechanics. After an order is created the cart is cleared and a lightweight confirmation screen is displayed. |
| 17 | +
|
| 18 | +--- |
| 19 | + |
| 20 | +## Project Structure |
| 21 | + |
| 22 | +``` |
| 23 | +spa-guest-checkout/ |
| 24 | +├── index.html # Vite entry point |
| 25 | +├── src/ |
| 26 | +│ ├── App.tsx # Routes between Cart ↔︎ Checkout |
| 27 | +│ ├── components/ |
| 28 | +│ │ ├── CartView.tsx # Cart listing & promo code support |
| 29 | +│ │ └── CheckoutView.tsx # Guest checkout form (billing + shipping) |
| 30 | +│ └── auth/CartProvider.tsx # Initializes / persists cart |
| 31 | +└── README.md # ← you are here |
| 32 | +``` |
| 33 | + |
| 34 | +## How It Works |
| 35 | + |
| 36 | +### 1. Cart Initialization |
| 37 | + |
| 38 | +`initializeCart()` from `@epcc-sdk/sdks-shopper` is executed on app load. If a cart already exists in local-storage its ID is reused, otherwise a new cart is created. |
| 39 | + |
| 40 | +```tsx |
| 41 | +// src/auth/CartProvider.tsx |
| 42 | +useEffect(() => { |
| 43 | + initializeCart() |
| 44 | +}, []) |
| 45 | +``` |
| 46 | + |
| 47 | +### 2. Collecting Guest Details |
| 48 | + |
| 49 | +`CheckoutView` keeps a nested `form` state: |
| 50 | + |
| 51 | +```ts |
| 52 | +{ |
| 53 | + billing: { firstName, lastName, email, line1, city, region, postcode, country }, |
| 54 | + shipping: { firstName, lastName, line1, city, region, postcode, country, instructions }, |
| 55 | + sameAsBilling: true |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +If **Same as billing** is checked the shipping fields are hidden and the billing address is re-used. |
| 60 | + |
| 61 | +### 3. Converting Cart → Order |
| 62 | + |
| 63 | +Upon submit the component calls: |
| 64 | + |
| 65 | +```ts |
| 66 | +await checkoutApi({ |
| 67 | + path: { cartID }, |
| 68 | + body: { |
| 69 | + data: { |
| 70 | + customer: { email, name }, |
| 71 | + billing_address: { ... }, |
| 72 | + shipping_address: { ... } |
| 73 | + } |
| 74 | + } |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +The resulting order ID, total, and status are shown on a confirmation screen. |
| 79 | + |
| 80 | +### 4. Clearing Cart & Refreshing UI |
| 81 | + |
| 82 | +After a successful checkout: |
| 83 | + |
| 84 | +```ts |
| 85 | +window.dispatchEvent(new Event("cart:updated")) |
| 86 | +``` |
| 87 | + |
| 88 | +Any listeners (e.g. `CartView`) reload their data ensuring an empty cart is reflected immediately. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Running the Example Locally |
| 93 | + |
| 94 | +1. **Install deps** (from the repo root): |
| 95 | + |
| 96 | +```bash |
| 97 | +pnpm i # or npm install / yarn |
| 98 | +``` |
| 99 | + |
| 100 | +2. **Set environment variables** – create a `.env` file in `examples/spa-guest-checkout` (or export in your shell): |
| 101 | + |
| 102 | +``` |
| 103 | +VITE_APP_EPCC_ENDPOINT_URL=https://YOUR_EP_DOMAIN.elasticpath.com |
| 104 | +VITE_APP_EPCC_CLIENT_ID=YOUR_CLIENT_ID |
| 105 | +``` |
| 106 | + |
| 107 | +3. **Start Vite dev server**: |
| 108 | + |
| 109 | +```bash |
| 110 | +pnpm --filter spa-guest-checkout dev |
| 111 | +``` |
| 112 | + |
| 113 | +## Key Components |
| 114 | + |
| 115 | +- `CartProvider`: Initializes the cart on application load |
| 116 | +- `CartView`: Displays cart contents and manages cart operations |
| 117 | +- Cart utilities from SDK: `initializeCart`, `getCartId` |
| 118 | + |
| 119 | +## Getting Started |
| 120 | + |
| 121 | +Follow the setup instructions in the [SPA Authentication Example](../spa-authentication) README for authentication configuration. |
| 122 | + |
| 123 | +### Environment Variables |
| 124 | + |
| 125 | +This example uses the same environment variables as the SPA Authentication example: |
| 126 | + |
| 127 | +``` |
| 128 | +VITE_APP_EPCC_ENDPOINT_URL=your_endpoint_url |
| 129 | +VITE_APP_EPCC_CLIENT_ID=your_client_id |
| 130 | +``` |
| 131 | + |
| 132 | +## Learn More |
| 133 | + |
| 134 | +- [Cart Management with Elastic Path](https://elasticpath.dev/docs/api/carts/cart-management) |
| 135 | +- [Promotions in Elastic Path](https://elasticpath.dev/docs/api/promotions-builder/rule-promotions-api) |
0 commit comments