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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.