cart - JS SDK Store Reference

This documentation provides a reference to the sdk.store.cart set of methods used to send requests to Medusa's Store API routes.

Related guides: How to implement carts in the storefront.

create#

This method creates a cart. It sends a request to the Create Cart API route.

Related guide: How to create a cart in the storefront.

Example#

Code
1sdk.store.cart.create({2  region_id: "reg_123"3})4.then(({ cart }) => {5  console.log(cart)6})

Parameters#

The details of the cart to create.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The created cart.

update#

This method updates a cart. It sends a request to the Update Cart API route.

Related guide: How to update a cart in the storefront.

Example#

Code
1sdk.store.cart.update("cart_123", {2  region_id: "reg_123"3})4.then(({ cart }) => {5  console.log(cart)6})

Parameters#

idstring
The cart's ID.
The data to update in the cart.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The updated cart.

retrieve#

This method retrieves a cart by its ID. It sends a request to the Get Cart API route.

Related guide: How to retrieve a cart in the storefront.

Example#

To retrieve a cart by its ID:

Code
1sdk.store.cart.retrieve("cart_123")2.then(({ cart }) => {3  console.log(cart)4})

To specify the fields and relations to retrieve:

Code
1sdk.store.cart.retrieve("cart_123", {2  fields: "id,*items"3})4.then(({ cart }) => {5  console.log(cart)6})

Learn more about the fields property in the API reference.

Parameters#

idstring
The cart's ID.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The cart's details.

createLineItem#

This methods adds a product variant to the cart as a line item. It sends a request to the Add Line Item API route.

Related guide: How to manage a cart's line items in the storefront.

Example#

Code
1sdk.store.cart.createLineItem("cart_123", {2  variant_id: "variant_123",3  quantity: 14})5.then(({ cart }) => {6  console.log(cart)7})

Parameters#

cartIdstring
The cart's ID.
The details of the item to add.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The cart's details.

updateLineItem#

This method updates a line item in a cart. It sends a request to the Update Line Item API route.

Related guide: How to manage a cart's line items in the storefront.

Example#

Code
1sdk.store.cart.updateLineItem(2  "cart_123",3  "li_123",4  {5    quantity: 16  }7)8.then(({ cart }) => {9  console.log(cart)10})

Parameters#

cartIdstring
The cart's ID.
lineItemIdstring
The line item's ID.
The data to update.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The cart's details.

deleteLineItem#

This method deletes a line item from a cart. It sends a request to the Remove Line Item API route.

Related guide: How to manage a cart's line items in the storefront.

Example#

Code
1sdk.store.cart.deleteLineItem(2  "cart_123",3  "li_123"4)5.then(({ deleted, parent: cart }) => {6  console.log(deleted, cart)7})

Parameters#

cartIdstring
The cart's ID.
lineItemIdstring
The item's ID.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

The deletion's details.

addShippingMethod#

This method adds a shipping method to a cart. It sends a request to the Add Shipping Method API routes.

Related guide: Implement shipping step during checkout.

Example#

Code
1sdk.store.cart.addShippingMethod("cart_123", {2  option_id: "so_123",3  data: {4    // custom data for fulfillment provider.5  }6})7.then(({ cart }) => {8  console.log(cart)9})

Parameters#

cartIdstring
The cart's ID.
The shipping method's details.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The cart's details.

complete#

This method completes a cart and places the order. It's the last step of the checkout flow. The method sends a request to the Complete Cart API route.

Related guide: Learn how to complete cart in checkout flow.

Example#

Code
1sdk.store.cart.complete("cart_123")2.then((data) => {3  if(data.type === "cart") {4    // an error occurred5    console.log(data.error, data.cart)6  } else {7    // order placed successfully8    console.log(data.order)9  }10})

Parameters#

cartIdstring
The cart's ID.
querySelectParamsOptional
Configure the fields to retrieve in the created order.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

The order's details, if it was placed successfully. Otherwise, the cart is returned with an error.

transferCart#

This method updates the customer of a cart.

Example#

Code
1sdk.store.cart.transferCart("cart_123")2.then(({ cart }) => {3  console.log(cart)4})

Parameters#

idstring
The cart's ID.
querySelectParamsOptional
Configure the fields to retrieve in the cart.
headersClientHeadersOptional
Headers to pass in the request.

Returns#

PromisePromise<StoreCartResponse>
The updated cart.
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break