WooCommerce: Display Stock Status & Quantity in the Shop Loop

You can use the below snippet to display the stock status and quantity of each product in the WooCommerce loop.

The “loop” is basically a list of all multiple products. These loops are displayed around your store.

  • The Shop Page
  • Product Category and archieve pages
  • Custom Loop
  • Loop blocks using the WooCommerce Blocks.

Code snippet

Copy and paste the snippet into your site. The snippet is based on the action hook woocommerce_after_shop_loop_item.

We’ve tested the snippet and it works with WooCommerce 5.9.

function puri_shop_loop_show_stock() {
   global $product;
   echo wc_get_stock_html( $product );
}
add_action( 'woocommerce_after_shop_loop_item', 'puri_shop_loop_show_stock', 10 );Code language: PHP (php)

Before:

You can see the default product loop in our demo store.

Screenshot of products in the WooCommerce loop.

After:

After applying the custom code snippet from above. You can see the stock quantity is now displayed for our simple products. You’ll need to modify the snippet if you’d like to use it for variable products.

Screenshot of products in the WooCommerce loop with stock status and quantity displayed.

Create informative custom stock messages

We’ve already done the hard work of creating a plugin that will allow users to easily set custom stock messages for the entire store, or individual products. You can set custom stock messages via the WooCommerce settings and even use dynamic tags! Learn more about our Stock Status For WooCommerce Plugin.

You don’t even have to use the snippet above. We also include the option to enable stock status inside the WooCommerce loop. All the features in our plugins will receive security and WooCommerce compatibility updates.

Stock Status Plugin – Edit screen
Avatar photo
Morgan

I help eCommerce store owners to run their stores smoothly and get more sales. Let's discuss optimizing your store! Hit me up via the support page or on Twitter @morganhvidt