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.
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.
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.