Search.../

getProductVariants( )

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

Description

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.

Syntax

function getProductVariants(productId: string, options: ProductVariantOptions): Promise<Array<VariantItem>>

getProductVariants Parameters

NAME
TYPE
DESCRIPTION
productId

string

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

options

ProductVariantOptions

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

Returns

Fulfilled - The variants with the specified choices.

Return Type:

Promise<Array<VariantItem>>
NAME
TYPE
DESCRIPTION
_id

string

Unique variant ID.

choices

ProductChoices

The choices of the retrieved variant.

variant

VariantInfo

Variant information.

Was this helpful?

Get product variants by specified choices

Code Example

Copy Code
1import wixStores from 'wix-stores';
2
3let productId = "3fb6a3c8-988b-7895-04bd-5c59ae0b18ea"; // get product ID
4let options = {
5 choices: {
6 "Size": "Large",
7 "Color": "Red"
8 }
9};
10
11wixStores.getProductVariants(productId, options)
12 .then((variants) => {
13 let firstVariant = variants[0];
14 let firstPrice = firstVariant.variant.price;
15 let numberOfReturnedVariants = variants.length;
16 })
17 .catch((error) => {
18 console.error(error);
19 })
20
21/* Example of returned variants array:
22 *
23 * [
24 * {
25 * "_id": "00000000-0000-03e1-0005-957f699d0688",
26 * "choices": {
27 * "Size": "Large",
28 * "Color": "Red",
29 * "Pattern": "Paisley"
30 * },
31 * "variant": {
32 * "currency": "USD",
33 * "price": 25,
34 * "discountedPrice": 25,
35 * "formattedPrice": "$25.00",
36 * "formattedDiscountedPrice": "$25.00",
37 * "pricePerUnit": 0.25,
38 * "formattedPricePerUnit": "$0.25",
39 * "weight": 5,
40 * "sku": "217537123517253",
41 * "visible": true
42 * }
43 * },
44 * {
45 * "id": "00000000-0000-03e2-0005-957f699d0688",
46 * "choices": {
47 * "Size": "Large",
48 * "Color": "Red",
49 * "Pattern": "Houndstooth"
50 * },
51 * "variant": {
52 * "currency": "USD",
53 * "price": 25,
54 * "discountedPrice": 25,
55 * "formattedPrice": "$25.00",
56 * "formattedDiscountedPrice": "$25.00",
57 * "pricePerUnit": 0.25,
58 * "formattedPricePerUnit": "$0.25",
59 * "weight": 5,
60 * "sku": "217537123517253",
61 * "visible": true
62 * }
63 * }
64 * ]
65 *
66 */
Get product variants by variant IDs

Code Example

Copy Code
1import wixStores from 'wix-stores';
2
3let productId = "3fb6a3c8-988b-7895-04bd-5c59ae0b18ea"; // get product ID
4let options = {
5 variantIds: [
6 "00000000-0000-03e1-0005-957f699d0688",
7 "00000000-0000-03e2-0005-957f699d0688"
8 ]
9};
10
11wixStores.getProductVariants(productId, options)
12 .then((variants) => {
13 let firstVariant = variants[0];
14 let firstPrice = firstVariant.variant.price;
15 let numberOfReturnedVariants = variants.length;
16 })
17 .catch((error) => {
18 console.error(error);
19 })
20
21/* Example of returned variants array:
22 *
23 * [
24 * {
25 * "_id": "00000000-0000-03e1-0005-957f699d0688",
26 * "choices": {
27 * "Size": "Large",
28 * "Color": "Red",
29 * "Pattern": "Paisley"
30 * },
31 * "variant": {
32 * "currency": "USD",
33 * "price": 25,
34 * "discountedPrice": 25,
35 * "formattedPrice": "$25.00",
36 * "formattedDiscountedPrice": "$25.00",
37 * "pricePerUnit": 0.25,
38 * "formattedPricePerUnit": "$0.25",
39 * "weight": 5,
40 * "sku": "217537123517253",
41 * "visible": true
42 * }
43 * },
44 * {
45 * "id": "00000000-0000-03e2-0005-957f699d0688",
46 * "choices": {
47 * "Size": "Large",
48 * "Color": "Red",
49 * "Pattern": "Houndstooth"
50 * },
51 * "variant": {
52 * "currency": "USD",
53 * "price": 25,
54 * "discountedPrice": 25,
55 * "formattedPrice": "$25.00",
56 * "formattedDiscountedPrice": "$25.00",
57 * "pricePerUnit": 0.25,
58 * "formattedPricePerUnit": "$0.25",
59 * "weight": 5,
60 * "sku": "217537123517253",
61 * "visible": true
62 * }
63 * }
64 * ]
65 *
66 */