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()
function. The product ID is the corresponding product ID for the inventory item in a Wix store.
function updateInventoryVariantFields(
inventoryId: string,
inventoryInfo: InventoryItemVariantInfo,
): Promise<void>;
Inventory ID of the item with variants to update.
The inventory information to update in the variant.
/*******************************
* 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
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.