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

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

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