getProductVariants( )


Gets a product's available variants based on the specified product ID and either option choices or variant IDs.

The getProductVariants() function returns a Promise that is resolved to an array of VariantItem objects when the product variants with the specified choices or variant IDs are retrieved.

Method Declaration
Copy
function getProductVariants(
  productId: string,
  options: ProductVariantOptions,
): Promise<Array<VariantItem>>;
Method Parameters
productIdstringRequired

The ID of the product whose variants are being retrieved. Pass only this field to retrieve all the specified product's variants.


optionsProductVariantOptions

Variant options to return. If not specified, all the product's variants are returned.

Returns
Return Type:Promise<Array<VariantItem>>
JavaScript
/******************************* * Backend code - products.jsw * *******************************/ import wixStoresBackend from "wix-stores-backend"; export function getProductVariants(productId, options) { return wixStoresBackend.getProductVariants(productId, options); } /************* * Page code * *************/ import { getProductVariants } from "backend/products"; let productId = "3fb6a3c8-988b-7895-04bd-5c59ae0b18ea"; // get product ID let options = { choices: { Size: "Large", Color: "Red", }, }; getProductVariants(productId, options) .then((variants) => { let firstVariant = variants[0]; let firstPrice = firstVariant.variant.price; let numberOfReturnedVariants = variants.length; }) .catch((error) => { console.error(error); }); /* Example of returned variants array: * * [ * { * "_id": "00000000-0000-03e1-0005-957f699d0688", * "choices": { * "Size": "Large", * "Color": "Red", * "Pattern": "Paisley" * }, * "variant": { * "currency": "USD", * "price": 25, * "discountedPrice": 25, * "formattedPrice": "$25.00", * "formattedDiscountedPrice": "$25.00", * "pricePerUnit": 0.25, * "formattedPricePerUnit": "$0.25", * "weight": 5, * "sku": "217537123517253", * "visible": true * } * }, * { * "id": "00000000-0000-03e2-0005-957f699d0688", * "choices": { * "Size": "Large", * "Color": "Red", * "Pattern": "Houndstooth" * }, * "variant": { * "currency": "USD", * "price": 25, * "discountedPrice": 25, * "formattedPrice": "$25.00", * "formattedDiscountedPrice": "$25.00", * "pricePerUnit": 0.25, * "formattedPricePerUnit": "$0.25", * "weight": 5, * "sku": "217537123517253", * "visible": true * } * } * ] * */
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?