> 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: updateInventoryVariantFieldsByProductId(productId: string, inventoryInfo: InventoryItemVariantInfo) # Method package: wixStoresBackend # Method menu location: wixStoresBackend --> updateInventoryVariantFieldsByProductId # Method Link: https://dev.wix.com/docs/velo/apis/wix-stores-backend/update-inventory-variant-fields-by-product-id.md # Method Description: Updates an existing inventory item's variants by product ID. 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()`](#updateInventoryVariantFields) function. # 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 product ID ```javascript /******************************* * Backend code - inventory.jsw * *******************************/ import wixStoresBackend from 'wix-stores-backend'; export function updateInventoryVariantFieldsByProductId(productId, item) { return wixStoresBackend.updateInventoryVariantFieldsByProductId(productId, item); } /************* * Page code * *************/ import { updateInventoryVariantFieldsByProductId } from 'backend/inventory'; $w.onReady(async function () { const product = await $w('#productPage1').getProduct(); let options = product.variants; let foundVariant = options.find((option) => { return option.choices.Color === "red" && option.choices.Size === "large"; }); let variantInfo = { trackQuantity: false, variants: [{ inStock: true, variantId: foundVariant._id, }] }; updateInventoryVariantFieldsByProductId(product._id, variantInfo); }); /* 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 product ID using user input ```javascript /******************************* * Backend code - inventory.jsw * *******************************/ import wixStoresBackend from 'wix-stores-backend'; export function updateInventoryVariantFieldsByProductId(id, item) { return wixStoresBackend.updateInventoryVariantFieldsByProductId(id, item); } /************* * Page code * *************/ import { updateInventoryVariantFieldsByProductId } from 'backend/inventory'; import wixData from 'wix-data'; export function updateByProductIdButton_click(event) { const productId = $w('#updateByProductIdInventoryId').value; const trackInventory = $w('#updateByProductIdTrackInventory').checked; const variantId = $w('#updateByProductIdVariantId').value; const inStock = $w('#updateByProductIdInStock').checked; const quantity = $w('#updateByProductIdQuantity').value; updateInventoryVariantFieldsByProductId(productId, { trackQuantity: trackInventory, variants: [{ variantId, inStock, quantity }] }).then(console.log("Variant inventory updated.")); } // Populate variant dropdown options // from which the user will choose. export async function updateByProductIdRefreshVariantId_click(event) { const productId = $w('#updateByProductIdInventoryId').value; const variantRefreshResults = await wixData.query('Stores/InventoryItems').eq('productId', productId).find(); const ids = variantRefreshResults.items[0].variants.map(item => { return { label: item.variantId, value: item.variantId }; }); $w('#updateByProductIdVariantId').options = ids; } /* A sample inventory item with variants is: * * { * "_id" : 133c6\-...-0fb2, * "externalId" : 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 * } */ ``` ---