Search.../

getProductVariants( )

Deprecated. This function will continue to work, but a newer version is available at wix-stores.product.getVariants().

Description

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

Migration Instructions

If this function is already in your code, it will continue to work. To stay compatible with future changes, migrate to wix-stores.product.getVariants().

To migrate to the new function:

  1. Add the new import statement:

    import { product } from 'wix-stores'
    javascript | Copy Code
  2. If you plan to migrate all functions that use wix-stores, remove the original import wixStores statement.

  3. Look for any code that uses wixStores.getProductVariants(), and replace it with product.getVariants(). Update your code to work with the new product.getVariants() call and response properties.

  4. Test your changes to make sure your code behaves as expected.

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
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
1// Updated example using product.getVariants()
2
3import { product } from 'wix-stores';
4
5const productId = "91f7ac8b-2baa-289c-aa50-6d64764f35d3";
6const options = {
7 choices: {
8 "Weight": "500g"
9 }
10};
11
12product.getVariants(productId, options)
13 .then((variants) => {
14 const firstVariant = variants[0];
15 const firstPrice = firstVariant.variant.price;
16 const numberOfReturnedVariants = variants.length;
17 })
18 .catch((error) => {
19 console.error(error);
20 });
21
22/*
23 * Example of returned variants:
24 *
25 * [
26 * {
27 * "_id": "00000000-0000-003f-0005-a316e6ba5b37",
28 * "choices": {
29 * "Weight": "500g",
30 * "Ground for": "Stovetop"
31 * },
32 * "variant": {
33 * "currency": "USD",
34 * "price": 65,
35 * "discountedPrice": 60,
36 * "pricePerUnit": 0.24,
37 * "formattedPrice": "$65.00",
38 * "formattedDiscountedPrice": "$65.00",
39 * "formattedPricePerUnit": "$0.24",
40 * "weight": 0.5,
41 * "sku": "10002",
42 * "visible": true
43 * }
44 * },
45 * {
46 * "_id": "00000000-0000-0040-0005-a316e6ba5b37",
47 * "choices": {
48 * "Weight": "500g",
49 * "Ground for": "Filter"
50 * },
51 * "variant": {
52 * "currency": "USD",
53 * "price": 70,
54 * "discountedPrice": 65,
55 * "pricePerUnit": 0.26,
56 * "formattedPrice": "$70.00",
57 * "formattedDiscountedPrice": "$70.00",
58 * "formattedPricePerUnit": "$0.26",
59 * "weight": 1,
60 * "sku": "10004",
61 * "visible": true
62 * }
63 * }
64 * ]
65 *
66 */
67
68
69// Deprecated example
70
71import wixStores from 'wix-stores';
72
73const productId = "3fb6a3c8-988b-7895-04bd-5c59ae0b18ea";
74const options = {
75 choices: {
76 "Size": "Large",
77 "Color": "Red"
78 }
79};
80
81wixStores.getProductVariants(productId, options)
82 .then((variants) => {
83 const firstVariant = variants[0];
84 const firstPrice = firstVariant.variant.price;
85 const numberOfReturnedVariants = variants.length;
86 })
87 .catch((error) => {
88 console.error(error);
89 });
Get product variants by variant IDs

Copy Code
1// Updated example using product.getVariants()
2
3import { product } from 'wix-stores';
4
5const productId = "91f7ac8b-2baa-289c-aa50-6d64764f35d3";
6const options = {
7 variantIds: [
8 "00000000-0000-0020-0005-a316e6ba5b37",
9 "00000000-0000-003f-0005-a316e6ba5b37"
10 ]
11};
12
13product.getVariants(productId, options)
14 .then((variants) => {
15 const firstVariant = variants[0];
16 const firstPrice = firstVariant.variant.price;
17 const numberOfReturnedVariants = variants.length;
18 })
19 .catch((error) => {
20 console.error(error);
21 })
22
23/*
24 * Example of returned variants array:
25 *
26 * [
27 * {
28 * "_id": "00000000-0000-0020-0005-a316e6ba5b37",
29 * "choices": {
30 * "Weight": "250g",
31 * "Ground for": "Stovetop"
32 * },
33 * "variant": {
34 * "currency": "USD",
35 * "price": 35,
36 * "discountedPrice": 30,
37 * "pricePerUnit": 0.12,
38 * "formattedPrice": "$35.00",
39 * "formattedDiscountedPrice": "$35.00",
40 * "formattedPricePerUnit": "$0.12",
41 * "weight": 0.25,
42 * "sku": "10001",
43 * "visible": true
44 * }
45 * },
46 * {
47 * "_id": "00000000-0000-003f-0005-a316e6ba5b37",
48 * "choices": {
49 * "Weight": "500g",
50 * "Ground for": "Stovetop"
51 * },
52 * "variant": {
53 * "currency": "USD",
54 * "price": 65,
55 * "discountedPrice": 60,
56 * "pricePerUnit": 0.24,
57 * "formattedPrice": "$65.00",
58 * "formattedDiscountedPrice": "$65.00",
59 * "formattedPricePerUnit": "$0.24",
60 * "weight": 0.5,
61 * "sku": "10002",
62 * "visible": true
63 * }
64 * }
65 * ]
66 *
67 */
68
69
70// Deprecated example
71
72import wixStores from 'wix-stores';
73
74const productId = "3fb6a3c8-988b-7895-04bd-5c59ae0b18ea";
75const options = {
76 variantIds: [
77 "00000000-0000-03e1-0005-957f699d0688",
78 "00000000-0000-03e2-0005-957f699d0688"
79 ]
80};
81
82wixStores.getProductVariants(productId, options)
83 .then((variants) => {
84 const firstVariant = variants[0];
85 const firstPrice = firstVariant.variant.price;
86 const numberOfReturnedVariants = variants.length;
87 })
88 .catch((error) => {
89 console.error(error);
90 });