WP: Can this be modified for only Low and Zero Stock?
Posted: Tue Oct 10, 2017 3:34 am
I want to alter this so if there is >0 and <3 in stock, then show
But if there is zero in stock, show:
But forget about the rest. So if it's 4+, then it shows nothing.
I tried this but it broke it:
Code: Select all
echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';Code: Select all
echo '<div class="remaining">Out of Stock</div>';Code: Select all
// Add Stock Quantity on Shop Page
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( number_format($product->stock,0,'','') < 3 ) { // if stock is low
echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';
} else {
echo '<div class="remaining">' . number_format($product->stock,0,'','') . ' left in stock</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);Code: Select all
// Add Stock Quantity on Shop Page
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( number_format($product->stock,0,'','') == 0 ) { // if stock zero
echo '<div class="remaining">Out of stock</div>';
}
else
if (( number_format($product->stock,0,'','') < 3 )&& ( number_format($product->stock,0,'','') > 1 ){ // if stock is low
echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';
}
}
}