Written on 27. October 2020 by Lennart Christensen

How to visualize on frontend as: 10+ in stock in Woocommerce.

If you state the number of stocks in your webshop, you may want to limit the number if the number of stocks exceeds a certain number. Eg 10. here I have a function that can show quantity in stock if the product is in stock. If the product is not in stock, it says "Out of stock.

How to visualize on frontend as: 10+ in stock in Woocommerce.
if you indicate stock quantities in your webshop, you may want to limit the number if the stock quantity exceeds a certain number. For example 10. here I have a function that can show the number in stock if the product is in stock. If the product is not in stock it says "Not in stock.

/*--------------------------------------------------------------
Single-product availability @@ lennartc
--------------------------------------------------------------*/
function wcStock($availability)
{
    global $product;
	if ($availability === 'outofstock') {
	echo '' . __('Product is currently unavailable', 'cco') . '';
	}
	if ($availability === 'instock') {
	    	    $antal = $product->get_stock_quantity();
	    	    if ($antal > 10) {
			        $antal = '10+';
                }
	echo '<span class="your_styles"&gt';
	    	    echo $antal ? $antal . ' ' : ''; echo __('In stock', 'cco') .' - '. __('Fast delivery', 'cco') .'</span&gt';
	}
}

The above code can be used and you can edit in it. The texts are in Danish and can be translated with your theme's textdomain, in the above example 'cco'.

Use the function as follows:

The above code can be used and you can edit it. The texts are in English and can be translated with your theme's text domain, in the above example 'cco'.

Use the function like this:

 wcStock($product->get_stock_status());