Halloween Sale 15% off Ends in See the plans
PHP

Low Stock Products Dashboard Widget

Lists WooCommerce products that are running low or out of stock, so you can reorder before customers notice.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.