# Out-of-Stock Count Notice in the Admin

> Shows store managers a notice on the dashboard and WooCommerce screens with how many products are out of stock, linking straight to the filtered product list.

- Type: PHP
- Runs: Admin only
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, inventory, admin, notices
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-out-of-stock-admin-notice/

## Code

```php
add_action( 'admin_notices', function () {
    global $wpdb;

    $screen = get_current_screen();

    if ( ! current_user_can( 'manage_woocommerce' ) || ! $screen || ( 'dashboard' !== $screen->id && false === strpos( (string) $screen->id, 'woocommerce' ) && 'edit-product' !== $screen->id ) ) {
        return;
    }

    $count = get_transient( 'wpide_oos_count' );

    if ( false === $count ) {
        $count = (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->wc_product_meta_lookup} l INNER JOIN {$wpdb->posts} p ON p.ID = l.product_id
             WHERE l.stock_status = 'outofstock' AND p.post_type = 'product' AND p.post_status = 'publish'"
        );
        set_transient( 'wpide_oos_count', $count, HOUR_IN_SECONDS );
    }

    if ( $count > 0 ) {
        printf(
            '<div class="notice notice-warning"><p>%1$s <a href="%2$s">%3$s</a></p></div>',
            esc_html( sprintf( _n( '%d product is out of stock.', '%d products are out of stock.', $count ), $count ) ),
            esc_url( admin_url( 'edit.php?post_type=product&stock_status=outofstock' ) ),
            esc_html( 'View them' )
        );
    }
} );

$wpide_oos_flush = function () {
    delete_transient( 'wpide_oos_count' );
};

add_action( 'woocommerce_product_set_stock_status', $wpide_oos_flush );
add_action( 'woocommerce_variation_set_stock_status', $wpide_oos_flush );
```

## Notes

The count is cached for an hour and refreshed as soon as any product's stock status changes.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Out-of-Stock Count Notice in the Admin" 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/
