Nexcess Logo

WooCommerce: How to Display Stock Status for Product Variations

Knowledge Base Home

Notice anything different?

We've enhanced the appearance of our portal and we're working on updating screenshots. Things might look different, but the functionality remains the same.
April 13, 2018

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):

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 '<br/>' . 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 on the following page:

Happy customizing!

Get Started With Managed WooCommerce

Not a Nexcess customer yet? Check out our fully managed WooCommerce hosting plans to get started today.

Recent Articles

Related Articles

Luke Cavanagh
Luke Cavanagh

Luke Cavanagh, Strategic Support & Accelerant at Liquid Web, brings a decade of experience working with WordPress and WooCommerce to our product team. His GitHub page offers a glimpse into his multiple areas of subject matter expertise.

"Ninja stuff with WordPress and WooCommerce," is an apropos way to describe Luke's savviness with these platforms — and his way of influencing our organization for improving to them.

Coming out of the University of Brighton with a Business and Technology Education Council (BTEC) Higher National Diploma (HND) in 2D & 3D Design, Luke's credentials prepared him well for his current role that blends both web development and design. His HND credential leveraged his foundational learning at West Kent College, where is received a National Diploma (ND) in Graphic Design.

In his personal life, Luke is a devoted husband and teen wrangler. He considers himself a Synthwave enthusiast, Jerry Goldsmith fan, and Doctor Who aficionado. He is happy to introduce his friends and teammates to essential vocabulary for life found only in British English, such as "gubbins" and similar terms.

Paul Stubblefield
Paul Stubblefield

Nexcess Knowledge Base Owner
Content Marketing in Nexcess Digital Marketing
Nexcess, A Liquid Web Brand

Paul Stubblefield
— Technical Writer & Knowledge Management Professional

"Delivering the next generation of life-enhancing technology platforms, software solutions, and mobile-ready applications for technology pioneers, thought leaders, and market innovators in a robustly connected world."

Paul lives in Bonita Springs, Florida, USA. He is an aficionado of art, coffee, good-natured humor, lifelong learning, music, nature, pets, technology, world cultures, and his Zen Patio Garden project.

We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.