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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…