Get started today!◦ Full contract buyout ◦ Up to $1,000 credit ◦ Free migrations
Chat with us
Home » What is WordPress Guide » WordPress with WooCommerce » WooCommerce: display stock status for product variations

WooCommerce: display stock status for product variations

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.

Get fast, reliable hosting for WordPress

Power your site with the industry’s fastest, most optimized WordPress hosting

PHP code snippet via image

Here is what the code snippet would look like (captured image of the actual code snippet):

Displaying your stock availability is as simple as adding a quick code snippet to your theme’s function.php file.

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 Liquid Web version of that code snippet that was used in the examples above for this article is found on the following page:

https://gist.github.com/lukecav/f399f606aa2a7a3b39a0e0331bd09b35

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.

Browse WooCommerce hosting plans

Recent articles

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

Additional resources

WooCommerce image size hacks →

Discover how to resize WooCommerce images for cleaner product displays.

Multi-vendor plugins for WooCommerce marketplaces →

Find multi-vendor plugins for building an online marketplace.

How to get WordPress products in Google products →

List WooCommerce products on Google to reach more shoppers.