Skip to content

Commerce Inventory API Reference

advanced
📜Corecommerce

The Inventory API manages stock levels, reservations, and warehouse assignments. IInventoryService is the primary interface for inventory operations. IWarehouseRepository manages warehouse definitions.

Optimizely.Commerce.Inventory

InterfacePurpose
IInventoryServiceRead and adjust inventory levels
IWarehouseRepositoryCRUD for warehouse definitions
IInventoryProcessorTransactional inventory operations (reserve, purchase, backorder)

Returns inventory records for catalog entry codes.

Parameters

NameTypeDescription
entryCodesIEnumerable<string>Catalog entry codes to query

Returns: IEnumerable<InventoryRecord>

Returns all inventory records for a warehouse.

Parameters

NameTypeDescription
warehouseCodeWarehouseCodeWarehouse identifier

Returns: IEnumerable<InventoryRecord>

Get stock for an entry
csharp
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);
  }
}

Creates or updates inventory records.

Parameters

NameTypeDescription
recordsIEnumerable<InventoryRecord>Inventory records to save

Returns: void

Throws: InventoryException if warehouse code or entry code is invalid

Removes inventory records.

Parameters

NameTypeDescription
keysIEnumerable<InventoryKey>Composite keys (entry code + warehouse code)

Returns: void

PropertyTypeDescription
CatalogEntryCodestringSKU code
WarehouseCodestringWarehouse identifier
PurchaseAvailableQuantitydecimalQuantity available for purchase
PreorderAvailableQuantitydecimalQuantity available for preorder
BackorderAvailableQuantitydecimalQuantity available for backorder
PurchaseAvailableUtcDateTimeWhen stock becomes available
IsTrackedboolWhether inventory is tracked for this entry
Set stock levels
csharp
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 });
}

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

NameTypeDescription
shipmentIShipmentShipment containing line items to validate

Returns: IDictionary<ILineItem, IList<ValidationIssue>>

MethodDescription
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
Reserve inventory at checkout
csharp
public IDictionary<ILineItem, IList<ValidationIssue>> ReserveStock(ICart cart)
{
  var validationIssues = _inventoryProcessor
      .AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment());

  return validationIssues;
}
MethodReturnsDescription
Get(string code)IWarehouseGet warehouse by code
List()IEnumerable<IWarehouse>List all warehouses
Save(IWarehouse)voidCreate or update a warehouse
Delete(string code)voidDelete a warehouse
PropertyTypeDescription
CodestringUnique warehouse code
NamestringDisplay name
IsActiveboolWhether the warehouse is operational
IsFulfillmentCenterboolCan fulfill orders
IsPickupLocationboolAvailable for in-store pickup
ContactInformationWarehouseContactInformationAddress and contact details
ExceptionCause
InventoryExceptionInvalid warehouse or entry code
InsufficientStockExceptionNot enough stock to fulfill the request
InventoryRequestExceptionError during reservation or purchase