Cart Provider Overview
useCart
This hook exposes the context of CartProvider.
The context provides helper functions and mutations for managing the cart and checkout. You can refer to the following guides for examples on how to use them:
Example
import * as React from "react"
import { useCart } from "medusa-react"
const Cart = () => {
const handleClick = () => {
createCart.mutate({}) // create an empty cart
}
const { cart, createCart } = useCart()
return (
<div>
{createCart.isLoading && <div>Loading...</div>}
{!cart?.id && (
<button onClick={handleClick}>
Create cart
</button>
)}
{cart?.id && (
<div>Cart ID: {cart.id}</div>
)}
</div>
)
}
export default Cart
In the example above, you retrieve the createCart
mutation and cart
state object using the useCart
hook.
If the cart
is not set, a button is shown. When the button is clicked, the createCart
mutation is executed, which interacts with the backend and creates a new cart.
After the cart is created, the cart
state variable is set and its ID is shown instead of the button.
The example above does not store in the browser the ID of the cart created, so the cart’s data will be gone on refresh. You would have to do that using the browser’s Local Storage.
Returns
setCart
(cart: Omit<Cart, "refundable_amount" | "refunded_total">) => voidRequiredpay
UseMutationResult<Response<StoreCartsRes>, Error, StorePostCartsCartPaymentSessionReq, unknown>RequiredA mutation used to select a payment processor during checkout.
Using it is equivalent to using the useSetPaymentSession mutation.
pay
UseMutationResult<Response<StoreCartsRes>, Error, StorePostCartsCartPaymentSessionReq, unknown>RequiredcreateCart
UseMutationResult<Response<StoreCartsRes>, Error, undefined | StorePostCartReq, unknown>RequiredstartCheckout
UseMutationResult<Response<StoreCartsRes>, Error, void, unknown>RequiredcompleteCheckout
UseMutationResult<Response<StoreCompleteCartRes>, Error, void, unknown>RequiredupdateCart
UseMutationResult<Response<StoreCartsRes>, Error, StorePostCartsCartReq, unknown>RequiredA mutation used to update a cart’s details such as region, customer email, shipping address, and more.
Using it is equivalent to using the useUpdateCart mutation.
updateCart
UseMutationResult<Response<StoreCartsRes>, Error, StorePostCartsCartReq, unknown>RequiredaddShippingMethod
UseMutationResult<Response<StoreCartsRes>, Error, StorePostCartsCartShippingMethodReq, unknown>RequiredA mutation used to add a shipping method to the cart during checkout.
Using it is equivalent to using the useAddShippingMethodToCart mutation.
addShippingMethod
UseMutationResult<Response<StoreCartsRes>, Error, StorePostCartsCartShippingMethodReq, unknown>RequiredtotalItems
numberRequiredcart
Omit<Cart, "refundable_amount" | "refunded_total">The currently-used cart.
cart
Omit<Cart, "refundable_amount" | "refunded_total">CartProvider
CartProvider
makes use of some of the hooks already exposed by medusa-react
to perform cart operations on the Medusa backend.
You can use it to create a cart, start the checkout flow, authorize payment sessions, and so on.
It also manages one single global piece of state which represents a cart, exactly like the one created on your Medusa backend.
To use CartProvider
, you first have to insert it somewhere in your component tree below the MedusaProvider. Then, in any of the child components,
you can use the useCart hook exposed by medusa-react
to get access to cart operations and data.
Example
import { CartProvider, MedusaProvider } from "medusa-react"
import Storefront from "./Storefront"
import { QueryClient } from "@tanstack/react-query"
import React from "react"
const queryClient = new QueryClient()
function App() {
return (
<MedusaProvider
queryClientProviderProps={{ client: queryClient }}
baseUrl="http://localhost:9000"
>
<CartProvider>
<Storefront />
</CartProvider>
</MedusaProvider>
)
}
export default App
Parameters
Props of the provider.
Returns
Element
ElementRequiredCartProvider
makes use of some of the hooks already exposed by medusa-react
to perform cart operations on the Medusa backend.
You can use it to create a cart, start the checkout flow, authorize payment sessions, and so on.
It also manages one single global piece of state which represents a cart, exactly like the one created on your Medusa backend.
To use CartProvider
, you first have to insert it somewhere in your component tree below the MedusaProvider. Then, in any of the child components,
you can use the useCart hook exposed by medusa-react
to get access to cart operations and data.