Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Created November 8, 2023 10:54
Show Gist options
  • Save hslaszlo/871d4e93f453b3329b6c15bb8351a159 to your computer and use it in GitHub Desktop.
Save hslaszlo/871d4e93f453b3329b6c15bb8351a159 to your computer and use it in GitHub Desktop.
WooCommerce: check if product is out of stock and doesn't allow backorders
<?php
/*
// source: https://stackoverflow.com/questions/51327748/woocommerce-check-if-product-is-out-of-stock-and-doesnt-allow-backorders
*/
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>";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment