Skip to main content
Skip to main content

Swaps

Queries and Mutations listed here are used to send requests to the Store Swap API Routes.

A swap is created by a customer or an admin to exchange an item with a new one. Creating a swap implicitely includes creating a return for the item being exchanged.

Related Guide: How to create a swap in a storefront

Mutations

useCreateSwap

This hook creates a Swap for an Order. This will also create a return and associate it with the swap. If a return shipping option is specified, the return will automatically be fulfilled. To complete the swap, you must use the useCompleteCart hook passing it the ID of the swap's cart.

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 swap creation or the request is interrupted for any reason, the swap creation can be retried by passing the idempotency key in the Idempotency-Key header.

Example

import React from "react"
import { useCreateSwap } from "medusa-react"

type Props = {
orderId: string
}

type CreateData = {
return_items: {
item_id: string
quantity: number
}[]
additional_items: {
variant_id: string
quantity: number
}[]
return_shipping_option: string
}

const CreateSwap = ({
orderId
}: Props) => {
const createSwap = useCreateSwap()
// ...

const handleCreate = (
data: CreateData
) => {
createSwap.mutate({
...data,
order_id: orderId
}, {
onSuccess: ({ swap }) => {
console.log(swap.id)
}
})
}

// ...
}

export default CreateSwap

Mutation Function Parameters

StorePostSwapsReqStorePostSwapsReqRequired
The details of the swap to create.

Mutation Function Returned Data

StoreSwapsResStoreSwapsResRequired
The swap's details.

Queries

useCartSwap

This hook retrieves a Swap's details by the ID of its cart.

Example

import React from "react"
import { useCartSwap } from "medusa-react"

type Props = {
cartId: string
}

const Swap = ({ cartId }: Props) => {
const {
swap,
isLoading,
} = useCartSwap(cartId)

return (
<div>
{isLoading && <span>Loading...</span>}
{swap && <span>{swap.id}</span>}

</div>
)
}

export default Swap

Hook Parameters

cartIdstringRequired
The ID of the swap's cart.

Query Returned Data

swapSwapRequired
Swap details.
Was this section helpful?