5
votes

How do I check if a product is out of stock (stock quantity 0) and doesn't allow backorders? The following code doesn't work for some reason.

add_action('woocommerce_before_add_to_cart_button','show_stock_single');
function show_stock_single() {
  global $product;
  if($product->get_stock_quantity()<1) {
    if($product->backorders_allowed()) echo '<p>Backorders allowed</p>';
    else echo '<p>Backorders not allowed</p>';
  }
  else echo '<p>Available</p>';
}

It shows "backorders allowed" for products that allow backorders, but nothing if backorders are not allowed. Why?

2
Do you mean that you get nothing on your screen at all or just that you do not see the text "Backorders not allowed" where you expect to (but everything else on the page appears as expected)?Patrick Q
Everything works but I don't see "Backorders not allowed".otpabu
Have you confirmed that a) the function gets called in this case and b) you get inside the outer if (the quantity one) in this case? There doesn't appear to be anything inherently wrong with the code itself, so it is likely a case of the code not following the logical path that you are expecting. See HERE for a basic mock of your code working as expected.Patrick Q
It looks like the "woocommerce_before_add_to_cart_button" hook doesn't fire when back orders aren't allowed, hence your function (and any other function hooked to it) never gets called.Thapedict

2 Answers

10
votes

Whenever I get an if statement that's not doing as I think it should, the first thing I try is to reverse it, so give this a go...

if($product->get_stock_quantity()>0) {
    echo '<p>Available</p>';
} else {
    if($product->backorders_allowed()) { 
        echo '<p>Backorders allowed</p>';
    } else {
        echo '<p>Backorders not allowed</p>';
    }
}

- I'd also make sure you're enclosing the various if bits with the {...} brackets. I know PHP is supposed to allow a more relaxed requirement with single line ifs, but it might be the cause!

5
votes

When I have problems with a certain code I always check that each variable is returning the correct value.

In this case I separated each section to see what could be failing, and also I changed the Hook, to see that nothing could be causing some conflict.

Sorry if the code got too long. I commented in each part to help to understand what each part does. Also I added images of each result.

Case 1 https://i.stack.imgur.com/rmu5m.jpg

Case 2 https://i.stack.imgur.com/Zxnrt.jpg

Case 3 https://i.stack.imgur.com/v8Tzc.jpg

add_action('woocommerce_single_product_summary','show_stock_single',5);
function show_stock_single() {
  global $product;
  $StockQ=$product->get_stock_quantity();

  if ($StockQ>=1)//Stock is Available
  {
    echo "<p>Available</p>";
  }
  elseif($StockQ<1)//Product is Out of Stock
  {
    echo "<p>Out of Stock</p>";
    if ($product->backorders_allowed())//Product is out of stock AND allow backorders
    {
        echo "<p>Backorder Allowed</p>";
    }
    else//Product is out of stock AND DO NOT allow backorders
    {
        echo "<p>Backorder NOT Allowed</p>";
    }
  }
}

NOTE: The theme I use is Storefront to avoid any plugin/template conflict