Stock Management
Manage physical, reserved, and available inventory levels.
Stock Management
The Stock Manager allows you to handle inventory levels for both Products and Variants. It tracks three key states:
- Physical Stock: The actual count of items sitting in the warehouse.
- Reserved Stock: Items allocated to active orders but not yet shipped (fulfilled).
- Available Stock: The quantity available for new sales (
Physical - Reserved).
Accessing the Manager
You can access the stock manager via the PIM facade.
use Obelaw\Pim\Facades\PIM;
$stockManager = PIM::stockManager();
Reading Stock Levels
Retrieve current stock levels for a Product or ProductVariant.
$physical = $stockManager->getPhysical($variant);
$reserved = $stockManager->getReserved($variant);
$available = $stockManager->getAvailable($variant);
echo "We have {$physical} items, but only {$available} are for sale.";
Modifying Stock
Reserving Stock
Used when an order is placed. This increases reserved_stock and decreases available_stock.
Throws an Exception if insufficient stock is available.
try {
$stockManager->reserve($variant, 5);
} catch (Exception $e) {
// "Local PIM: Insufficient stock. Available: 10, Requested: 15"
}
Releasing Stock
Used when an order is cancelled. This decreases reserved_stock and increases available_stock.
$stockManager->release($variant, 5);
Fulfilling Stock
Used when an order is shipped. This decreases both physical_stock and reserved_stock.
// Item has left the warehouse
$stockManager->fulfill($variant, 5);