Search.../

updateInventoryVariantFieldsByProductId( )

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

Description

The updateInventoryVariantFieldsByProductId() function returns a Promise that resolves when the variant with the specified product 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 inventory ID instead of product ID using the updateInventoryVariantFields() function.

Syntax

function updateInventoryVariantFieldsByProductId(productId: string, inventoryInfo: InventoryItemVariantInfo): Promise<void>

updateInventoryVariantFieldsByProductId Parameters

NAME
TYPE
DESCRIPTION
productId
string

The corresponding product ID for the inventory item in a Wix store.

inventoryInfo
InventoryItemVariantInfo

The 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 product ID

Copy Code
1/*******************************
2 * Backend code - inventory.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function updateInventoryVariantFieldsByProductId(productId, item) {
8 return wixStoresBackend.updateInventoryVariantFieldsByProductId(productId, item);
9}
10
11/*************
12 * Page code *
13 *************/
14
15import { updateInventoryVariantFieldsByProductId } from 'backend/inventory';
16
17$w.onReady(async function () {
18 const product = await $w('#productPage1').getProduct();
19 let options = product.variants;
20 let foundVariant = options.find((option) => {
21 return option.choices.Color === "red" && option.choices.Size === "large";
22 });
23 let variantInfo = {
24 trackQuantity: false,
25 variants: [{
26 inStock: true,
27 variantId: foundVariant._id,
28 }]
29 };
30 updateInventoryVariantFieldsByProductId(product._id, variantInfo);
31});
32
33/* A sample inventory item with variants is:
34 *
35 * {
36 * "_id" : 133c6\-...-0fb2,
37 * "productId" : ecc3-...-1f04d,
38 * "trackQuantity" : true,
39 * "variants" : -[
40 * {
41 * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270,
42 * "inStock" : false,
43 * "quantity" : 0
44 * },
45 * {
46 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,
47 * "inStock" : false,
48 * "quantity" : 0
49 * },
50 * {
51 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,
52 * "inStock" : true,
53 * "quantity" : 100
54 * },
55 * {
56 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,
57 * "inStock" : true,
58 * "quantity" : 123
59 * },
60 * {
61 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,
62 * "inStock" : false,
63 * "quantity" : 0
64 * },
65 * {
66 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,
67 * "inStock" : true,
68 * "quantity" : 1
69 * }
70 * ],
71 * "_updatedDate" : 2020-02-17T08:25:57.734Z
72 * }
73 */
Update a variant's inventory information by product ID using user input

This example assumes the following elements exist on the page:

  • Text boxes for entering the product ID and the inventory amount.
  • Checkboxes for indicating if inventory is being tracked and if an item is in-stock.
  • A drop-down for displaying a product's variants.
  • Buttons for populating the drop-down and for performing the update.

Copy Code
1/*******************************
2 * Backend code - inventory.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function updateInventoryVariantFieldsByProductId(id, item) {
8 return wixStoresBackend.updateInventoryVariantFieldsByProductId(id, item);
9}
10
11/*************
12 * Page code *
13 *************/
14
15import { updateInventoryVariantFieldsByProductId } from 'backend/inventory';
16import wixData from 'wix-data';
17
18
19export function updateByProductIdButton_click(event) {
20 const productId = $w('#updateByProductIdInventoryId').value;
21 const trackInventory = $w('#updateByProductIdTrackInventory').checked;
22 const variantId = $w('#updateByProductIdVariantId').value;
23 const inStock = $w('#updateByProductIdInStock').checked;
24 const quantity = $w('#updateByProductIdQuantity').value;
25
26 updateInventoryVariantFieldsByProductId(productId, {
27 trackQuantity: trackInventory,
28 variants: [{
29 variantId,
30 inStock,
31 quantity
32 }]
33 }).then(console.log("Variant inventory updated."));
34
35}
36
37// Populate variant dropdown options
38// from which the user will choose.
39export async function updateByProductIdRefreshVariantId_click(event) {
40 const productId = $w('#updateByProductIdInventoryId').value;
41 const variantRefreshResults = await wixData.query('Stores/InventoryItems').eq('productId', productId).find();
42 const ids = variantRefreshResults.items[0].variants.map(item => {
43 return { label: item.variantId, value: item.variantId };
44 });
45 $w('#updateByProductIdVariantId').options = ids;
46}
47
48/* A sample inventory item with variants is:
49 *
50 * {
51 * "_id" : 133c6\-...-0fb2,
52 * "externalId" : ecc3-...-1f04d,
53 * "trackQuantity" : true,
54 * "variants" : -[
55 * {
56 * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270,
57 * "inStock" : false,
58 * "quantity" : 0
59 * },
60 * {
61 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,
62 * "inStock" : false,
63 * "quantity" : 0
64 * },
65 * {
66 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,
67 * "inStock" : true,
68 * "quantity" : 100
69 * },
70 * {
71 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,
72 * "inStock" : true,
73 * "quantity" : 123
74 * },
75 * {
76 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,
77 * "inStock" : false,
78 * "quantity" : 0
79 * },
80 * {
81 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,
82 * "inStock" : true,
83 * "quantity" : 1
84 * }
85 * ],
86 * "_updatedDate" : 2020-02-17T08:25:57.734Z
87 * }
88 */