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?
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