Search.../

updateInventoryVariantFields( )

Updates an existing inventory item's variants by inventory ID.

Description

The updateInventoryVariantFields() function returns a Promise that resolves when the variant with the specified inventory ID has been updated.

Only the properties passed in the inventory item object will be updated. All other properties will remain the same.

Note: You can also update a variant's inventory by product ID instead of inventory ID using the updateInventoryVariantFieldsByProductId() function. The product ID is the corresponding product ID for the inventory item in a Wix store.

Syntax

function updateInventoryVariantFields(inventoryId: string, inventoryInfo: InventoryItemVariantInfo): Promise<void>

updateInventoryVariantFields Parameters

NAME
TYPE
DESCRIPTION
inventoryId

string

Inventory ID of the item with variants to update.

inventoryInfo

InventoryItemVariantInfo

The inventory information to update in the variant.

Returns

Fulfilled - When the items are updated. Rejected - Error message.

Return Type:

Promise<void>

Was this helpful?

Update a variant's inventory information by inventory ID

Code Example

Copy Code
1/*******************************
2 * Backend code - inventory.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function updateInventoryVariantFields(id, item) {
8 return wixStoresBackend.updateInventoryVariantFields(id, item);
9}
10
11export function getProductVariantsBackend(productId, options) {
12 return wixStoresBackend.getProductVariants(productId, options);
13}
14
15
16/*************
17 * Page code *
18 *************/
19
20import wixData from 'wix-data';
21import { updateInventoryVariantFields, getProductVariantsBackend } from 'backend/inventory';
22
23$w.onReady(async function () {
24
25 // we want to change the inventory info for
26 // a small, blue suit.
27 let item = "Suit";
28 let size = "small";
29 let color = "blue";
30
31 // query to get the product ID and inventory ID for a suit
32 let myProductDetails = await getMyProduct(item);
33 let myProductId = myProductDetails[0];
34 let myInventoryId = myProductDetails[1];
35
36 // query to get the variant ID for the small, blue suit
37 let myVariantId = await getMyVariant(myProduct, size, color);
38
39 // set up the inventory information to update for the variant
40 let myVariantData = {
41 "trackQuantity": true,
42 "variants": [{
43 "quantity": 1,
44 "variantId": myVariantId
45 }]
46 };
47
48 try {
49 // run the function to update the inventory for the variant
50 await updateInventoryVariantFields(myInventoryId, myVariantData);
51 }
52 catch (err) {
53 // handle the error
54 }
55});
56
57export function getMyProduct(theItem) {
58 return wixData.query("Stores/Products")
59 .eq("name", theItem)
60 .find()
61 .then((results) => {
62 if (results.items.length > 0) {
63 return [results.items[0]._id, results.items[0].inventoryItem];
64 } else {
65 // handle the error
66 return;
67 };
68 });
69};
70
71export function getMyVariant(productId, size, color) {
72 let productOptions = {
73 "choices": {
74 "Size": size,
75 "Color": color
76 }
77 };
78 return getProductVariantsBackend(productId, productOptions)
79 .then((variantResults) => {
80 if (variantResults.items.length > 0) {
81 return variantResults.items[0]._id;
82 } else {
83 // handle the error
84 return
85 }
86 })
87}
88
89/* A sample inventory item with variants is:
90 *
91 * {
92 * "_id" : 133c6\-...-0fb2,
93 * "productId" : ecc3-...-1f04d,
94 * "trackQuantity" : true,
95 * "variants" : -[
96 * {
97 * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270,
98 * "inStock" : false,
99 * "quantity" : 0
100 * },
101 * {
102 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,
103 * "inStock" : false,
104 * "quantity" : 0
105 * },
106 * {
107 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,
108 * "inStock" : true,
109 * "quantity" : 100
110 * },
111 * {
112 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,
113 * "inStock" : true,
114 * "quantity" : 123
115 * },
116 * {
117 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,
118 * "inStock" : false,
119 * "quantity" : 0
120 * },
121 * {
122 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,
123 * "inStock" : true,
124 * "quantity" : 1
125 * }
126 * ],
127 * "_updatedDate" : 2020-02-17T08:25:57.734Z
128 * }
129 */
Update a variant's inventory information by inventory ID according to how it is being tracked

This example demonstrates how to update:

  • The inventory amount if inventory is being tracked.
  • The in-stock/out-of-stock status if inventory is not being tracked.

Code Example

Copy Code
1/*******************************
2 * Backend code - inventory.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function updateInventoryVariantFields(id, item) {
8 return wixStoresBackend.updateInventoryVariantFields(id, item);
9}
10
11
12/*************
13 * Page code *
14 *************/
15
16import { updateInventoryVariantFields } from 'backend/inventory';
17
18const productId = // get product ID by query, user input, or from a productPage element
19const variantId = // get variant ID by query, user input, `getProductVariants()', or from a productPage element
20const productTrackQuantity = // determine if inventory is being tracked
21
22// check if quantity can be tracked for this product
23if (productTrackQuantity) {
24 let item = {
25 "trackQuantity": productTrackQuantity,
26 "variants": [{
27 "variantId": variantId,
28 "quantity": 10
29 }]
30 };
31} else {
32 let item = {
33 "trackQuantity": productTrackQuantity,
34 "variants": [{
35 "variantId": variantId,
36 "inStock": true
37 }]
38 };
39}
40
41updateInventoryVariantFields(productId, item)
42 .then((updateResults) => {
43 // inventory item variant data have been updated
44 })
45 .catch((error) => {
46 // there was an error updating the inventory item variant data
47 });
48
49
50/* A sample inventory item with variants is:
51 *
52 * {
53 * "_id" : 133c6\-...-0fb2,
54 * "productId" : ecc3-...-1f04d,
55 * "trackQuantity" : true,
56 * "variants" : -[
57 * {
58 * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270,
59 * "inStock" : false,
60 * "quantity" : 0
61 * },
62 * {
63 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,
64 * "inStock" : false,
65 * "quantity" : 0
66 * },
67 * {
68 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,
69 * "inStock" : true,
70 * "quantity" : 10
71 * },
72 * {
73 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,
74 * "inStock" : true,
75 * "quantity" : 123
76 * },
77 * {
78 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,
79 * "inStock" : false,
80 * "quantity" : 0
81 * },
82 * {
83 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,
84 * "inStock" : true,
85 * "quantity" : 1
86 * }
87 * ],
88 * "_updatedDate" : 2020-02-17T08:25:57.734Z
89 * }
90 */