Manage Inventory
Why inventory management matters
Section titled βWhy inventory management mattersβInventory accuracy determines whether customers can buy what they see. Overselling creates fulfillment failures and refund requests. Underselling leaves revenue on the table. Commerce tracks stock at the warehouse level, giving you precise control over what is available, what is on backorder, and when to reorder.
This guide covers setting inventory records, configuring reorder points, and handling backorder scenarios.
What you will do
Section titled βWhat you will doβ- Set inventory records for variants
- Configure reorder points to trigger restocking
- Enable backorder handling for out-of-stock items
- Query inventory across warehouses
Set inventory records
Section titled βSet inventory recordsβEach inventory record ties a variant to a warehouse with a specific quantity. You must set IsTracked to true for Commerce to decrement stock on purchase.
using Mediachase.Commerce.InventoryService;
public class InventoryManager
{
private readonly IInventoryService _inventoryService;
public InventoryManager(IInventoryService inventoryService)
{
_inventoryService = inventoryService;
}
public void SetStock(string variantCode,
string warehouseCode, decimal quantity)
{
var record = new InventoryRecord
{
CatalogEntryCode = variantCode,
WarehouseCode = warehouseCode,
PurchaseAvailableQuantity = quantity,
PurchaseAvailableUtc = DateTime.UtcNow,
IsTracked = true
};
_inventoryService.Save(new[] { record });
}
public decimal GetAvailableStock(
string variantCode, string warehouseCode)
{
var records = _inventoryService.QueryByEntry(
new[] { variantCode });
return records
.Where(r => r.WarehouseCode == warehouseCode)
.Sum(r => r.PurchaseAvailableQuantity);
}
} Configure reorder points
Section titled βConfigure reorder pointsβReorder points define the stock threshold at which you should replenish inventory. Commerce does not automatically place purchase orders, but you can monitor levels and trigger notifications.
public class ReorderMonitor
{
private readonly IInventoryService _inventoryService;
private const decimal DefaultReorderPoint = 10m;
public ReorderMonitor(IInventoryService inventoryService)
{
_inventoryService = inventoryService;
}
public IEnumerable<ReorderAlert> CheckReorderLevels(
string warehouseCode)
{
var allRecords = _inventoryService
.QueryByWarehouse(warehouseCode);
return allRecords
.Where(r => r.PurchaseAvailableQuantity
<= DefaultReorderPoint)
.Select(r => new ReorderAlert
{
VariantCode = r.CatalogEntryCode,
CurrentStock = r.PurchaseAvailableQuantity,
ReorderPoint = DefaultReorderPoint,
Warehouse = warehouseCode
});
}
}
public class ReorderAlert
{
public string VariantCode { get; set; }
public decimal CurrentStock { get; set; }
public decimal ReorderPoint { get; set; }
public string Warehouse { get; set; }
} Enable backorder handling
Section titled βEnable backorder handlingβBackorders let customers purchase items that are temporarily out of stock. Set the backorder quantity and the expected availability date so customers know when to expect shipment.
public void EnableBackorder(string variantCode,
string warehouseCode, decimal backorderQty,
DateTime expectedDate)
{
var record = new InventoryRecord
{
CatalogEntryCode = variantCode,
WarehouseCode = warehouseCode,
PurchaseAvailableQuantity = 0,
PurchaseAvailableUtc = DateTime.UtcNow,
BackorderAvailableQuantity = backorderQty,
BackorderAvailableUtc = expectedDate,
IsTracked = true
};
_inventoryService.Save(new[] { record });
} | Setting | Purpose |
|---|---|
| PurchaseAvailableQuantity | Units in stock and ready to ship now |
| BackorderAvailableQuantity | Units customers can order before the restock date |
| BackorderAvailableUtc | Date when backordered items become available |
| IsTracked | Set to true to decrement on purchase; false for unlimited (digital goods) |
Common issues
Section titled βCommon issuesβ| Issue | Cause | Fix |
|---|---|---|
| Stock not decrementing after purchase | IsTracked set to false | Set IsTracked = true on the inventory record |
| Backorder option not showing | BackorderAvailableQuantity is zero | Set a positive backorder quantity and future date |
| Wrong stock shown on storefront | Querying incorrect warehouse | Verify market-to-warehouse mapping in Commerce configuration |
| Overselling despite tracking | Race condition under high concurrency | Use IInventoryService transactional methods for atomic updates |