Enable Email Notifications for Low and Out-of-Stock Inventory

How to enable low stock email notifications in woocommerce; Woocommerce out of stock email alerts setup; Set up low stock notifications wordpress; Enable out of stock notifications woocommerce; Woocommerce stock notification email settings; How to get notified when product is out of stock woocommerce; Configure low stock alerts in woocommerce; Woocommerce email notifications for inventory management; Set up stock alerts in woocommerce; Woocommerce low stock email configuration;

Explanation

To keep track of your inventory, this code helps you get email alerts when your products are running low or are out of stock in WooCommerce.

Here's how it works:

  • Custom Email Classes: The code adds two custom email classes for low stock and no stock notifications. These classes define the email's content and format.
  • Triggering Emails: When a product's stock level changes, the code triggers the appropriate email notification. This is done using WooCommerce's built-in actions for low and no stock events.
  • Email Content: The emails include details like the product name and are sent to the admin's email address. The content can be customized using template files.

By using this setup, you'll receive timely notifications, helping you manage your stock levels effectively and avoid disappointing customers with unavailable products.

Code

add_action( 'init', 'wp_dudecom_enable_stock_notifications' );

function wp_dudecom_enable_stock_notifications() {
    add_filter( 'woocommerce_email_classes', 'wp_dudecom_add_custom_email_classes' );
    add_action( 'woocommerce_low_stock_notification', 'wp_dudecom_trigger_low_stock_email' );
    add_action( 'woocommerce_no_stock_notification', 'wp_dudecom_trigger_no_stock_email' );
}

function wp_dudecom_add_custom_email_classes( $email_classes ) {
    require_once 'class-wp-dudecom-low-stock-email.php';
    require_once 'class-wp-dudecom-no-stock-email.php';

    $email_classes['WP_Dudecom_Low_Stock_Email'] = new WP_Dudecom_Low_Stock_Email();
    $email_classes['WP_Dudecom_No_Stock_Email'] = new WP_Dudecom_No_Stock_Email();

    return $email_classes;
}

function wp_dudecom_trigger_low_stock_email( $product ) {
    $mailer = WC()->mailer();
    $mails = $mailer->get_emails();
    if ( ! empty( $mails['WP_Dudecom_Low_Stock_Email'] ) ) {
        $mails['WP_Dudecom_Low_Stock_Email']->trigger( $product );
    }
}

function wp_dudecom_trigger_no_stock_email( $product ) {
    $mailer = WC()->mailer();
    $mails = $mailer->get_emails();
    if ( ! empty( $mails['WP_Dudecom_No_Stock_Email'] ) ) {
        $mails['WP_Dudecom_No_Stock_Email']->trigger( $product );
    }
}

class WP_Dudecom_Low_Stock_Email extends WC_Email {

    public function __construct() {
        $this->id = 'wp_dudecom_low_stock_email';
        $this->title = 'Low Stock Notification';
        $this->description = 'This email is sent when a product is low in stock.';
        $this->heading = 'Product Low in Stock';
        $this->subject = 'Low Stock Alert: {product_name}';
        $this->template_html = 'emails/wp-dudecom-low-stock-email.php';
        $this->template_plain = 'emails/plain/wp-dudecom-low-stock-email.php';

        add_action( 'wp_dudecom_low_stock_notification', array( $this, 'trigger' ), 10, 1 );

        parent::__construct();
    }

    public function trigger( $product ) {
        if ( ! $this->is_enabled() ) {
            return;
        }

        $this->object = $product;
        $this->recipient = get_option( 'admin_email' );

        $this->placeholders = array(
            '{product_name}' => $this->object->get_name(),
        );

        if ( ! $this->get_recipient() ) {
            return;
        }

        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }

    public function get_content_html() {
        return wc_get_template_html( $this->template_html, array(
            'email_heading' => $this->get_heading(),
            'product'       => $this->object,
            'sent_to_admin' => true,
            'plain_text'    => false,
            'email'         => $this,
        ) );
    }

    public function get_content_plain() {
        return wc_get_template_html( $this->template_plain, array(
            'email_heading' => $this->get_heading(),
            'product'       => $this->object,
            'sent_to_admin' => true,
            'plain_text'    => true,
            'email'         => $this,
        ) );
    }
}

class WP_Dudecom_No_Stock_Email extends WC_Email {

    public function __construct() {
        $this->id = 'wp_dudecom_no_stock_email';
        $this->title = 'Out of Stock Notification';
        $this->description = 'This email is sent when a product is out of stock.';
        $this->heading = 'Product Out of Stock';
        $this->subject = 'Out of Stock Alert: {product_name}';
        $this->template_html = 'emails/wp-dudecom-no-stock-email.php';
        $this->template_plain = 'emails/plain/wp-dudecom-no-stock-email.php';

        add_action( 'wp_dudecom_no_stock_notification', array( $this, 'trigger' ), 10, 1 );

        parent::__construct();
    }

    public function trigger( $product ) {
        if ( ! $this->is_enabled() ) {
            return;
        }

        $this->object = $product;
        $this->recipient = get_option( 'admin_email' );

        $this->placeholders = array(
            '{product_name}' => $this->object->get_name(),
        );

        if ( ! $this->get_recipient() ) {
            return;
        }

        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }

    public function get_content_html() {
        return wc_get_template_html( $this->template_html, array(
            'email_heading' => $this->get_heading(),
            'product'       => $this->object,
            'sent_to_admin' => true,
            'plain_text'    => false,
            'email'         => $this,
        ) );
    }

    public function get_content_plain() {
        return wc_get_template_html( $this->template_plain, array(
            'email_heading' => $this->get_heading(),
            'product'       => $this->object,
            'sent_to_admin' => true,
            'plain_text'    => true,
            'email'         => $this,
        ) );
    }
}

Instructions

File Location: Add the code to your theme's functions.php file or create a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
  2. Locate the File: Navigate to wp-content/themes/your-theme/functions.php or create a new plugin file in wp-content/plugins/.
  3. Edit the File: Open the functions.php file or your plugin file in a text editor.
  4. Add the Code: Copy and paste the provided code into the file.
  5. Save Changes: Save the file and ensure there are no syntax errors.
  6. Upload Email Templates: Create the necessary email template files:
    • In your theme directory, create a folder named emails and another named plain inside it.
    • Create the files wp-dudecom-low-stock-email.php and wp-dudecom-no-stock-email.php in the emails folder.
    • Create the plain text versions wp-dudecom-low-stock-email.php and wp-dudecom-no-stock-email.php in the plain folder.
  7. Test the Functionality: Adjust product stock levels in WooCommerce to trigger low and no stock notifications and verify that emails are sent to the admin email address.

For assistance with implementation or to explore more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.