I know you can be a bit new to this and it may seem very confusing, but I will try to explain to you so that you can solve your problem.
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 '<div class="remaining">Solo quedan ' . number_format($product->stock, 0, '', '') . ' ud. disponibles!</div>';
} else {
echo '<div class="remaining">' . number_format($product->stock, 0, '', '') . ' ud. disponibles</div>';
}
}
}
If you see, on your third line, you have something like this if ( $product->stock )
, which means you're asking the system if the product you're seeing has stock. For the system $product-stock
is a number, which tells the number of units left of the product in the stock, so if you have 4, then $product-stock
must be equal to 4, if you have 0 it should be equal to 0. For many programs, when your questions (by means of a if
) what you want to say is: 'System, in this product ( $product-stock
) is there something?' To which the system if you see any number greater than 1 tells you, YES , if there is, then execute the line that follows. Or if it is equal to 0, it answers NO , and does not execute the code that is within the {}
.
What you need is to say that if there is nothing, do something, and for that we help ourselves with a sentence called else
(It means 'If not'). So if the condition is not met, then do this, leaving the code like this:
function bbloomer_show_stock_shop() {
global $product;
if ( $product->stock ) {
if ( number_format( $product->stock, 0, '', '' ) < 4 ) {
echo '<div class="remaining">Solo quedan ' . number_format($product->stock, 0, '', '') . ' ud. disponibles!</div>';
} else {
echo '<div class="remaining">' . number_format($product->stock, 0, '', '') . ' ud. disponibles</div>';
}
} else { // Esto significa que si no hay nada, vendrá hasta acá
echo '<div class="remaining">Sin Stock</div>';
}
}
I hope I have helped and explained well, whatever you need, or doubt, do not forget to write me and comment