updateInventoryVariantFields( )
Updates an existing inventory item's variants by inventory ID.
Description
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()
function. The product ID is the corresponding product ID for the inventory item in a Wix store.
Syntax
function updateInventoryVariantFields(inventoryId: string, inventoryInfo: InventoryItemVariantInfo): Promise<void>
updateInventoryVariantFields Parameters
NAME
TYPE
DESCRIPTION
Inventory ID of the item with variants to update.
The inventory information to update in the variant.
Returns
Fulfilled - When the items are updated. Rejected - Error message.
Return Type:
Related Content:
Was this helpful?
1/*******************************2 * Backend code - inventory.jsw *3 *******************************/45import wixStoresBackend from 'wix-stores-backend';67export function updateInventoryVariantFields(id, item) {8 return wixStoresBackend.updateInventoryVariantFields(id, item);9}1011export function getProductVariantsBackend(productId, options) {12 return wixStoresBackend.getProductVariants(productId, options);13}1415/*************16 * Page code *17 *************/1819import wixData from 'wix-data';20import { updateInventoryVariantFields, getProductVariantsBackend } from 'backend/inventory';2122$w.onReady(async function () {2324 // we want to change the inventory info for25 // a small, blue suit.26 let item = "Suit";27 let size = "small";28 let color = "blue";2930 // query to get the product ID and inventory ID for a suit31 let myProductDetails = await getMyProduct(item);32 let myProductId = myProductDetails[0];33 let myInventoryId = myProductDetails[1];3435 // query to get the variant ID for the small, blue suit36 let myVariantId = await getMyVariant(myProduct, size, color);3738 // set up the inventory information to update for the variant39 let myVariantData = {40 "trackQuantity": true,41 "variants": [{42 "quantity": 1,43 "variantId": myVariantId44 }]45 };4647 try {48 // run the function to update the inventory for the variant49 await updateInventoryVariantFields(myInventoryId, myVariantData);50 }51 catch (err) {52 // handle the error53 }54});5556export function getMyProduct(theItem) {57 return wixData.query("Stores/Products")58 .eq("name", theItem)59 .find()60 .then((results) => {61 if (results.items.length > 0) {62 return [results.items[0]._id, results.items[0].inventoryItem];63 } else {64 // handle the error65 return;66 };67 });68};6970export function getMyVariant(productId, size, color) {71 let productOptions = {72 "choices": {73 "Size": size,74 "Color": color75 }76 };77 return getProductVariantsBackend(productId, productOptions)78 .then((variantResults) => {79 if (variantResults.items.length > 0) {80 return variantResults.items[0]._id;81 } else {82 // handle the error83 return84 }85 })86}8788/* A sample inventory item with variants is:89 *90 * {91 * "_id" : 133c6\-...-0fb2,92 * "productId" : ecc3-...-1f04d,93 * "trackQuantity" : true,94 * "variants" : -[95 * {96 * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270,97 * "inStock" : false,98 * "quantity" : 099 * },100 * {101 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,102 * "inStock" : false,103 * "quantity" : 0104 * },105 * {106 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,107 * "inStock" : true,108 * "quantity" : 100109 * },110 * {111 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,112 * "inStock" : true,113 * "quantity" : 123114 * },115 * {116 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,117 * "inStock" : false,118 * "quantity" : 0119 * },120 * {121 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,122 * "inStock" : true,123 * "quantity" : 1124 * }125 * ],126 * "_updatedDate" : 2020-02-17T08:25:57.734Z127 * }128 */
This example demonstrates how to update:
- The inventory amount if inventory is being tracked.
- The in-stock/out-of-stock status if inventory is not being tracked.
1/*******************************2 * Backend code - inventory.jsw *3 *******************************/45import wixStoresBackend from 'wix-stores-backend';67export function updateInventoryVariantFields(id, item) {8 return wixStoresBackend.updateInventoryVariantFields(id, item);9}1011/*************12 * Page code *13 *************/1415import { updateInventoryVariantFields } from 'backend/inventory';1617const productId = // get product ID by query, user input, or from a productPage element18const variantId = // get variant ID by query, user input, `getProductVariants()', or from a productPage element19const productTrackQuantity = // determine if inventory is being tracked2021// check if quantity can be tracked for this product22if (productTrackQuantity) {23 let item = {24 "trackQuantity": productTrackQuantity,25 "variants": [{26 "variantId": variantId,27 "quantity": 1028 }]29 };30} else {31 let item = {32 "trackQuantity": productTrackQuantity,33 "variants": [{34 "variantId": variantId,35 "inStock": true36 }]37 };38}3940updateInventoryVariantFields(productId, item)41 .then((updateResults) => {42 // inventory item variant data have been updated43 })44 .catch((error) => {45 // there was an error updating the inventory item variant data46 });474849/* A sample inventory item with variants is:50 *51 * {52 * "_id" : 133c6\-...-0fb2,53 * "productId" : ecc3-...-1f04d,54 * "trackQuantity" : true,55 * "variants" : -[56 * {57 * "variantId" : 00000000-0000-0020-0005-9ec14dfb2270,58 * "inStock" : false,59 * "quantity" : 060 * },61 * {62 * "variantId" : 00000000-0000-0021-0005-9ec14dfb2270,63 * "inStock" : false,64 * "quantity" : 065 * },66 * {67 * "variantId" : 00000000-0000-0022-0005-9ec14dfb2270,68 * "inStock" : true,69 * "quantity" : 1070 * },71 * {72 * "variantId" : 00000000-0000-003f-0005-9ec14dfb2270,73 * "inStock" : true,74 * "quantity" : 12375 * },76 * {77 * "variantId" : 00000000-0000-0040-0005-9ec14dfb2270,78 * "inStock" : false,79 * "quantity" : 080 * },81 * {82 * "variantId" : 00000000-0000-0041-0005-9ec14dfb2270,83 * "inStock" : true,84 * "quantity" : 185 * }86 * ],87 * "_updatedDate" : 2020-02-17T08:25:57.734Z88 * }89 */