I'm looking to edit the woocommerce low stock message. But only for product variations which are set to allow backorders.
The message should include the stock level, and only appear when the stock level is below 5 but more than 0.
Currently using the below. But it is displaying on all product variations, we need to only display on variations set to allow backorders.
add_filter( 'woocommerce_get_availability', 'change_stock_text', 20, 2 );
function change_stock_text( $availability, $_product ) {
if ( $_product->is_in_stock() ) {
if ( $_product->get_stock_quantity() < 5 ) {
if ( $_product->get_stock_quantity() > 0 ) {
$qty = $_product->get_stock_quantity();
$availability['availability'] = __( "{$qty} in stock, more available on backorder", 'woocommerce' );
}
}
}
return $availability;
}
Thank you.
To add a customized availability string targeting product variations with allowed backorders when stock is low (between 1 and 4), use the following:
add_filter( 'woocommerce_get_availability', 'product_variation_backorders_allowed_low_stock_availability_string', 20, 2 );
function product_variation_backorders_allowed_low_stock_availability_string( $availability, $product ) {
$stock_qty = $product->get_stock_quantity();
// Targeting backorders allowed product variation with low stock
if ( $product->is_in_stock() && $product->is_type('variation') && $product->backorders_allowed() && $stock_qty > 0 && $stock_qty < 5 ) {
$availability['availability'] = sprintf( __('%d in stock, more available on backorder', 'woocommerce'), $stock_qty );
}
return $availability;
}
Code goes in functions.php file of your child theme (or in a plugin).