Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: check if product is out of stock and doesn't allow backorders

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?

like image 499
otpabu Avatar asked Jul 13 '18 14:07

otpabu


People also ask

How do I enable backorder in WooCommerce?

Backorders set to 'Allow'If you tick the 'Enable stock management at product level' box on the 'Inventory' tab for a product and choose the 'Allow' backorders option, then the stock column of the product table will label the product as being 'In Stock', with no indication that it is only available on backorder.

How do I manage backorders in WooCommerce?

Open up the product you want to put on backorder in the Edit Product screen. Scroll down to the Product data section and click on the Inventory tab. Finally, set the Stock status to On backorder using the dropdown menu.

How do I fix out of stock in WooCommerce?

To resolve this go to Product -> Variations -> click on the dropdown box which defaults to “Add Variation” -> Set Regular Prices -> click Go. Hopefully that's all it takes to fix your problem too.


Video Answer


2 Answers

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!

like image 152
Peter HvD Avatar answered Sep 17 '22 13:09

Peter HvD


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

like image 36
Syntax_Error Avatar answered Sep 20 '22 13:09

Syntax_Error