confirmVariantInventoryWorkflow - Medusa Core Workflows Reference

This documentation provides a reference to the confirmVariantInventoryWorkflow. It belongs to the @medusajs/medusa/core-flows package.

This workflow validates that product variants are in-stock at the specified sales channel, before adding them or updating their quantity in the cart. If a variant doesn't have sufficient quantity in-stock, the workflow throws an error. If all variants have sufficient inventory, the workflow returns the cart's items with their inventory details.

This workflow is useful when confirming that a product variant has sufficient quantity to be added to or updated in the cart. It's executed by other cart-related workflows, such as addToCartWorkflow, to confirm that a product variant can be added to the cart at the specified quantity.

NoteLearn more about the links between the product variant and sales channels and inventory items in this documentation.

You can use this workflow within your own customizations or custom workflows, allowing you to check whether a product variant has enough inventory quantity before adding them to the cart.

Source Code

Examples#

You can retrieve a variant's required details using Query:

Code
1const { data: variants } = await query.graph({2  entity: "variant",3  fields: [4    "id",5    "manage_inventory",6    "inventory_items.inventory_item_id",7    "inventory_items.required_quantity",8    "inventory_items.inventory.requires_shipping",9    "inventory_items.inventory.location_levels.stock_locations.id",10    "inventory_items.inventory.location_levels.stock_locations.name",11    "inventory_items.inventory.location_levels.stock_locations.sales_channels.id",12    "inventory_items.inventory.location_levels.stock_locations.sales_channels.name",13  ],14  filters: {15    id: ["variant_123"]16  }17})
NoteIn a workflow, use useQueryGraphStep instead.

Then, pass the variant's data with the other required data to the workflow:

When updating an item quantity:

Steps#

Workflow Hook

Step conditioned by when

Input#

ConfirmVariantInventoryWorkflowInputDTOConfirmVariantInventoryWorkflowInputDTO
The details necessary to check whether the variant has sufficient inventory.
sales_channel_idstring
The ID of the sales channel to check the inventory availability in.
variantsobject[]
The variants to confirm they have sufficient in-stock quantity.
itemsobject[]
The items in the cart, or to be added.
itemsToUpdateobject[]Optional
The new quantity of the variant to be added to the cart. This is useful when updating a variant's quantity in the cart.

Output#

ConfirmVariantInventoryWorkflowOutputConfirmVariantInventoryWorkflowOutput
The details of the cart items with inventory result computed for the specified input.
itemsobject[]
The cart's line items with the computed inventory result.
Was this page helpful?