Set Automatic Notifications for Out-of-Stock Products in WooCommerce
Explanation
Here's how you can set up automatic notifications for out-of-stock products in WooCommerce:
- Add a Custom Field: A checkbox is added to each product's settings. This lets you enable notifications for when a product is back in stock. You can find this option under the product's inventory settings.
- Save the Setting: When you update the product, the choice you made about enabling notifications is saved. This means WooCommerce remembers whether you want to notify customers when the product is back in stock.
- Notify Customers: When a product is restocked, an email is automatically sent to everyone who subscribed for notifications. This email tells them the product is available again, encouraging them to make a purchase.
- Subscription Form: On the product page, if the item is out of stock and notifications are enabled, a form appears. Customers can enter their email to get notified when the product is back in stock.
- Handle Subscriptions: When a customer submits their email, it's saved if it's not already on the list. They get a confirmation message saying they'll be notified when the product is available again.
This setup ensures your customers are kept in the loop about product availability, potentially increasing sales by bringing them back to your store when items are restocked.
Code
// Add a custom field to WooCommerce product data for enabling back in stock notifications
function wp_dudecom_add_back_in_stock_field() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox( array(
'id' => '_enable_back_in_stock_notifications',
'label' => __('Enable Back In Stock Notifications', 'woocommerce'),
'description' => __('Enable this to notify customers when this product is back in stock.', 'woocommerce'),
));
echo '</div>';
}
add_action('woocommerce_product_options_inventory_product_data', 'wp_dudecom_add_back_in_stock_field');
// Save the custom field value
function wp_dudecom_save_back_in_stock_field($post_id) {
$enable_back_in_stock = isset($_POST['_enable_back_in_stock_notifications']) ? 'yes' : 'no';
update_post_meta($post_id, '_enable_back_in_stock_notifications', $enable_back_in_stock);
}
add_action('woocommerce_process_product_meta', 'wp_dudecom_save_back_in_stock_field');
// Notify customers when a product is back in stock
function wp_dudecom_notify_back_in_stock($product_id) {
$product = wc_get_product($product_id);
if ($product->is_in_stock() && 'yes' === get_post_meta($product_id, '_enable_back_in_stock_notifications', true)) {
$subscribers = get_post_meta($product_id, '_back_in_stock_subscribers', true);
if (!empty($subscribers)) {
foreach ($subscribers as $subscriber_email) {
wp_mail(
$subscriber_email,
__('Product Back In Stock', 'woocommerce'),
sprintf(__('The product %s is back in stock. You can purchase it now.', 'woocommerce'), $product->get_name())
);
}
delete_post_meta($product_id, '_back_in_stock_subscribers');
}
}
}
add_action('woocommerce_product_set_stock_status', 'wp_dudecom_notify_back_in_stock', 10, 1);
// Add a form for customers to subscribe to back in stock notifications
function wp_dudecom_back_in_stock_form() {
global $product;
if (!$product->is_in_stock() && 'yes' === get_post_meta($product->get_id(), '_enable_back_in_stock_notifications', true)) {
echo '<form method="post" id="back-in-stock-form">';
echo '<input type="email" name="back_in_stock_email" placeholder="Enter your email" required />';
echo '<button type="submit">' . __('Notify Me', 'woocommerce') . '</button>';
echo '</form>';
}
}
add_action('woocommerce_single_product_summary', 'wp_dudecom_back_in_stock_form', 35);
// Handle form submission and save subscriber email
function wp_dudecom_handle_back_in_stock_form() {
if (isset($_POST['back_in_stock_email']) && is_email($_POST['back_in_stock_email'])) {
global $product;
$email = sanitize_email($_POST['back_in_stock_email']);
$subscribers = get_post_meta($product->get_id(), '_back_in_stock_subscribers', true);
if (empty($subscribers)) {
$subscribers = array();
}
if (!in_array($email, $subscribers)) {
$subscribers[] = $email;
update_post_meta($product->get_id(), '_back_in_stock_subscribers', $subscribers);
wc_add_notice(__('You will be notified when the product is back in stock.', 'woocommerce'), 'success');
} else {
wc_add_notice(__('You are already subscribed for back in stock notifications.', 'woocommerce'), 'notice');
}
}
}
add_action('wp', 'wp_dudecom_handle_back_in_stock_form');
Instructions
To implement automatic notifications for out-of-stock products in WooCommerce, follow these steps:
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.
- Access Your WordPress Dashboard: Log in to your WordPress admin panel.
- Navigate to Appearance > Theme Editor: If you are adding the code to
functions.php
, go to the Theme Editor. Alternatively, if using a custom plugin, navigate to Plugins > Add New and create a new plugin file. - Insert the Code: Copy and paste the provided code into the
functions.php
file or your custom plugin file. - Save Changes: After pasting the code, save the changes to apply the new functionality.
- Enable Notifications for Products:
- Go to Products > All Products in your WordPress dashboard.
- Edit a product and navigate to the Inventory tab.
- Check the "Enable Back In Stock Notifications" option to allow notifications for that product.
- Update the product to save your changes.
- Test the Functionality:
- Visit a product page that is out of stock.
- Ensure the subscription form appears, allowing customers to enter their email for notifications.
- Simulate a restock by updating the product's stock status to "In Stock" and verify that notification emails are sent to subscribers.
This setup will help keep your customers informed about product availability, potentially increasing sales by bringing them back to your store when items are restocked.
Need help with implementation or more advanced functionality? Visit wp-dude.com for expert assistance.