Skip to main content
Skip to main content

How to Manage Item Allocations in Orders

In this document, you’ll learn how to manage item allocations in orders using the admin APIs.

Note

The terms “item allocation” and “reservation” are synonymous and are used interchangeably throughout this guide.

Overview

When an order is placed, the ordered quantity is reserved from the stocked quantity of each product variant having manage_inventory enabled. The reserved quantity is allocated to a random location associated with the order’s sales channel, but can also be changed afterward.

When a fulfillment is created, a location can be specified to fulfill the item from. This will deduct the stocked quantity and reset the reserved quantity in the chosen location based on the quantity chosen for fulfillment.

When a return is requested, a location can be specified to return the item to. The will increment the stocked quantity in the chosen location based on the returned quantity.

Item Allocation Diagram

Scenario

You want to add or use the following admin functionalities:

  • Manage an item allocation for any resource, and not just for an item in an order. This includes listing, updating, or deleting item allocation.
  • Specify location when creating fulfillment.
  • Specify location when creating a return. This also applies to returns that are part of a swap or a claim.

Prerequisites

Medusa Components

It is assumed that you already have a Medusa backend installed and set up. If not, you can follow the quickstart guide to get started.

Required Module

This guide assumes you have a stock location and inventory modules installed. You can use Medusa’s Stock Location and Inventory modules or create your own modules.

JS Client

This guide includes code snippets to send requests to your Medusa backend using Medusa’s JS Client, among other methods.

If you follow the JS Client code blocks, it’s assumed you already have Medusa’s JS Client installed and have created an instance of the client.

Medusa React

This guide also includes code snippets to send requests to your Medusa backend using Medusa React, among other methods.

If you follow the Medusa React code blocks, it's assumed you already have Medusa React installed and have used MedusaProvider higher in your component tree.

Authenticated Admin User

You must be an authenticated admin user before following along with the steps in the tutorial.

You can learn more about authenticating as an admin user in the API reference.


Manage Item Allocations in an Order

Using the reservations admin REST APIs, you can create an item allocation or reservation for any resource, and not just items in an order. This gives you more options in how you handle item allocations.

In this guide, however, the focus will be on how to use these API Routes for order-related functionalities.

Create Item Allocation

Item allocations are created automatically for items that are associated with product variants having the manage_inventory attribute enabled. You typically don’t need to create an item allocation, unless you delete the previous item allocation.

You can create an item allocation by sending a request to the Create a Reservation API Route:

medusa.admin.reservations.create({
line_item_id,
location_id,
inventory_item_id,
quantity,
})
.then(({ reservation }) => {
console.log(reservation.id)
})

This API Route requires the following body parameters:

  • line_item_id: The ID of the order’s line item the allocation is being created for.
  • location_id: The ID of the location the item is being allocated from.
  • inventory_item_id: The ID of the inventory item the line item’s variant is associated with.
  • quantity: The quantity to allocate.

The request returns the created reservation as an object.

List Item Allocations

When listing item allocations, by default, you’ll be retrieving all item allocations in your commerce system. You can, however, provide optional fields to filter the item allocations retrieved.

You can retrieve the item allocations of a line item in an order using the List Reservations API Route:

medusa.admin.reservations.list({
line_item_id,
})
.then(({ reservations, count, limit, offset }) => {
console.log(reservations.length)
})

This API Route doesn't require any path or query parameters. As mentioned earlier, you can pass query parameters to filter the reservations. In the code snippets above, you filter the reservations by a line item ID. You can, however, filter by other attributes, such as the ID of the location. You can refer to the API reference for a full list of query parameters.

The request returns the reservations along with pagination fields.

Retrieve Item Allocation

You can retrieve a single item allocation by its ID using the Get a Reservation API Route:

medusa.admin.reservations.retrieve(reservationId)
.then(({ reservation }) => {
console.log(reservation.id)
})

This API Route requires the reservation’s ID as a path parameter.

The request returns the reservation as an object.

Update Item Allocation

You can update an item allocation to change the location to allocate from or the quantity to allocate by sending a request to the Update Reservation API Route:

medusa.admin.reservations.update(reservationId, {
quantity,
})
.then(({ reservation }) => {
console.log(reservation.id)
})

This API Route requires the ID of the reservation as a path parameter.

In the request body parameters, you can optionally pass any of the following parameters to make updates to the reservation:

  • quantity: The quantity that should be reserved.
  • location_id: The ID of the location that the item should be allocated from.
  • metadata: set or change the reservation’s metadata.

The request returns the updated reservation as an object.

Delete Item Allocation

Deleting an item allocation means that the quantity that was previously reserved is no longer reserved.

You can delete an item allocation by sending a request to the Delete Reservation API Route:

medusa.admin.reservations.delete(reservationId)
.then(({ id, object, deleted }) => {
console.log(id)
})

This API Route requires the reservation ID to be passed as a path parameter.

The request returns the following fields:

  • id: The ID of the reservation.
  • object: The type of object that was removed. In this case, the value will be reservation.
  • deleted: A boolean value indicating whether the reservation was successfully deleted.

Specify Location when Creating Fulfillment

When you create a fulfillment of an order, you can specify the location to fulfill the item from by passing the location_id parameter:

medusa.admin.orders.createFulfillment(orderId, {
items: [
{
item_id,
quantity,
},
],
// ...other parameters
location_id,
})
.then(({ order }) => {
console.log(order.id)
})

The location_id is an optional parameter that allows you to specify where to fulfill the item from. This subsequently decrements the stock quantity of the product variant in that location.

You can learn more about this API Route's parameters and response in the API reference.


Specify Location when Requesting Return

When requesting a return, you can specify the location to return the item to by passing the location_id parameter:

medusa.admin.orders.requestReturn(orderId, {
items: [
{
item_id,
quantity: 1,
},
],
// other parameters...
location_id,
})
.then(({ order }) => {
console.log(order.returns)
})

The location_id is an optional parameter that allows you to specify where to return the item to. This subsequently increments the stock quantity of the product variant in that location.

You can learn more about this API Route's parameters and response in the API reference.


See Also

Was this section helpful?