Snippet

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

How to enable low stock email notifications in woocommerceWoocommerce out of stock email alerts setupSet up low stock notifications wordpressEnable out of stock notifications woocommerceWoocommerce stock notification email settingsHow to get notified when product is out of stock woocommerceConfigure low stock alerts in woocommerceWoocommerce email notifications for inventory managementSet up stock alerts in woocommerceWoocommerce 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

1add_action( 'init', 'wp_dudecom_enable_stock_notifications' ); 2 3function wp_dudecom_enable_stock_notifications() { 4 add_filter( 'woocommerce_email_classes', 'wp_dudecom_add_custom_email_classes' ); 5 add_action( 'woocommerce_low_stock_notification', 'wp_dudecom_trigger_low_stock_email' ); 6 add_action( 'woocommerce_no_stock_notification', 'wp_dudecom_trigger_no_stock_email' ); 7} 8 9function wp_dudecom_add_custom_email_classes( $email_classes ) { 10 require_once 'class-wp-dudecom-low-stock-email.php'; 11 require_once 'class-wp-dudecom-no-stock-email.php'; 12 13 $email_classes['WP_Dudecom_Low_Stock_Email'] = new WP_Dudecom_Low_Stock_Email(); 14 $email_classes['WP_Dudecom_No_Stock_Email'] = new WP_Dudecom_No_Stock_Email(); 15 16 return $email_classes; 17} 18 19function wp_dudecom_trigger_low_stock_email( $product ) { 20 $mailer = WC()->mailer(); 21 $mails = $mailer->get_emails(); 22 if ( ! empty( $mails['WP_Dudecom_Low_Stock_Email'] ) ) { 23 $mails['WP_Dudecom_Low_Stock_Email']->trigger( $product ); 24 } 25} 26 27function wp_dudecom_trigger_no_stock_email( $product ) { 28 $mailer = WC()->mailer(); 29 $mails = $mailer->get_emails(); 30 if ( ! empty( $mails['WP_Dudecom_No_Stock_Email'] ) ) { 31 $mails['WP_Dudecom_No_Stock_Email']->trigger( $product ); 32 } 33} 34 35class WP_Dudecom_Low_Stock_Email extends WC_Email { 36 37 public function __construct() { 38 $this->id = 'wp_dudecom_low_stock_email'; 39 $this->title = 'Low Stock Notification'; 40 $this->description = 'This email is sent when a product is low in stock.'; 41 $this->heading = 'Product Low in Stock'; 42 $this->subject = 'Low Stock Alert: {product_name}'; 43 $this->template_html = 'emails/wp-dudecom-low-stock-email.php'; 44 $this->template_plain = 'emails/plain/wp-dudecom-low-stock-email.php'; 45 46 add_action( 'wp_dudecom_low_stock_notification', array( $this, 'trigger' ), 10, 1 ); 47 48 parent::__construct(); 49 } 50 51 public function trigger( $product ) { 52 if ( ! $this->is_enabled() ) { 53 return; 54 } 55 56 $this->object = $product; 57 $this->recipient = get_option( 'admin_email' ); 58 59 $this->placeholders = array( 60 '{product_name}' => $this->object->get_name(), 61 ); 62 63 if ( ! $this->get_recipient() ) { 64 return; 65 } 66 67 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); 68 } 69 70 public function get_content_html() { 71 return wc_get_template_html( $this->template_html, array( 72 'email_heading' => $this->get_heading(), 73 'product' => $this->object, 74 'sent_to_admin' => true, 75 'plain_text' => false, 76 'email' => $this, 77 ) ); 78 } 79 80 public function get_content_plain() { 81 return wc_get_template_html( $this->template_plain, array( 82 'email_heading' => $this->get_heading(), 83 'product' => $this->object, 84 'sent_to_admin' => true, 85 'plain_text' => true, 86 'email' => $this, 87 ) ); 88 } 89} 90 91class WP_Dudecom_No_Stock_Email extends WC_Email { 92 93 public function __construct() { 94 $this->id = 'wp_dudecom_no_stock_email'; 95 $this->title = 'Out of Stock Notification'; 96 $this->description = 'This email is sent when a product is out of stock.'; 97 $this->heading = 'Product Out of Stock'; 98 $this->subject = 'Out of Stock Alert: {product_name}'; 99 $this->template_html = 'emails/wp-dudecom-no-stock-email.php'; 100 $this->template_plain = 'emails/plain/wp-dudecom-no-stock-email.php'; 101 102 add_action( 'wp_dudecom_no_stock_notification', array( $this, 'trigger' ), 10, 1 ); 103 104 parent::__construct(); 105 } 106 107 public function trigger( $product ) { 108 if ( ! $this->is_enabled() ) { 109 return; 110 } 111 112 $this->object = $product; 113 $this->recipient = get_option( 'admin_email' ); 114 115 $this->placeholders = array( 116 '{product_name}' => $this->object->get_name(), 117 ); 118 119 if ( ! $this->get_recipient() ) { 120 return; 121 } 122 123 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); 124 } 125 126 public function get_content_html() { 127 return wc_get_template_html( $this->template_html, array( 128 'email_heading' => $this->get_heading(), 129 'product' => $this->object, 130 'sent_to_admin' => true, 131 'plain_text' => false, 132 'email' => $this, 133 ) ); 134 } 135 136 public function get_content_plain() { 137 return wc_get_template_html( $this->template_plain, array( 138 'email_heading' => $this->get_heading(), 139 'product' => $this->object, 140 'sent_to_admin' => true, 141 'plain_text' => true, 142 'email' => $this, 143 ) ); 144 } 145}

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.