Retrieving Variation-Specific Stock in WooCommerce Themes
When building a WordPress theme for WooCommerce, displaying inventory levels requires more nuance than simply calling a generic getter method. The standard approach using $product->get_stock_quantity() returns the stock level at the parent product level. This value is often derived from summing the available stock across all variations or inheriting it if no specific variation data exists. Consequently, this number remains static regardless of which specific variant a customer selects in the cart.
If you need to display the actual quantity available for a specific variationâsuch as "Large Red T-Shirt" versus "Medium Blue T-Shirt"âyou must look at the variation data object itself. The parent product does not inherently know which variation is currently selected until the user interacts with the selector or the cart state updates.
Accessing the Variation Object
To retrieve stock for a specific variation, you first need an instance of that variation. In WooCommerce's WC_Product_Variable, the available variations are stored as objects within the $product object. You can iterate through these to inspect their properties, but relying on this loop is typically reserved for debugging or initial data gathering.
foreach ($product->get_available_variations() as $variation) {
// This gives you access to the specific variation object
echo "Variation SKU: " . $variation['name'] . "<br>";
}
While iterating through available variations allows you to inspect properties like max_qty, this approach has a significant limitation. The max_qty property generally reflects the initial maximum stock set during variation creation or a calculated baseline, not the real-time dynamic stock level that changes as items are sold. If your goal is to show how many units of a specific size/color remain in inventory, relying on max_qty will result in inaccurate displays once sales occur.
Using the Selected Variation
The most accurate way to fetch variation-specific stock is to access the data associated with the variation currently selected by the user. WooCommerce stores this information in the $product object under a key that changes based on the product type and version, but it is typically accessible via get_variation().
You can check if a specific variation exists and then retrieve its quantity:
if ( $variation = $product->get_variation() ) {
echo $variation->get_stock_quantity();
} else {
echo 'No variation selected';
}
This method ensures that the stock count reflects the actual inventory for the specific SKU corresponding to the attributes chosen by the customer. If no variation is currently selected (common on product pages before a user picks an option), this check prevents errors and allows you to fallback to displaying the global stock or handling the UI state appropriately.
Handling Custom Stock Management
A common pitfall arises when products use custom stock management. If you have set "Manage stock?" to yes for both the parent product and individual variations, WooCommerce calculates stock based on the sum of child variation stocks minus sold quantities. However, if you are managing inventory manually or using plugins like WooCommerce Serial Numbers, the get_stock_quantity() method on the variation object is your primary source of truth.
It is also important to note that Twenty Fifteen (or any other default theme) might display stock simply because it checks for a specific class name or global variable. If you are seeing variation quantities without writing custom code in those themes, it is likely because the theme template files (single-product.php) already include logic to check for selected variations and render their respective stock data.
For developers extending functionality or creating new templates, always prefer querying the active variation object directly rather than iterating through all available options unless you are building a custom selector that requires pre-calculating totals. This keeps your logic aligned with WooCommerce's internal state management and ensures accuracy as inventory levels fluctuate in real-time.