> 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 # UpdateInventoryVariants # Package: catalogV1 # Namespace: InventoryWriteApi # Method link: https://dev.wix.com/docs/api-reference/business-solutions/stores/catalog-v1/inventory/update-inventory-variants.md ## Permission Scopes: Manage Products: SCOPE.DC-STORES.MANAGE-PRODUCTS ## Introduction Updates product inventory, including total quantity, whether the product is in stock, and whether the product inventory is tracked. --- ## REST API ### Schema ``` Method: updateInventoryVariants Description: Updates product inventory, including total quantity, whether the product is in stock, and whether the product inventory is tracked. URL: https://www.wixapis.com/stores/v2/inventoryItems/product/{inventoryItem.productId} Method: PATCH # Note: If the parameter `a.b` is listed under required parameters, `b` is only required if `a` is also present. Required parameters: inventoryItem, inventoryItem.productId Method parameters: param name: inventoryItem | type: InventoryItemV2 | required: true - name: id | type: string | description: Inventory item GUID. - name: productId | type: string | description: Product GUID. | required: true - name: trackQuantity | type: boolean | description: Whether quantity is being tracked. - name: variants | type: array | description: Variants associated with this inventory item. - name: variantId | type: string | description: Variant GUID. - name: inStock | type: boolean | description: Whether the product is listed as in stock. - name: quantity | type: integer | description: Quantity currently left in inventory. - name: preorderInfo | type: PreorderInfo | description: Preorder information. - name: enabled | type: boolean | description: Whether the item is available for preorder. - name: message | type: string | description: A message the buyer will see when the item is out of stock and preorder is enabled. - name: limit | type: integer | description: Number of products that can be preordered after stock reaches zero. Return type: UpdateInventoryVariantsResponse EMPTY-OBJECT {} ``` ### Examples ### UpdateInventoryVariants ```curl ~~~cURL curl -X PATCH https://www.wixapis.com/stores/v2/inventoryItems/0ff8f8b0-2857-5974-193e-8283c21fde00 \ -H 'Content-Type: application/json' \ -H 'Authorization: ' \ --data-binary ' { "inventoryItem": { "trackQuantity": false, "variants": [ { "variantId": "00000000-0000-0000-0000-000000000000", "inStock": false } ], "numericId": 0, "preorderInfo": { "enabled": true, "limit": 6, "message": "Expected to ship on June" } } }' ~~~ ``` ### UpdateInventoryVariants ```curl ~~~cURL curl -X PATCH https://www.wixapis.com/stores/v2/inventoryItems/0ff8f8b0-2857-5974-193e-8283c21fde00 \ -H 'Content-Type: application/json' \ -H 'Authorization: ' \ --data-binary ' { "inventoryItem": { "trackQuantity": false, "variants": [ { "variantId": "00000000-0000-0001-0005-9a596c8e6f10", "inStock": true }, { "variantId": "00000000-0000-0002-0005-9a596c8e6f10", "inStock": true } ], "numericId": 0, "preorderInfo": { "enabled": true, "limit": 6, "message": "Expected to ship on June" } } }' ~~~ ``` ### UpdateInventoryVariants ```curl ~~~cURL curl -X PATCH https://www.wixapis.com/stores/v2/inventoryItems/0ff8f8b0-2857-5974-193e-8283c21fde00 \ -H 'Content-Type: application/json' \ -H 'Authorization: ' \ --data-binary ' { "inventoryItem": { "trackQuantity": true, "variants": [ { "variantId": "00000000-0000-0000-0000-000000000000", "quantity": 1 } ], "numericId": 0, "preorderInfo": { "enabled": true, "limit": 4, "message": "Expected to ship on June" } } }' ~~~ ``` --- ## JavaScript SDK ### Schema ``` Method: wixClientAdmin.catalogV1.InventoryWriteApi.updateInventoryVariants(productId, inventoryItem) Description: Updates product inventory, including total quantity, whether the product is in stock, and whether the product inventory is tracked. # Note: If the parameter `a.b` is listed under required parameters, `b` is only required if `a` is also present. Required parameters: inventoryItem, productId Method parameters: param name: inventoryItem | type: UpdateInventoryVariants | required: true - name: _id | type: string | description: Inventory item GUID. - name: trackQuantity | type: boolean | description: Whether quantity is being tracked. - name: variants | type: array | description: Variants associated with this inventory item. - name: variantId | type: string | description: Variant GUID. - name: inStock | type: boolean | description: Whether the product is listed as in stock. - name: quantity | type: integer | description: Quantity currently left in inventory. - name: preorderInfo | type: PreorderInfo | description: Preorder information. - name: enabled | type: boolean | description: Whether the item is available for preorder. - name: message | type: string | description: A message the buyer will see when the item is out of stock and preorder is enabled. - name: limit | type: integer | description: Number of products that can be preordered after stock reaches zero. param name: productId | type: string | description: Product GUID. | required: true Return type: PROMISE EMPTY-OBJECT {} ``` ### Examples ### Update a variant's inventory information by product ID (with $w) ```javascript /************************************** * Backend code - inventory.web.js/ts * **************************************/ import { Permissions, webMethod } from '@wix/web-methods'; import { inventory } from '@wix/stores'; export const updateInventoryVariantFieldsByProductId = webMethod(Permissions.Anyone, (productId, inventoryItem) => { return inventory.updateInventoryVariants(productId, inventoryItem); }); /************* * Page code * *************/ import { updateInventoryVariantFieldsByProductId } from 'backend/inventory.web'; $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 inventoryItem = { trackQuantity: false, variants: [{ inStock: true, variantId: foundVariant._id, }] }; updateInventoryVariantFieldsByProductId(product._id, inventoryItem); }); /* 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 (with $w) 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. ```javascript /************************************** * Backend code - inventory.web.js/ts * **************************************/ import { Permissions, webMethod } from '@wix/web-methods'; import { inventory } from '@wix/stores'; export const updateInventoryVariantFieldsByProductId = webMethod(Permissions.Anyone, (productId, inventoryItem) => { return inventory.updateInventoryVariants(productId, inventoryItem); }); /************* * Page code * *************/ import { updateInventoryVariantFieldsByProductId } from 'backend/inventory.web'; import { items } from '@wix/data'; export function updateByProductIdButton_click(event) { const productId = $w('#updateByProductIdInventoryId').value; const trackQuantity = $w('#updateByProductIdTrackInventory').checked; const variantId = $w('#updateByProductIdVariantId').value; const inStock = $w('#updateByProductIdInStock').checked; const quantity = $w('#updateByProductIdQuantity').value; updateInventoryVariantFieldsByProductId(productId, { trackQuantity, 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 items.query('Stores/InventoryItems').eq('productId', productId).find(); const ids = variantRefreshResults.items[0].variants.map(variant => { return { label: variant.variantId, value: variant.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 * } */ ``` ### Update multiple variants inventory information Update inventory for multiple variants with preorder information ```javascript import { inventory } from "@wix/stores"; const productId = "c43d36c5-c35a-91ec-1c33-d28efc70d5a6"; const inventoryItem = { trackQuantity: true, variants: [ { variantId: "00000000-0000-0001-0005-9a596c8e6f10", inStock: true, quantity: 10 }, { variantId: "00000000-0000-0002-0005-9a596c8e6f10", inStock: true, quantity: 10 } ] }; async function updateInventoryVariants() { const response = await inventory.updateInventoryVariants(productId, inventoryItem); return response; } const result = await inventory.updateInventoryVariants(productId, inventoryItem); ``` ### updateInventoryVariants (self-hosted) Self-hosted SDK calls require you to [create a client](https://dev.wix.com/docs/sdk/articles/work-with-the-sdk/about-the-wix-client.md). ```javascript import { createClient } from '@wix/sdk'; import { inventory } from '@wix/stores'; // Import the auth strategy for the relevant access type // Import the relevant host module if needed const myWixClient = createClient ({ modules: { inventory }, // Include the auth strategy and host as relevant }); async function updateInventoryVariants(productId,inventoryItem) { const response = await myWixClient.inventory.updateInventoryVariants(productId,inventoryItem); }; ``` ---