Carts
Queries and Mutations listed here are used to send requests to the Store Cart API Routes.
A cart is a virtual shopping bag that customers can use to add items they want to purchase. A cart is then used to checkout and place an order.
The hooks listed have general examples on how to use them, but it's highly recommended to use the CartProvider provider and the useCart hook to manage your cart and access the current cart across your application.
Related Guide: How to implement cart functionality in your storefront.
Mutations
useCreateCart
This hook creates a Cart. Although optional, specifying the cart's region and sales channel can affect the cart's pricing and the products that can be added to the cart respectively.
So, make sure to set those early on and change them if necessary, such as when the customer changes their region.
If a customer is logged in, make sure to pass its ID or email within the cart's details so that the cart is attached to the customer.
Example
import React from "react"
import { useCreateCart } from "medusa-react"
type Props = {
regionId: string
}
const Cart = ({ regionId }: Props) => {
const createCart = useCreateCart()
const handleCreate = () => {
createCart.mutate({
region_id: regionId
// creates an empty cart
}, {
onSuccess: ({ cart }) => {
console.log(cart.items)
}
})
}
// ...
}
export default Cart
Mutation Function Parameters
Mutation Function Returned Data
The cart's details.
useUpdateCart
This hook updates a Cart's details. If the cart has payment sessions and the region was not changed, the payment sessions are updated. The cart's totals are also recalculated.
Example
import React from "react"
import { useUpdateCart } from "medusa-react"
type Props = {
cartId: string
}
const Cart = ({ cartId }: Props) => {
const updateCart = useUpdateCart(cartId)
const handleUpdate = (
email: string
) => {
updateCart.mutate({
email
}, {
onSuccess: ({ cart }) => {
console.log(cart.email)
}
})
}
// ...
}
export default Cart
Hook Parameters
cartId
stringRequiredMutation Function Parameters
The details to update of the cart.
Mutation Function Returned Data
The cart's details.
useCompleteCart
This hook completes a cart and place an order or create a swap, based on the cart's type. This includes attempting to authorize the cart's payment.
If authorizing the payment requires more action, the cart will not be completed and the order will not be placed or the swap will not be created.
An idempotency key will be generated if none is provided in the header Idempotency-Key
and added to
the response. If an error occurs during cart completion or the request is interrupted for any reason, the cart completion can be retried by passing the idempotency
key in the Idempotency-Key
header.
Example
import React from "react"
import { useCompleteCart } from "medusa-react"
type Props = {
cartId: string
}
const Cart = ({ cartId }: Props) => {
const completeCart = useCompleteCart(cartId)
const handleComplete = () => {
completeCart.mutate(void 0, {
onSuccess: ({ data, type }) => {
console.log(data.id, type)
}
})
}
// ...
}
export default Cart