Skip to main content

How to Use Sales Channels in Storefronts

In this document, you’ll learn how to use sales channels in your storefront.

Overview

In your storefront, you can filter products by sales channels. You can also associate carts with a sales channel.

This guide explains how to perform these operations using the Storefront APIs.

Prerequisites

Medusa Components

It's assumed that you already have a Medusa server installed and set up. If not, you can follow our quickstart guide to get started.

It is also assumed you already have a storefront set up. It can be a custom storefront or one of Medusa’s storefronts. If you don’t have a storefront set up, you can install either the Next.js or Gatsby storefronts.

JS Client

This guide includes code snippets to send requests to your Medusa server using Medusa’s JS Client and JavaScript’s Fetch API.

If you follow the JS Client code blocks, it’s assumed you already have Medusa’s JS Client installed and have created an instance of the client.

Filter Products by Sales Channel

To filter products by a specific sales channel, pass the

Copy to Clipboard
query parameter to the List Products endpoint:

Report Incorrect Code
Report Incorrect CodeReport Incorrect Code
Copy to Clipboard
medusa.products.list({
sales_channel_id: [
salesChannelId
]
})
.then(({ products, limit, offset, count }) => {
console.log(products.length);
});
Copy to Clipboard

The

Copy to Clipboard
query parameter is an array of sales channel IDs. You can pass more than one sales channel.

The request returns an array of products. These are the products that are available in the sales channel.

Associate a Cart with a Sales Channel

When Creating a Cart

To associate a sales channel with a cart while creating it, you can pass the

Copy to Clipboard
request body parameter with the ID of the sales channel:

Report Incorrect Code
Report Incorrect CodeReport Incorrect Code
Copy to Clipboard
medusa.carts.create({
sales_channel_id: salesChannelId
})
.then(({cart}) => {
console.log(cart.id);
});
Copy to Clipboard

The request returns the created cart.

Updating an Existing Cart

You can update the sales channel of an existing cart by passing the

Copy to Clipboard
request body parameter with the ID of the sales channel:

Report Incorrect Code
Report Incorrect CodeReport Incorrect Code
Copy to Clipboard
medusa.carts.update(cartId, {
sales_channel_id: salesChannelId
})
.then(({cart}) => {
console.log(cart.id);
});
Copy to Clipboard

The request returns the updated cart.

What’s Next 🚀