I would like to show the stock status of variable products in the dropdown menu, including 'on backorder' as most products on my site are available on backorder rather than being 'out of stock'.
I have tried the answer from How to add variation stock status to Woocommerce product variation dropdown however, every variable is listed as 'in stock' because the product is set to allow backorders.
I would like to incorporate checking the actual stock levels like below, but I can't get it to work properly with the above link.
$var_stock_count = $variation->get_stock_quantity();
// if there are 0 or less, display 'on backorder'
if( $var_stock_count <= 0 ) {
return ' - (On Backorder)';
}
else {
return ' - (In Stock)';
}
I need help incorporating the two pieces of code together. Thank you!
This updated function that will handle products on backorders (when stock quantity is less than 1):
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You will get something like: