CodeAPI

wix-stores-backend

The wix-stores-backend module contains functionality for working with your site's store from backend code.

To use the Stores API, import wixStores from the wix-stores-backend module:

   import wixStores from 'wix-stores-backend';

Table of Contents

OBJECTS

?
Objects used when setting, getting, or calling the properties and methods listed above.
ProductOptionsAvailabilityAn object returned by the getProductOptionsAvailability() function representing the availability of a product.
ProductOptionsAvailabilitySelectedVariantAn object representing the product variant selected using the getProductOptionsAvailability() funtion.
StoreProductChoicesAn object representing the selection of specific choices from the product's options.

getProductOptionsAvailability( )

Gets the availability of a product based on the specified option choices.

Description

The getProductOptionsAvailability() function returns a Promise that is resolved to a ProductOptionsAvailability object when the product's availability information about the product is retrieved.

The information returned in the selectedVariant and availableForPurchase properties reflects the option choices passed in using the StoreProductChoices parameter.

If the specified choices result in the selection of a single product variant, that variant is returned in the selectedVariant property and the availableForPurchase property indicates whether that product variant is available for purchase.

If the specified choices do not result in the selection of a single product variant, no variant is returned in the selectedVariant property and the availableForPurchase property will be false.

Syntax

function getProductOptionsAvailability(productId: string, choices: StoreProductChoices): Promise<ProductOptionsAvailability>
PARAMETERS
?
The kind of data the property stores.
productId
string
The ID of the product whose availability is being checked.
choices
Option choices to use when checking the product's availability.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Fulfilled - The availability information of the product.

Examples

Get a product's availability information

This example gets a product's availability information using an option choice. The product contains more than one option, so no variant is selected and the product with the specified choices is not available for purchase.

/*****************************
 * backend code - stores.jsw *
 *****************************/

import wixStores from 'wix-stores-backend';

export function myBackendFunction(productId, choices) {
  return wixStores.getProductOptionsAvailability(productId, choices);
}

/********************
 * client-side code *
 ********************/
import {myFunction} from 'backend/myModule';

// ...

let productId = // get product ID
let choices = {
  "Size": "Large"
};

myBackendFunction(productId, choices)
  .then( (availability) => {
      let available = availability.availableForPurchase;  // false
      let options = availability.productOptions;          // see below
      let mainMedia = availability.mainMedia;
      let mediaItems = availability.mediaItems;
      let selectedVariant = availability.selectedVariant; // null
  } )
  .catch( (error) => {
    console.log(error);
  } );

/*
 * options:
 *
 * "Size":{
 *   "optionType":"drop_down",
 *   "name":"Size",
 *   "choices":[
 *     {
 *       "value":"Small",
 *       "description":"Small",
 *       "inStock":true,
 *       "visible":true
 *     },
 *     {
 *       "value":"Large",
 *       "description":"Large",
 *       "inStock":true,
 *       "visible":true
 *     }
 *   ]
 * },
 * "Color":{
 *   "optionType":"color",
 *   "name":"Color",
 *   "choices":[
 *     {
 *       "value":"rgb(0, 128, 0)",
 *       "description":"green",
 *       "inStock":true,
 *       "visible":true
 *     },
 *     {
 *       "value":"rgb(255, 0, 0)",
 *       "description":"red",
 *       "inStock":true,
 *       "visible":true
 *     }
 *   ]
 * }
 */

Get a product's availability information

This example gets a product's availability information using multiple option choices. Since all the product's options have a corresponding choice, a variant is selected.

/*****************************
 * backend code - stores.jsw *
 *****************************/

import wixStores from 'wix-stores-backend';

export function myBackendFunction(productId, choices) {
  return wixStores.getProductOptionsAvailability(productId, choices);
}

/********************
 * client-side code *
 ********************/
import {myFunction} from 'backend/myModule';

// ...

let productId = // get product ID
let choices = {
  "Size": "Large",
  "Color": "Red"
};

myBackendFunction(productId, choices)
  .then( (availability) => {
      let available = availability.availableForPurchase;  // true
      let options = availability.productOptions;          // see below
      let mainMedia = availability.mainMedia;
      let mediaItems = availability.mediaItems;
      let selectedVariant = availability.selectedVariant; // see below
  } )
  .catch( (error) => {
    console.log(error);
  } );

/*
 * options:
 *
 * "Size":{
 *   "optionType":"drop_down",
 *   "name":"Size",
 *   "choices":[
 *     {
 *       "value":"Small",
 *       "description":"Small",
 *       "inStock":true,
 *       "visible":true
 *     },
 *     {
 *       "value":"Large",
 *       "description":"Large",
 *       "inStock":true,
 *       "visible":true
 *     }
 *   ]
 * },
 * "Color":{
 *   "optionType":"color",
 *   "name":"Color",
 *   "choices":[
 *     {
 *       "value":"rgb(0, 128, 0)",
 *       "description":"green",
 *       "inStock":true,
 *       "visible":true
 *     },
 *     {
 *       "value":"rgb(255, 0, 0)",
 *       "description":"red",
 *       "inStock":true,
 *       "visible":true
 *     }
 *   ]
 * }
 *
 *
 * selectedVariant:
 *
 * "selectedVariant":{
 *   "sku":"366615376135191",
 *   "currency":"USD",
 *   "price":7.5,
 *   "discountedPrice":7.5,
 *   "formattedPrice":"$7.50",
 *   "formattedDiscountedPrice":"$7.50",
 *   "visible":true,
 *   "inStock":true,
 *   "weight":0
 * }
 */

ProductOptionsAvailability

An object returned by the getProductOptionsAvailability() function representing the availability of a product.

See Also

getProductOptionsAvailability( )

Syntax

type ProductOptionsAvailability = {
  availableForPurchase: boolean
  productOptions: StoreProductOptions
  mainMedia: string
  mediaItems: Array<ImageItem> | Array<VideoItem>
  selectedVariant: ProductOptionsAvailabilitySelectedVariant
}
MEMBERS
?
The kind of data the property stores.
availableForPurchase
boolean
Whether the product with the specified option choices is available for purchase.
productOptions
An object representing all the available options for a store product.
mainMedia
string
Main product media item (image or video) URL.
mediaItems
Array<ImageItem> | Array<VideoItem>
List of product media items.
selectedVariant
The variant of the product selected using the specified option choices if there is one.

ProductOptionsAvailabilitySelectedVariant

An object representing the product variant selected using the getProductOptionsAvailability() funtion.

See Also

getProductOptionsAvailability( ), ProductOptionsAvailability( )

Syntax

type ProductOptionsAvailabilitySelectedVariant = {
  sku: string
  currency: string
  price: number
  discountedPrice: number
  formattedPrice: string
  formattedDiscountedPrice: string
  visible: boolean
  inStock: boolean
  weight: number
}
MEMBERS
?
The kind of data the property stores.
sku
string
Product variant stock keeping unit value.
currency
string
Product variant currency.
price
number
Product variant price.
discountedPrice
number
Discounted product variant price.
formattedPrice
string
Product variant price formatted with the currency.
formattedDiscountedPrice
string
Discounted product variant price formatted with the currency.
visible
boolean
Whether the product variant is shown in the store.
inStock
boolean
Whether the product variant is in stock.
weight
number
Product variant weight.

StoreProductChoices

An object representing the selection of specific choices from the product's options.

See Also

getProductOptionsAvailability( )

Syntax

type StoreProductChoices = {
  optionKey: string
}
MEMBERS
?
The kind of data the property stores.
optionKey
string

Value of the option. This key name is dependent on the options added to the product. For example, if a product has a size option, this key value will be something like "Size" and its value will be something like "Large".