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

Code Example

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

Code Example

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/*************
13 * Page code *
14 *************/
15
16import { updateInventoryVariantFieldsByProductId } from 'backend/inventory';
17import wixData from 'wix-data';
18
19
20export function updateByProductIdButton_click(event) {
21 const productId = $w('#updateByProductIdInventoryId').value;
22 const trackInventory = $w('#updateByProductIdTrackInventory').checked;
23 const variantId = $w('#updateByProductIdVariantId').value;
24 const inStock = $w('#updateByProductIdInStock').checked;
25 const quantity = $w('#updateByProductIdQuantity').value;
26
27 updateInventoryVariantFieldsByProductId(productId, {
28 trackQuantity: trackInventory,
29 variants: [{
30 variantId,
31 inStock,
32 quantity
33 }]
34 }).then(console.log("Variant inventory updated."));
35
36}
37
38// Populate variant dropdown options
39// from which the user will choose.
40export async function updateByProductIdRefreshVariantId_click(event) {
41 const productId = $w('#updateByProductIdInventoryId').value;
42 const variantRefreshResults = await wixData.query('Stores/InventoryItems').eq('productId', productId).find();
43 const ids = variantRefreshResults.items[0].variants.map(item => {
44 return { label: item.variantId, value: item.variantId };
45 });
46 $w('#updateByProductIdVariantId').options = ids;
47}
48
49/* A sample inventory item with variants is:
50 *
51 * {
52 * "_id" : 133c6\-...-0fb2,
53 * "externalId" : 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" : 100
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 */