1 ---
2 tags:
3 - cart
4 - storefront
5 products:
6 - cart
7 ---
8
9 import { CodeTabs, CodeTab } from "docs-ui"
10
11 export const metadata = {
12 title: `Checkout Step 1: Enter Email`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to add an email step to the checkout flow. This typically would be the first step of the checkout flow, but you can also change the steps of the checkout flow as you see fit.
18
19 When the user enters their email, use the [Update Cart API route](!api!/store/carts/update-a-cart) to update the cart with the email.
20
21 <Note title="Tip">
22
23 If the customer is logged-in, you can pre-fill the email with the customer's email.
24
25 </Note>
26
27 For example:
28
29 <Note title="Tip">
30
31 - This example uses the `useCart` hook defined in the [Cart React Context guide](../../cart/context/page.mdx).
32 - Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx).
33
34 </Note>
35
36 <CodeTabs group="store-request">
37 <CodeTab label="React" value="react">
38
39 export const highlights = [
40 ["4", "useCart", "The `useCart` hook was defined in the Cart React Context documentation."],
41 ["14", "TODO", "Cart must have at least one item. If not, redirect to another page."],
42 ]
43
44 ```tsx highlights={highlights}
45 "use client" // include with Next.js 13+
46
47 import { useEffect, useState } from "react"
48 import { useCart } from "@/providers/cart"
49 import { sdk } from "@/lib/sdk"
50
51 export default function CheckoutEmailStep() {
52 const { cart, setCart } = useCart()
53 const [email, setEmail] = useState("")
54 const [loading, setLoading] = useState(false)
55
56 useEffect(() => {
57 if (cart && !cart.items?.length) {
58 // TODO redirect to another path
59 }
60 }, [cart])
61
62 const updateCartEmail = (
63 e: React.MouseEvent<HTMLButtonElement, MouseEvent>
64 ) => {
65 if (!cart || !email.length) {
66 return
67 }
68
69 e.preventDefault()
70 setLoading(true)
71
72 sdk.store.cart.update(cart.id, {
73 email,
74 })
75 .then(({ cart: updatedCart }) => {
76 setCart(updatedCart)
77 })
78 .finally(() => setLoading(false))
79 }
80
81 return (
82 <div>
83 {!cart && <span>Loading...</span>}
84 <input
85 type="email"
86 placeholder="Email"
87 value={email}
88 disabled={!cart}
89 onChange={(e) => setEmail(e.target.value)}
90 />
91 <button
92 disabled={!cart || !email || loading}
93 onClick={updateCartEmail}
94 >
95 Set Email
96 </button>
97 </div>
98 )
99 }
100 ```
101
102 </CodeTab>
103 <CodeTab label="JS SDK" value="js-sdk">
104
105 ```ts
106 const cartId = localStorage.getItem("cart_id")
107
108 sdk.store.cart.update(cart.id, {
109 email,
110 })
111 .then(({ cart }) => {
112 // use cart...
113 console.log(cart)
114 })
115 ```
116
117 </CodeTab>
118 </CodeTabs>
119
120 After the customer enters and submits their email, you send a request to the [Update Cart API route](!api!/store/carts/update-a-cart) passing it the email in the request body.
121
122 Notice that if the cart doesn't have items, you should redirect to another page as the checkout requires at least one item in the cart. Redirecting to another page is not covered in this guide as this depends on the storefront framework you're using.