Commerce Inventory API Reference
Overview
Section titled “Overview”The Inventory API manages stock levels, reservations, and warehouse assignments. IInventoryService is the primary interface for inventory operations. IWarehouseRepository manages warehouse definitions.
Namespace
Section titled “Namespace”Optimizely.Commerce.Inventory
Key interfaces
Section titled “Key interfaces”| Interface | Purpose |
|---|---|
IInventoryService | Read and adjust inventory levels |
IWarehouseRepository | CRUD for warehouse definitions |
IInventoryProcessor | Transactional inventory operations (reserve, purchase, backorder) |
IInventoryService
Section titled “IInventoryService”QueryByEntry(IEnumerable<string>)
Section titled “QueryByEntry(IEnumerable<string>)”Returns inventory records for catalog entry codes.
Parameters
| Name | Type | Description |
|---|---|---|
entryCodes | IEnumerable<string> | Catalog entry codes to query |
Returns: IEnumerable<InventoryRecord>
QueryByWarehouse(WarehouseCode)
Section titled “QueryByWarehouse(WarehouseCode)”Returns all inventory records for a warehouse.
Parameters
| Name | Type | Description |
|---|---|---|
warehouseCode | WarehouseCode | Warehouse identifier |
Returns: IEnumerable<InventoryRecord>
public class InventoryLookup
{
private readonly IInventoryService _inventoryService;
public InventoryLookup(IInventoryService inventoryService)
{
_inventoryService = inventoryService;
}
public decimal GetTotalStock(string skuCode)
{
var records = _inventoryService.QueryByEntry(new[] { skuCode });
return records.Sum(r => r.PurchaseAvailableQuantity);
}
} Save(IEnumerable<InventoryRecord>)
Section titled “Save(IEnumerable<InventoryRecord>)”Creates or updates inventory records.
Parameters
| Name | Type | Description |
|---|---|---|
records | IEnumerable<InventoryRecord> | Inventory records to save |
Returns: void
Throws: InventoryException if warehouse code or entry code is invalid
Delete(IEnumerable<InventoryKey>)
Section titled “Delete(IEnumerable<InventoryKey>)”Removes inventory records.
Parameters
| Name | Type | Description |
|---|---|---|
keys | IEnumerable<InventoryKey> | Composite keys (entry code + warehouse code) |
Returns: void
InventoryRecord properties
Section titled “InventoryRecord properties”| Property | Type | Description |
|---|---|---|
CatalogEntryCode | string | SKU code |
WarehouseCode | string | Warehouse identifier |
PurchaseAvailableQuantity | decimal | Quantity available for purchase |
PreorderAvailableQuantity | decimal | Quantity available for preorder |
BackorderAvailableQuantity | decimal | Quantity available for backorder |
PurchaseAvailableUtc | DateTime | When stock becomes available |
IsTracked | bool | Whether inventory is tracked for this entry |
public void SetStock(string skuCode, string warehouseCode, decimal quantity)
{
var record = new InventoryRecord
{
CatalogEntryCode = skuCode,
WarehouseCode = warehouseCode,
PurchaseAvailableQuantity = quantity,
PurchaseAvailableUtc = DateTime.UtcNow,
IsTracked = true
};
_inventoryService.Save(new[] { record });
} IInventoryProcessor
Section titled “IInventoryProcessor”Handles transactional inventory operations during checkout.
AdjustInventoryOrRemoveLineItem(IShipment)
Section titled “AdjustInventoryOrRemoveLineItem(IShipment)”Validates and adjusts inventory for a shipment. Removes line items with insufficient stock.
Parameters
| Name | Type | Description |
|---|---|---|
shipment | IShipment | Shipment containing line items to validate |
Returns: IDictionary<ILineItem, IList<ValidationIssue>>
RequestInventory operations
Section titled “RequestInventory operations”| Method | Description |
|---|---|
Reserve(IOrderGroup) | Reserves stock for all line items in the order |
Purchase(IOrderGroup) | Converts reservations to purchases (decrements stock) |
Cancel(IOrderGroup) | Releases reservations without purchasing |
public IDictionary<ILineItem, IList<ValidationIssue>> ReserveStock(ICart cart)
{
var validationIssues = _inventoryProcessor
.AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment());
return validationIssues;
} IWarehouseRepository
Section titled “IWarehouseRepository”| Method | Returns | Description |
|---|---|---|
Get(string code) | IWarehouse | Get warehouse by code |
List() | IEnumerable<IWarehouse> | List all warehouses |
Save(IWarehouse) | void | Create or update a warehouse |
Delete(string code) | void | Delete a warehouse |
IWarehouse properties
Section titled “IWarehouse properties”| Property | Type | Description |
|---|---|---|
Code | string | Unique warehouse code |
Name | string | Display name |
IsActive | bool | Whether the warehouse is operational |
IsFulfillmentCenter | bool | Can fulfill orders |
IsPickupLocation | bool | Available for in-store pickup |
ContactInformation | WarehouseContactInformation | Address and contact details |
Common exceptions
Section titled “Common exceptions”| Exception | Cause |
|---|---|
InventoryException | Invalid warehouse or entry code |
InsufficientStockException | Not enough stock to fulfill the request |
InventoryRequestException | Error during reservation or purchase |