Low Stock Products Dashboard Widget
Lists WooCommerce products that are running low or out of stock, so you can reorder before customers notice.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! function_exists( 'wc_get_products' ) || ! current_user_can( 'edit_products' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_woo_low_stock', 'Low Stock', function () {
$threshold = (int) get_option( 'woocommerce_notify_low_stock_amount', 2 );
$products = wc_get_products( array(
'limit' => 15,
'manage_stock' => true,
'stock_status' => array( 'instock', 'outofstock', 'onbackorder' ),
'orderby' => 'meta_value_num',
'meta_key' => '_stock',
'order' => 'ASC',
'type' => array( 'simple', 'variation' ),
) );
$rows = array();
foreach ( $products as $product ) {
if ( (int) $product->get_stock_quantity() <= $threshold ) {
$rows[] = $product;
}
}
if ( ! $rows ) {
echo '<p>All tracked products are above the low stock threshold.</p>';
return;
}
echo '<table class="widefat striped"><tbody>';
foreach ( $rows as $product ) {
$edit_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
printf(
'<tr><td><a href="%1$s">%2$s</a></td><td style="text-align:right;%3$s">%4$d</td></tr>',
esc_url( get_edit_post_link( $edit_id ) ),
esc_html( $product->get_name() ),
$product->get_stock_quantity() <= 0 ? 'color:#d63638;font-weight:600' : '',
(int) $product->get_stock_quantity()
);
}
echo '</tbody></table>';
} );
} );
Notes
Uses the low stock threshold from WooCommerce > Settings > Products > Inventory.
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.