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');23functionwp_dudecom_enable_stock_notifications(){4add_filter('woocommerce_email_classes','wp_dudecom_add_custom_email_classes');5add_action('woocommerce_low_stock_notification','wp_dudecom_trigger_low_stock_email');6add_action('woocommerce_no_stock_notification','wp_dudecom_trigger_no_stock_email');7}89functionwp_dudecom_add_custom_email_classes($email_classes){10require_once'class-wp-dudecom-low-stock-email.php';11require_once'class-wp-dudecom-no-stock-email.php';1213$email_classes['WP_Dudecom_Low_Stock_Email']=newWP_Dudecom_Low_Stock_Email();14$email_classes['WP_Dudecom_No_Stock_Email']=newWP_Dudecom_No_Stock_Email();1516return$email_classes;17}1819functionwp_dudecom_trigger_low_stock_email($product){20$mailer=WC()->mailer();21$mails=$mailer->get_emails();22if(!empty($mails['WP_Dudecom_Low_Stock_Email'])){23$mails['WP_Dudecom_Low_Stock_Email']->trigger($product);24}25}2627functionwp_dudecom_trigger_no_stock_email($product){28$mailer=WC()->mailer();29$mails=$mailer->get_emails();30if(!empty($mails['WP_Dudecom_No_Stock_Email'])){31$mails['WP_Dudecom_No_Stock_Email']->trigger($product);32}33}3435classWP_Dudecom_Low_Stock_EmailextendsWC_Email{3637publicfunction__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';4546add_action('wp_dudecom_low_stock_notification',array($this,'trigger'),10,1);4748parent::__construct();49}5051publicfunctiontrigger($product){52if(!$this->is_enabled()){53return;54}5556$this->object=$product;57$this->recipient=get_option('admin_email');5859$this->placeholders=array(60'{product_name}'=>$this->object->get_name(),61);6263if(!$this->get_recipient()){64return;65}6667$this->send($this->get_recipient(),$this->get_subject(),$this->get_content(),$this->get_headers(),$this->get_attachments());68}6970publicfunctionget_content_html(){71returnwc_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}7980publicfunctionget_content_plain(){81returnwc_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}9091classWP_Dudecom_No_Stock_EmailextendsWC_Email{9293publicfunction__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';101102add_action('wp_dudecom_no_stock_notification',array($this,'trigger'),10,1);103104parent::__construct();105}106107publicfunctiontrigger($product){108if(!$this->is_enabled()){109return;110}111112$this->object=$product;113$this->recipient=get_option('admin_email');114115$this->placeholders=array(116'{product_name}'=>$this->object->get_name(),117);118119if(!$this->get_recipient()){120return;121}122123$this->send($this->get_recipient(),$this->get_subject(),$this->get_content(),$this->get_headers(),$this->get_attachments());124}125126publicfunctionget_content_html(){127returnwc_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}135136publicfunctionget_content_plain(){137returnwc_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:
Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
Locate the File: Navigate to wp-content/themes/your-theme/functions.php or create a new plugin file in wp-content/plugins/.
Edit the File: Open the functions.php file or your plugin file in a text editor.
Add the Code: Copy and paste the provided code into the file.
Save Changes: Save the file and ensure there are no syntax errors.
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.
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.