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.
Contact Us
Contact Us
Sign in
Sign in
Nexcess Logo

WooCommerce: How to Display Stock Status for Product Variations


Displaying Stock Status for Product Variations in Your WooCommerce Store


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:

https://www.businessbloomer.com/woocommerce-display-variations-name-stock-shop-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:

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

Happy customizing!

Sales Performance Monitor — Built-In Ecommerce Monitoring for Your Online Store


Personalized revenue insights, delivered right to your inbox. The Nexcess Sales Performance Monitor delivers weekly revenue trends to help you stay profitable.


Why Choose Nexcess?


Because we are different! Chris Lema captures "the why" in his passionate and stirring recount of a Nexcess support-related story.


Why Your Hosting Provider Matters


We’ve optimized hosting for the industry’s leading commerce and content platforms. That means speed, security, scalability and support are built in. Backed by free migrations and 30-day money-back guarantee, let us show you why we’re the best.

No one optimizes hosting for open-source applications better than Nexcess!



Recent Articles

Related Articles

AJ Morris
AJ Morris


AJ Morris is the Product Innovation & Marketing Manager at iThemes. He’s been involved in the WordPress community for over a decade focusing on building, designing, and launching WordPress websites and businesses.

Ronald Caldwell
Luke Cavanagh
Luke Cavanagh


Product Operations Manager at Nexcess. A devoted husband and tween wrangler. Synthwave enthusiast. Jerry Goldsmith fan. Doctor Who fan and related gubbins.

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.