Replace WooCommerce's stock text with friendlier messages like "Only 3 left" and "Back soon".
add_filter( 'woocommerce_get_availability_text', function ( string $text, WC_Product $product ): string {
if ( ! $product->is_in_stock() ) {
return 'Back soon';
}
if ( $product->managing_stock() ) {
$qty = (int) $product->get_stock_quantity();
if ( $qty > 0 && $qty <= 5 ) {
return sprintf( 'Only %d left', $qty );
}
return 'In stock';
}
return $text;
}, 10, 2 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WooCommerce’s stock text, such as “12 in stock”, is functional but flat. Low stock is a reason to buy now, so saying “Only 3 left” helps. This snippet rewrites the message.
The woocommerce_get_availability_text filter returns the stock text shown on product pages. Out-of-stock products say “Back soon”. Products that track stock show “Only N left” at five or fewer, and “In stock” above that, so you don’t reveal exact numbers. Products that don’t track stock keep WooCommerce’s text.
Backorder messages are left unchanged.
Set a product to manage stock with a quantity of 3 and open its page. It should say “Only 3 left”.