[wd_asp elements=’search’ ratio=’100%’ id=1]

Add how many left in stock on products stock controlled only – Woocommerce

21st January 2021

WooCommerce

woocommerce category codehaven

I have 3 products in woocommerce that I wish to sell and show how many left in stock for each one. The rest of the website is not stock controlled.
I do not want to show a label that says 745 in stock or whatever for all of them as they are always in stock. Just the clearance or on sale items in my one and only page.
The code below achieves this.


add_action('woocommerce_after_shop_loop_item','bbloomer_show_stock_shop', 10);

function bbloomer_show_stock_shop() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( number_format( $product->stock,0,'','' ) < 4 ) { // if stock is low echo '

Only ' . number_format($product->stock,0,'','') . ' left in stock!

';
}
}
}


Add the code to the functions.php

With thanks to this site for this great code https://www.businessbloomer.com/woocommerce-add-stock-quantity-on-shop-page/