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.
Authorization
Request
This endpoint does not take any parameters
Response Object
Fulfilled - When the items are updated. Rejected - Error message.
Returns an empty object.
Status/Error Codes
Related Content:
Was this helpful?
Update a variant's inventory information by product ID
Copy Code
1/*******************************2 * Backend code - inventory.jsw *3 *******************************/45import wixStoresBackend from 'wix-stores-backend';67export function updateInventoryVariantFieldsByProductId(productId, item) {8 return wixStoresBackend.updateInventoryVariantFieldsByProductId(productId, item);9}1011/*************12 * Page code *13 *************/1415import { updateInventoryVariantFieldsByProductId } from 'backend/inventory';1617$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});3233/* 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" : 044 * },45 * {46 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,47 * "inStock" : false,48 * "quantity" : 049 * },50 * {51 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,52 * "inStock" : true,53 * "quantity" : 10054 * },55 * {56 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,57 * "inStock" : true,58 * "quantity" : 12359 * },60 * {61 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,62 * "inStock" : false,63 * "quantity" : 064 * },65 * {66 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,67 * "inStock" : true,68 * "quantity" : 169 * }70 * ],71 * "_updatedDate" : 2020-02-17T08:25:57.734Z72 * }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 *******************************/45import wixStoresBackend from 'wix-stores-backend';67export function updateInventoryVariantFieldsByProductId(id, item) {8 return wixStoresBackend.updateInventoryVariantFieldsByProductId(id, item);9}1011/*************12 * Page code *13 *************/1415import { updateInventoryVariantFieldsByProductId } from 'backend/inventory';16import wixData from 'wix-data';171819export 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;2526 updateInventoryVariantFieldsByProductId(productId, {27 trackQuantity: trackInventory,28 variants: [{29 variantId,30 inStock,31 quantity32 }]33 }).then(console.log("Variant inventory updated."));3435}3637// Populate variant dropdown options38// 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}4748/* 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" : 059 * },60 * {61 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,62 * "inStock" : false,63 * "quantity" : 064 * },65 * {66 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,67 * "inStock" : true,68 * "quantity" : 10069 * },70 * {71 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,72 * "inStock" : true,73 * "quantity" : 12374 * },75 * {76 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,77 * "inStock" : false,78 * "quantity" : 079 * },80 * {81 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,82 * "inStock" : true,83 * "quantity" : 184 * }85 * ],86 * "_updatedDate" : 2020-02-17T08:25:57.734Z87 * }88 */