I am using this code in showing the stocks of products:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
echo '';
}
if ( number_format($product->stock,0,'','') > 0 ) { // if stock is low
echo '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';
}
else {
echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>';
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
And if the product is a variable I use this answer code to display the stock availability:
Get the total stock of all variations from a variable product In Woocommerce
How can I merge this codes in a single conditional function?
For example. if the products is a simple product, the other code for variable product will not display.
The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.
To handle the stock availability display for other product types than variable, you can use the dedicated function
wc_get_stock_html()
instead, which will simplify the code.Code goes in function.php file of your active child theme (or active theme). Tested and works.