Show quantity in the stock in the form of the article

0

I have this code that I got on the web, I have no idea of programming, by the way.

The function makes me show the number of products available in my store just below the article's file. When the stock is less than 4 shows "Only left ..."; The fact is that when the stock is 0 it does not show me anything and I do not know what I can add to show for example "Without stock" when the quantity is equal to 0.

Thanks in advance. Greetings.

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>'; 
    }
  }
}  
    
asked by Javi 31.08.2018 в 16:22
source

2 answers

0

A possible solution would be

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>'; 
}
}else {
    echo '<div class="remaining">Sin Stock!</div>';
 }
} 
    
answered by 31.08.2018 в 16:46
0

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

    
answered by 31.08.2018 в 16:55