Manual SMS Notifications for Out-of-Stock WooCommerce Products: A Step-by-Step Guide

Introduction

In the dynamic world of e-commerce, staying connected with customers is crucial, especially regarding product availability. WooCommerce, a robust and customizable e-commerce platform, offers various hooks to enhance your online store’s functionality. One such enhancement is notifying customers via SMS when an out-of-stock product is back in stock.

This article will guide you through adding a form to WooCommerce products that allows customers to sign up for SMS notifications. While this process is manual for now, WP SMS users can look forward to an automatic feature in the near future.

With higher open rates than emails, SMS notifications are a reliable method of instantly informing your customers. They directly communicate to your customer’s mobile device, ensuring your message is seen promptly.

Prerequisites

To implement this feature, you’ll need:

  • A WordPress website with WooCommerce installed.
  • WP SMS plugin for managing SMS communications.
  • A basic understanding of hooks in WordPress and PHP.

Step 1: Understanding WooCommerce Hooks

Hooks in WordPress allow developers to ‘hook into’ the platform’s code to alter or add functionality. WooCommerce comes with its own hooks, which we’ll use for our purposes. woocommerce_get_stock_html.

Step 2: Adding a Custom Form to Out-of-Stock Products

When a product is out of stock, we’ll append a form to the stock HTML using the woocommerce_get_stock_html filter. This form will allow users to subscribe to an SMS notification. Here’s the PHP code that does just that:

// Add filter to 'woocommerce_get_stock_html'
add_filter('woocommerce_get_stock_html', function ($html, $product) {
    if (!$product->is_in_stock()) {
        // Your custom form HTML goes here
        $html .= '
        <form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post">
            <p class="stock out-of-stock">Out of stock</p>
            <label for="mobile_number">Subscribe to SMS alerts:</label>
            <input type="tel" id="mobile_number" name="mobile_number" required>
            <button type="submit">Notify Me</button>
        </form>';
    }
    return $html;
}, 10, 2);

Step 3: Capturing Form Submissions

Next, we need to handle the form submission and use the provided addSubscriber function from WP SMS Pro to add the user’s mobile number to the SMS group.

// Handle form submission
add_action('init', function () {
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['mobile_number'])) {
        // Assume the name is available from user input or user profile
        $name = 'John Doe';
        $group_id = 1; // The ID of your SMS group in WP SMS Pro

        // Add subscriber to SMS newsletter
        if (function_exists('\WP_SMS\Newsletter::addSubscriber')) {
            \WP_SMS\Newsletter::addSubscriber($name, sanitize_text_field($_POST['mobile_number']), $group_id);
            // Provide user feedback (customize as needed)
            add_action('wp_footer', function() {
                echo '<script>alert("Thank you for subscribing to SMS notifications!");</script>';
            });
        }
    }
});

Manual vs. Automatic Notifications

Currently, the process of sending SMS notifications when products are restocked is manual. As a store owner, you must monitor stock levels and send SMS alerts to the subscribed users. However, we are excited to announce that automatic SMS notifications are on the horizon in the WP SMS WooCommerce Pro Add-On. This upcoming feature will streamline the notification process, ensuring customers are informed automatically as soon as products become available again.

Conclusion

Implementing SMS notifications for out-of-stock items is a proactive way to enhance customer service and engagement. Using the power of WooCommerce hooks and the WP SMS plugin, you can keep your customers informed and eager to return to your store.