wix-stores-backend

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:

ICAgaW1wb3J0IHdpeFN0b3JlcyBmcm9tICd3aXgtc3RvcmVzLWJhY2tlbmQnOw==
   import wixStores from 'wix-stores-backend';

Contents

getProductOptionsAvailability( ) Gets the availability of a product based on the specified option choices.
ProductOptionsAvailability An object returned by the getProductOptionsAvailability() function representing the availability of a product.
ProductOptionsAvailabilitySelectedVariant An object representing the product variant selected using the getProductOptionsAvailability() funtion.
StoreProductChoices An object representing the selection of specific choices from the product's options.
getProductOptionsAvailability( )

getProductOptionsAvailability( )

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

function getProductOptionsAvailability(productId: String, choices: StoreProductChoices): Promise<ProductOptionsAvailability>

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.

Parameters

productId String The ID of the product whose availability is being checked.
choices StoreProductChoices Option choices to use when checking the product's availability.

Return Value

Returns a Promise

On fulfillment ProductOptionsAvailability 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.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqIGJhY2tlbmQgY29kZSAtIHN0b3Jlcy5qc3cgKgogKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCgppbXBvcnQgd2l4U3RvcmVzIGZyb20gJ3dpeC1zdG9yZXMtYmFja2VuZCc7CgpleHBvcnQgZnVuY3Rpb24gbXlCYWNrZW5kRnVuY3Rpb24ocHJvZHVjdElkLCBjaG9pY2VzKSB7CiAgcmV0dXJuIHdpeFN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tYWxsIiwKICogICAgICAgImluU3RvY2siOnRydWUsCiAqICAgICAgICJ2aXNpYmxlIjp0cnVlCiAqICAgICB9LAogKiAgICAgewogKiAgICAgICAidmFsdWUiOiJMYXJnZSIsCiAqICAgICAgICJkZXNjcmlwdGlvbiI6IkxhcmdlIiwKICogICAgICAgImluU3RvY2siOnRydWUsCiAqICAgICAgICJ2aXNpYmxlIjp0cnVlCiAqICAgICB9CiAqICAgXQogKiB9LAogKiAiQ29sb3IiOnsKICogICAib3B0aW9uVHlwZSI6ImNvbG9yIiwKICogICAibmFtZSI6IkNvbG9yIiwKICogICAiY2hvaWNlcyI6WwogKiAgICAgewogKiAgICAgICAidmFsdWUiOiJyZ2IoMCwgMTI4LCAwKSIsCiAqICAgICAgICJkZXNjcmlwdGlvbiI6ImdyZWVuIiwKICogICAgICAgImluU3RvY2siOnRydWUsCiAqICAgICAgICJ2aXNpYmxlIjp0cnVlCiAqICAgICB9LAogKiAgICAgewogKiAgICAgICAidmFsdWUiOiJyZ2IoMjU1LCAwLCAwKSIsCiAqICAgICAgICJkZXNjcmlwdGlvbiI6InJlZCIsCiAqICAgICAgICJpblN0b2NrIjp0cnVlLAogKiAgICAgICAidmlzaWJsZSI6dHJ1ZQogKiAgICAgfQogKiAgIF0KICogfQogKgo=
/*****************************
 * 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.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqIGJhY2tlbmQgY29kZSAtIHN0b3Jlcy5qc3cgKgogKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCgppbXBvcnQgd2l4U3RvcmVzIGZyb20gJ3dpeC1zdG9yZXMtYmFja2VuZCc7CgpleHBvcnQgZnVuY3Rpb24gbXlCYWNrZW5kRnVuY3Rpb24ocHJvZHVjdElkLCBjaG9pY2VzKSB7CiAgcmV0dXJuIHdpeFN0b3Jlcy5nZXRQcm9kdWN0T3B0aW9uc0F2YWlsYWJpbGl0eShwcm9kdWN0SWQsIGNob2ljZXMpOwp9CgovKioqKioqKioqKioqKioqKioqKioKICogY2xpZW50LXNpZGUgY29kZSAqCiAqKioqKioqKioqKioqKioqKioqKi8KaW1wb3J0IHtteUZ1bmN0aW9ufSBmcm9tICdiYWNrZW5kL215TW9kdWxlJzsKCi8vIC4uLgoKbGV0IHByb2R1Y3RJZCA9IC8vIGdldCBwcm9kdWN0IElECmxldCBjaG9pY2VzID0gewogICJTaXplIjogIkxhcmdlIiwKICAiQ29sb3IiOiAiUmVkIgp9OwoKbXlCYWNrZW5kRnVuY3Rpb24ocHJvZHVjdElkLCBjaG9pY2VzKQogIC50aGVuKCAoYXZhaWxhYmlsaXR5KSA9PiB7CiAgICAgIGxldCBhdmF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iZGVzY3JpcHRpb24iOiJncmVlbiIsCiAqICAgICAgICJpblN0b2NrIjp0cnVlLAogKiAgICAgICAidmlzaWJsZSI6dHJ1ZQogKiAgICAgfSwKICogICAgIHsKICogICAgICAgInZhbHVlIjoicmdiKDI1NSwgMCwgMCkiLAogKiAgICAgICAiZGVzY3JpcHRpb24iOiJyZWQiLAogKiAgICAgICAiaW5TdG9jayI6dHJ1ZSwKICogICAgICAgInZpc2libGUiOnRydWUKICogICAgIH0KICogICBdCiAqIH0KICoKICoKICogc2VsZWN0ZWRWYXJpYW50OgogKgogKiAic2VsZWN0ZWRWYXJpYW50Ijp7CiAqICAgInNrdSI6IjM2NjYxNTM3NjEzNTE5MSIsCiAqICAgImN1cnJlbmN5IjoiVVNEIiwKICogICAicHJpY2UiOjcuNSwKICogICAiZGlzY291bnRlZFByaWNlIjo3LjUsCiAqICAgImZvcm1hdHRlZFByaWNlIjoiJDcuNTAiLAogKiAgICJmb3JtYXR0ZWREaXNjb3VudGVkUHJpY2UiOiIkNy41MCIsCiAqICAgInZpc2libGUiOnRydWUsCiAqICAgImluU3RvY2siOnRydWUsCiAqICAgIndlaWdodCI6MAogKiB9Cg==
/*****************************
 * 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

ProductOptionsAvailability

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

Properties

availableForPurchase Boolean Whether the product with the specified option choices is available for purchase.
productOptions StoreProductOptions An object representing all the available options for a store product.
mainMedia String Main product media item (image or video) URL.
mediaItems ImageItem[ ] |
VideoItem[ ]
List of product media items.
selectedVariant ProductOptionsAvailabilitySelectedVariant The variant of the product selected using the specified option choices if there is one.

See Also

getProductOptionsAvailability( )

ProductOptionsAvailabilitySelectedVariant

ProductOptionsAvailabilitySelectedVariant

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

Properties

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.

See Also

getProductOptionsAvailability( ), ProductOptionsAvailability( )

StoreProductChoices

StoreProductChoices

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

Properties

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".

See Also

getProductOptionsAvailability( )