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?
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.
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.
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.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With