Halloween Sale 15% off Ends in See the plans
PHP

Daily Low Stock Digest Email

Sends one email each morning listing every product at or below your low stock threshold, instead of a separate alert per product.

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

Code

$wpide_digest_to = ''; // Recipient; empty uses the WooCommerce stock email recipient or admin email.

add_action( 'init', function () {
    if ( ! wp_next_scheduled( 'wpide_low_stock_digest' ) ) {
        wp_schedule_event( strtotime( 'tomorrow 07:00' ) - (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ), 'daily', 'wpide_low_stock_digest' );
    }
} );

add_action( 'wpide_low_stock_digest', function () use ( $wpide_digest_to ) {
    global $wpdb;

    if ( ! function_exists( 'WC' ) ) {
        return;
    }

    $threshold = (int) get_option( 'woocommerce_notify_low_stock_amount', 2 );
    $rows      = $wpdb->get_results( $wpdb->prepare(
        "SELECT l.product_id, l.sku, l.stock_quantity FROM {$wpdb->wc_product_meta_lookup} l
         INNER JOIN {$wpdb->posts} p ON p.ID = l.product_id AND p.post_status = 'publish'
         WHERE l.stock_quantity IS NOT NULL AND l.stock_quantity <= %d
         ORDER BY l.stock_quantity ASC LIMIT 200",
        $threshold
    ) );

    if ( ! $rows ) {
        return;
    }

    $lines = '';

    foreach ( $rows as $row ) {
        $lines .= sprintf( '<tr><td><a href="%1$s">%2$s</a></td><td>%3$s</td><td style="text-align:right">%4$s</td></tr>', esc_url( admin_url( 'post.php?action=edit&post=' . ( wp_get_post_parent_id( $row->product_id ) ?: $row->product_id ) ) ), esc_html( get_the_title( $row->product_id ) ), esc_html( $row->sku ), esc_html( wc_stock_amount( $row->stock_quantity ) ) );
    }

    $to      = $wpide_digest_to ? $wpide_digest_to : get_option( 'woocommerce_stock_email_recipient', get_option( 'admin_email' ) );
    $mailer  = WC()->mailer();
    $content = '<table style="width:100%;border-collapse:collapse" cellpadding="6"><tr><th style="text-align:left">Product</th><th style="text-align:left">SKU</th><th style="text-align:right">Stock</th></tr>' . $lines . '</table>';

    $mailer->send( $to, sprintf( '%d products are low on stock', count( $rows ) ), $mailer->wrap_message( 'Low stock digest', $content ) );
} );

Notes

Uses the low stock threshold from WooCommerce > Settings > Products > Inventory and sends nothing on days when no product is low.

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.