WooCommerce PHP Beginner Requires WooCommerce

Custom Stock Messages (“Only 3 Left”)

Replace WooCommerce's stock text with friendlier messages like "Only 3 left" and "Back soon".

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 13 lines
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 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • WooCommerce’s “Stock display format” setting under Products → Inventory also affects this text. The snippet replaces it either way.
  • Only claim low stock when it’s true. Fake scarcity can break consumer protection rules.

How to test it

Set a product to manage stock with a quantity of 3 and open its page. It should say “Only 3 left”.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only