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