WooCommerce: Display Stock Status for Product Variations

If you have products that tend to sell out in your WooCommerce store, or that you typically sell in bulk, it might be beneficial to display the available product quantity.
Get fast, reliable hosting for WordPress
Power your site with the industry’s fastest, most optimized WordPress hosting
If you have products that tend to sell out, or that you typically sell in bulk, it might be beneficial to display the available product quantity. This can ensure your customers can order the quantity or volume they need.
Displaying your stock availability is as simple as adding a quick code snippet to your theme’s function.php file.
PHP Code Snippet via Image
Here is what the code snippet would look like (captured image of the actual code snippet):

PHP Code Snippet via Text
Here is what the code snippet would look like (copiable text string of the actual code snippet):
add_action( ‘woocommerce_after_shop_loop_item’, ‘wc_echo_stock_variations_loop’ );
function wc_echo_stock_variations_loop(){
global $product;
if ( $product->get_type() == ‘variable’ ) {
foreach ( $product->get_available_variations() as $key ) {
$variation = wc_get_product( $key[‘variation_id’] );
$stock = $variation->get_availability();
$stock_string = $stock[‘availability’] ? $stock[‘availability’] : __( ‘In stock’, ‘woocommerce’ );
$attr_string = array();
foreach ( $key[‘attributes’] as $attr_name => $attr_value ) {
$attr_string[] = $attr_value;
}
echo ‘
‘ . implode( ‘, ‘, $attr_string ) . ‘: ‘ . $stock_string;
}
}
}
There is a straightforward code snippet that could be used for displaying stock status on variable products on the shop page in WooCommerce that is publicly available to the WooCommerce User Community on the following page:
The Nexcess version of that code snippet that was used in the examples above for this article is found on the following page:
Happy customizing!
Get Started With WooCommerce
Check out our fully managed WooCommerce hosting plans to get started today. Or if your store is growing fast, take a look at Liquid Web’s WooCommerce VPS solutions.
Recent Articles
- Setup & Settings for the W3 Total Cache Plugin
- Best Mailchimp Alternatives for Email Marketing
- How to Use the Nexcess Client Portal API Token
Related Articles
- How to Add Discounts to Products in WooCommerce
- Using Traditional Sales Tactics in WooCommerce
- Pull Marketing: What Is a Pull Promotion Strategy?
Additional resources
What is managed WordPress hosting? →
Get details and decide if managed WordPress hosting is right for you.
Must-have web development plugins to create WordPress websites at scale →
Explore the best web development plugins to enhance functionality, improve performance, and streamline your WordPress site.
A complete guide to WordPress shortcodes →
Shortcodes make life easier. Learn how to get started!