# "Only a Few Left" Low Stock Message

> When a product with managed stock drops to a few units, shows a highlighted "Only 3 left, order soon" message instead of the plain stock count.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, stock, urgency, conversion
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-low-stock-urgency/

## Code

```php
add_filter( 'woocommerce_get_availability_text', function ( $text, $product ) {
    $threshold = 5; // Show the message at this many units or fewer.

    if ( ! $product instanceof WC_Product || ! $product->managing_stock() || ! $product->is_in_stock() ) {
        return $text;
    }

    $qty = (int) $product->get_stock_quantity();

    if ( $qty > 0 && $qty <= $threshold ) {
        return sprintf( 'Only %d left, order soon', $qty );
    }

    return $text;
}, 20, 2 );

add_filter( 'woocommerce_get_stock_html', function ( $html, $product ) {
    if ( $product instanceof WC_Product && $product->managing_stock() && $product->is_in_stock() && (int) $product->get_stock_quantity() <= 5 ) {
        $html = str_replace( 'class="stock ', 'style="color:#b32d2e;font-weight:600;" class="stock ', $html );
    }

    return $html;
}, 10, 2 );
```

## Notes

Change $threshold to the quantity at which the message appears. Requires stock management on the product.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find ""Only a Few Left" Low Stock Message" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
