The SDK ecom
module allows you to take advantage of Wix eCommerce services in a site or app you build on any platform. This means you can handle shopping carts and checkout flows for your Wix Store products. This tutorial shows you how to create a React component with a complete eCommerce flow. The component lists products from a Wix store, allows visitors to add products to a shopping cart, and redirects visitors to a checkout page. The tutorial also demonstrates how to maintain cart sessions.
The tutorial is based on the Wix Headless example site. You can test out the live example site, or fork the site's code repo to use as a starting point for your own site or app.
This implementation focuses on simplicity and understandability, rather than feature richness, performance or completeness. For details about additional functionality, see Wix eCommerce in the API Reference. Looking for a more comprehensive example site integrating Wix Headless APIs for managing an online store? Check out our starter templates.
Note: The code in this tutorial is written in JSX, but you can use the SDK in any JavaScript environment.
Implementing the eCommerce flow includes the following steps:
- Set up the Wix Headless environment.
- Import the SDK modules and create an SDK client.
- Create a React component and state variables.
- Fetch your Wix Store products.
- Handle the cart session.
- Implement the checkout flow.
- Add the
useEffect
hook. - Render the UI.
Before using the SDK, there are a few things you need to set up on your Wix account and in your external site or app's coding environment.
To set up the Wix Headless environment, follow these steps:
- If you haven't already, create a project.
When prompted to add functionalities to your new project, select eCommerce. - Set up authorization for your site by creating and configuring an OAuth app.
- Set a domain to be used by Wix-managed pages.
- Set a domain that Wix can redirect to after completing a Wix-managed process.
- Install the API client and relevant SDK module packages by running the following commands:
For NPM:
For Yarn:Copy Codenpm install @wix/sdknpm install @wix/storesnpm install @wix/ecomnpm install @wix/redirectsCopy Codeyarn add @wix/sdkyarn add @wix/storesyarn add @wix/ecomyarn add @wix/redirects - Install the
react
package to handle UI rendering and thejs-cookie
package to handle session cookies. Run the following commands:
For NPM:
For Yarn:Copy Codenpm install reactnpm install js-cookieCopy Codeyarn add reactyarn add js-cookie
The next step is to set up your code file to run the SDK functions. To set up the code file, follow these steps:
-
Add the following import statements to the top of your code file:
Copy Codeimport Cookies from 'js-cookie';import { useEffect, useState } from 'react';import { createClient, OAuthStrategy } from '@wix/sdk';import { products } from '@wix/stores';import { currentCart } from '@wix/ecom';import { redirects } from '@wix/redirects'; -
Create an SDK client by adding the following code to your code file. Replace the value for
clientId
with your OAuth app's client ID. You can find the ID in your project's Headless Settings menu.
The value fortokens
is the'session'
cookie on the visitor's browser. It's used to make calls to the Wix API. This way, you can maintain previous visitor sessions. For information about managing cookies, see Session Token Management.Copy Codeconst myWixClient = createClient({modules: { products, currentCart, redirects },auth: OAuthStrategy({ clientId: `<YOUR-CLIENT-ID>` }),tokens: JSON.parse(Cookies.get('session') || null),});
The logic for our eCommerce flow is contained in a React component called Store
. To create the component, follow these steps:
- Add the following function component to your code file:
Copy Codeexport default function Store() {}
- Define state variables by adding the following code to the
Store
component:
TheproductList
variable stores the list of products from your project's Wix Store. Thecart
variable stores the current cart session.Copy Codeconst [productList, setProductList] = useState([]);const [cart, setCart] = useState({});
Define a function to fetch your Wix Store products by adding the following code to the Store
component. This function runs when the component is first rendered. The function uses the queryProducts()
function from the SDK's Stores module to query your store's products.
Copy Code
async function fetchProducts() {const productList = await myWixClient.products.queryProducts().find();setProductList(productList.items);}
Add the following 3 functions for handling cart sessions to the Store
component. Each of these functions use functions from the ecom
CurrentCart
submodule.
fetchCart()
- Fetches the current cart session, if there is one, using thegetCurrentCart()
function. Sets the browser's session cookie to the SDK client's current access tokens.fetchCart()
runs when the component is first rendered.Copy Codeasync function fetchCart() {try {setCart(await myWixClient.currentCart.getCurrentCart());} catch {}}addToCart()
- Adds a product to the cart using theaddToCurrentCart()
function.addToCart()
runs when a product rendered in the UI is clicked.Copy Codeasync function addToCart(product) {const options = product.productOptions.reduce((selected, option) => ({...selected,[option.name]: option.choices[0].description,}),{});const { cart } = await myWixClient.currentCart.addToCurrentCart({lineItems: [{catalogReference: {appId: '1380b703-ce81-ff05-f115-39571d94dfcd',catalogItemId: product._id,options: { options },},quantity: 1,},],});setCart(cart);}clearCart()
- Clears the current cart session using thedeleteCurrentCart()
function.clearCart()
runs when a Clear Cart button in the rendered UI is clicked.Copy Codeasync function clearCart() {const { cart } = await myWixClient.currentCart.deleteCurrentCart();setCart(cart);}
Add a function called createRedirect()
to the Store
component. This function runs when a Checkout button in the rendered UI is clicked. The function
does the following:
- Uses the
createCheckoutFromCurrentCart()
function to create a checkout for the products currently in the cart and retrieve acheckoutId
. - Uses the
createRedirectSession()
function with the retrievedcheckoutId
to retrieve anecom
checkout URL. This is the URL for a Wix-managed checkout page that the visitor can use to complete the checkout process. - Redirects the browser to the checkout URL. If the checkout is successful, the visitor is redirected to a Wix thank you page. After the thank you page, or if the checkout fails, the visitor is redirected to the URL passed in the
postFlowUrl
property when calling thecreateRedirectSession()
function.
Copy Code
async function createRedirect() {const { checkoutId } =await myWixClient.currentCart.createCheckoutFromCurrentCart({channelType: currentCart.ChannelType.WEB,});const redirect = await myWixClient.redirects.createRedirectSession({ecomCheckout: { checkoutId },callbacks: { postFlowUrl: window.location.href },});window.location = redirect.redirectSession.fullUrl;}
Notes:
- When redirecting from a Wix checkout page to an external site, Wix validates that the provided redirect URL is registered under an allowed domain for the given client ID. Therefore, you must add your domain to the OAuth app.
- A visitor can choose to log in to your site or app during the Wix checkout process.
Add the following code to the Store
component to run the fetchProducts()
fetchCart()
functions after the component is rendered. This ensures that your product data, any existing cart data, and member data are available when the component mounts.
Copy Code
useEffect(() => {fetchProducts();}, []);useEffect(() => {fetchCart();}, []);
Add the following code to the Store
component's return
statement to render the UI. The UI displays the following:
- A Choose Products section with a list of your store's products. Clicking a product adds it to the cart.
- A Cart section with a list of the products in the cart.
- A Clear Cart button that clears the current cart session.
- A Checkout button that redirects the visitor to the Wix checkout page.
Copy Code
<div><div><h2>Choose Products:</h2>{productList.map((product) => {return (<div key={product._id} onClick={() => addToCart(product)}>{product.name}</div>);})}</div><div><h2>Cart:</h2>{cart.lineItems?.length > 0 && (<><div onClick={() => createRedirect()}><h3>{cart.lineItems.length} items ({cart.subtotal.formattedAmount})</h3><span>Checkout</span></div><div onClick={() => clearCart()}><span>Clear cart</span></div></>)}</div></div>
You can use the following full code example as a starting point for developing your own site:
Copy Code
import { createClient, OAuthStrategy } from '@wix/sdk';import { products } from '@wix/stores';import { currentCart } from '@wix/ecom';import { redirects } from '@wix/redirects';import { useEffect, useState } from 'react';import Cookies from 'js-cookie';const myWixClient = createClient({modules: { products, currentCart, redirects },auth: OAuthStrategy({clientId: `<YOUR-CLIENT-ID>`,tokens: JSON.parse(Cookies.get('session') || null),}),});export default function Store() {const [productList, setProductList] = useState([]);const [cart, setCart] = useState({});async function fetchProducts() {const productList = await myWixClient.products.queryProducts().find();setProductList(productList.items);}async function fetchCart() {try {setCart(await myWixClient.currentCart.getCurrentCart());} catch {}}async function addToCart(product) {const options = product.productOptions.reduce((selected, option) => ({...selected,[option.name]: option.choices[0].description,}),{});const { cart } = await myWixClient.currentCart.addToCurrentCart({lineItems: [{catalogReference: {appId: '1380b703-ce81-ff05-f115-39571d94dfcd',catalogItemId: product._id,options: { options },},quantity: 1,},],});setCart(cart);}async function clearCart() {await myWixClient.currentCart.deleteCurrentCart();setCart({});}async function createRedirect() {const { checkoutId } =await myWixClient.currentCart.createCheckoutFromCurrentCart({channelType: currentCart.ChannelType.WEB,});const redirect = await myWixClient.redirects.createRedirectSession({ecomCheckout: { checkoutId },callbacks: { postFlowUrl: window.location.href },});window.location = redirect.redirectSession.fullUrl;}useEffect(() => {fetchProducts();}, []);useEffect(() => {fetchCart();}, []);return (<div><div><h2>Choose Products:</h2>{productList.map((product) => {return (<div key={product._id} onClick={() => addToCart(product)}>{product.name}</div>);})}</div><div><h2>Cart:</h2>{cart.lineItems?.length > 0 && (<><div onClick={() => createRedirect()}><h3>{cart.lineItems.length} items ({cart.subtotal.formattedAmount})</h3><span>Checkout</span></div><div onClick={() => clearCart()}><span>Clear cart</span></div></>)}</div></div>);}