Checking product availability and orderability in script
Key Takeaway
isInStock() checks the quantity against the stock level, but isOrderable() checks it against ATS. A backorderable or preorderable product can therefore be out of stock and still orderable.
In B2C Commerce script, you find out whether shoppers can buy a product through its ProductAvailabilityModel. The model separates two ideas. In stock means there is enough physical stock. Orderable means the product can be ordered now, which also covers backorder and preorder. The same model also returns a single availability status for display and a per-quantity breakdown for carts.
Getting the availability model
Every Product exposes availability through product.getAvailabilityModel(), which returns a dw.catalog.ProductAvailabilityModel for the current site's inventory. ProductAvailabilityModel has no constructor, so you always get it from the product.
To check a specific inventory list, call product.getAvailabilityModel(list) with a ProductInventoryList. Passing null raises an exception.
var model = product.getAvailabilityModel();
var record = model.getInventoryRecord();
if (record != null) {
var stock = record.stockLevel;
var ats = record.ATS;
}getInventoryRecord() (the inventoryRecord property) returns the product's ProductInventoryRecord, or null if the product has no record. The code checks for null before reading values such as stockLevel or ATS because a product may have no record.
Product.isAvailable() and the available property are deprecated, so use product.getAvailabilityModel().isInStock() instead. The availableFlag property and getAvailableFlag() are also deprecated in favor of getAvailabilityModel().