Search.../

getVariants( )

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 getVariants(productId: string, [options: ProductVariantOptions]): Promise<Array<VariantItem>>

getVariants Parameters

NAME
TYPE
DESCRIPTION
productId
string

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

options
Optional
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
Object

Choices of the retrieved variant in the form of an object containing a key:value pair for each variant choice. For example, if a variant has a size option, this key value will be something like "Size" and its value will be something like "Large".

variant
VariantInfo

Variant information.

Was this helpful?

Get product variants by specified choices

Copy Code
1import { product } from 'wix-stores-frontend';
2
3const productId = "91f7ac8b-2baa-289c-aa50-6d64764f35d3";
4const options = {
5 choices: {
6 "Weight": "500g"
7 }
8};
9
10product.getVariants(productId, options)
11 .then((variants) => {
12 const firstVariant = variants[0];
13 const firstPrice = firstVariant.variant.price;
14 const numberOfReturnedVariants = variants.length;
15 })
16 .catch((error) => {
17 console.error(error);
18 });
19
20
21/*
22 * Example of returned variants:
23 *
24 * [
25 * {
26 * "_id": "00000000-0000-003f-0005-a316e6ba5b37",
27 * "choices": {
28 * "Weight": "500g",
29 * "Ground for": "Stovetop"
30 * },
31 * "variant": {
32 * "currency": "USD",
33 * "price": 65,
34 * "discountedPrice": 60,
35 * "pricePerUnit": 0.24,
36 * "formattedPrice": "$65.00",
37 * "formattedDiscountedPrice": "$65.00",
38 * "formattedPricePerUnit": "$0.24",
39 * "weight": 0.5,
40 * "sku": "10002",
41 * "visible": true
42 * }
43 * },
44 * {
45 * "_id": "00000000-0000-0040-0005-a316e6ba5b37",
46 * "choices": {
47 * "Weight": "500g",
48 * "Ground for": "Filter"
49 * },
50 * "variant": {
51 * "currency": "USD",
52 * "price": 70,
53 * "discountedPrice": 65,
54 * "pricePerUnit": 0.26,
55 * "formattedPrice": "$70.00",
56 * "formattedDiscountedPrice": "$70.00",
57 * "formattedPricePerUnit": "$0.26",
58 * "weight": 1,
59 * "sku": "10004",
60 * "visible": true
61 * }
62 * }
63 * ]
64 *
65 */
Get product variants by variant IDs

Copy Code
1import { product } from 'wix-stores-frontend';
2
3const productId = "91f7ac8b-2baa-289c-aa50-6d64764f35d3";
4const options = {
5 variantIds: [
6 "00000000-0000-0020-0005-a316e6ba5b37",
7 "00000000-0000-003f-0005-a316e6ba5b37"
8 ]
9};
10
11product.getVariants(productId, options)
12 .then((variants) => {
13 const firstVariant = variants[0];
14 const firstPrice = firstVariant.variant.price;
15 const numberOfReturnedVariants = variants.length;
16 })
17 .catch((error) => {
18 console.error(error);
19 })
20
21
22/*
23 * Example of returned variants array:
24 *
25 * [
26 * {
27 * "_id": "00000000-0000-0020-0005-a316e6ba5b37",
28 * "choices": {
29 * "Weight": "250g",
30 * "Ground for": "Stovetop"
31 * },
32 * "variant": {
33 * "currency": "USD",
34 * "price": 35,
35 * "discountedPrice": 30,
36 * "pricePerUnit": 0.12,
37 * "formattedPrice": "$35.00",
38 * "formattedDiscountedPrice": "$35.00",
39 * "formattedPricePerUnit": "$0.12",
40 * "weight": 0.25,
41 * "sku": "10001",
42 * "visible": true
43 * }
44 * },
45 * {
46 * "_id": "00000000-0000-003f-0005-a316e6ba5b37",
47 * "choices": {
48 * "Weight": "500g",
49 * "Ground for": "Stovetop"
50 * },
51 * "variant": {
52 * "currency": "USD",
53 * "price": 65,
54 * "discountedPrice": 60,
55 * "pricePerUnit": 0.24,
56 * "formattedPrice": "$65.00",
57 * "formattedDiscountedPrice": "$65.00",
58 * "formattedPricePerUnit": "$0.24",
59 * "weight": 0.5,
60 * "sku": "10002",
61 * "visible": true
62 * }
63 * }
64 * ]
65 *
66 */