> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: updateInventoryVariantFields(inventoryId: string, inventoryInfo: InventoryItemVariantInfo) # Method package: wixStoresBackend # Method menu location: wixStoresBackend --> updateInventoryVariantFields # Method Link: https://dev.wix.com/docs/velo/apis/wix-stores-backend/update-inventory-variant-fields.md # Method Description: Updates an existing inventory item's variants by inventory ID. 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()`](#updateInventoryVariantFieldsByProductId) function. The product ID is the corresponding product ID for the inventory item in a Wix store. # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Update a variant's inventory information by inventory ID ```javascript /******************************* * Backend code - inventory.jsw * *******************************/ import wixStoresBackend from 'wix-stores-backend'; export function updateInventoryVariantFields(id, item) { return wixStoresBackend.updateInventoryVariantFields(id, item); } export function getProductVariantsBackend(productId, options) { return wixStoresBackend.getProductVariants(productId, options); } /************* * Page code * *************/ import wixData from 'wix-data'; import { updateInventoryVariantFields, getProductVariantsBackend } from 'backend/inventory'; $w.onReady(async function () { // we want to change the inventory info for // a small, blue suit. let item = "Suit"; let size = "small"; let color = "blue"; // query to get the product ID and inventory ID for a suit let myProductDetails = await getMyProduct(item); let myProductId = myProductDetails[0]; let myInventoryId = myProductDetails[1]; // query to get the variant ID for the small, blue suit let myVariantId = await getMyVariant(myProduct, size, color); // set up the inventory information to update for the variant let myVariantData = { "trackQuantity": true, "variants": [{ "quantity": 1, "variantId": myVariantId }] }; try { // run the function to update the inventory for the variant await updateInventoryVariantFields(myInventoryId, myVariantData); } catch (err) { // handle the error } }); export function getMyProduct(theItem) { return wixData.query("Stores/Products") .eq("name", theItem) .find() .then((results) => { if (results.items.length > 0) { return [results.items[0]._id, results.items[0].inventoryItem]; } else { // handle the error return; }; }); }; export function getMyVariant(productId, size, color) { let productOptions = { "choices": { "Size": size, "Color": color } }; return getProductVariantsBackend(productId, productOptions) .then((variantResults) => { if (variantResults.items.length > 0) { return variantResults.items[0]._id; } else { // handle the error return } }) } /* A sample inventory item with variants is: * * { * "_id" : 133c6\-...-0fb2, * "productId" : ecc3-...-1f04d, * "trackQuantity" : true, * "variants" : -[ * { * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270, * "inStock" : false, * "quantity" : 0 * }, * { * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270, * "inStock" : false, * "quantity" : 0 * }, * { * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270, * "inStock" : true, * "quantity" : 100 * }, * { * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270, * "inStock" : true, * "quantity" : 123 * }, * { * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270, * "inStock" : false, * "quantity" : 0 * }, * { * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270, * "inStock" : true, * "quantity" : 1 * } * ], * "_updatedDate" : 2020-02-17T08:25:57.734Z * } */ ``` ## Update a variant's inventory information by inventory ID according to how it is being tracked ```javascript /******************************* * Backend code - inventory.jsw * *******************************/ import wixStoresBackend from 'wix-stores-backend'; export function updateInventoryVariantFields(id, item) { return wixStoresBackend.updateInventoryVariantFields(id, item); } /************* * Page code * *************/ import { updateInventoryVariantFields } from 'backend/inventory'; const productId = // get product ID by query, user input, or from a productPage element const variantId = // get variant ID by query, user input, `getProductVariants()', or from a productPage element const productTrackQuantity = // determine if inventory is being tracked // check if quantity can be tracked for this product if (productTrackQuantity) { let item = { "trackQuantity": productTrackQuantity, "variants": [{ "variantId": variantId, "quantity": 10 }] }; } else { let item = { "trackQuantity": productTrackQuantity, "variants": [{ "variantId": variantId, "inStock": true }] }; } updateInventoryVariantFields(productId, item) .then((updateResults) => { // inventory item variant data have been updated }) .catch((error) => { // there was an error updating the inventory item variant data }); /* A sample inventory item with variants is: * * { * "_id" : 133c6\-...-0fb2, * "productId" : ecc3-...-1f04d, * "trackQuantity" : true, * "variants" : -[ * { * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270, * "inStock" : false, * "quantity" : 0 * }, * { * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270, * "inStock" : false, * "quantity" : 0 * }, * { * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270, * "inStock" : true, * "quantity" : 10 * }, * { * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270, * "inStock" : true, * "quantity" : 123 * }, * { * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270, * "inStock" : false, * "quantity" : 0 * }, * { * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270, * "inStock" : true, * "quantity" : 1 * } * ], * "_updatedDate" : 2020-02-17T08:25:57.734Z * } */ ``` ---