How to Get Sale Push Notifications in WooCommerce Without a Plugin – 2026

Last updated on July 8th, 2024 at 08:10 am

In this tutorial, you’ll learn how to set up push notifications for WooCommerce sales without using a plugin. We will use web push notifications to notify administrators and shop managers about new sales. This ensures that only authorized users receive these notifications. By the end of this guide, you’ll have a system in place that sends real-time notifications whenever a sale occurs on your WooCommerce store.

This implementation is crucial for store managers who need to stay updated about sales activities without constantly checking their admin dashboard. Let’s dive in!

Step 1: Setting Up Web Push Notifications

First, we need to set up the basics for web push notifications, which involves creating a service worker and implementing the push notification logic.

Create a Service Worker

Create a file named sw.js in your WordPress theme directory.

/*
 * Snippet: How to Get Sale Push Notifications in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1255
* Tested with WooCommerce 10.7.0
* "Service worker file for handling push notifications"
*/ self.addEventListener('push', function(event) { const data = event.data.json(); self.registration.showNotification(data.title, { body: data.body, icon: data.icon }); });
Register the Service Worker

Add the following code to your theme’s functions.php file to register the service worker.

/*
 * Snippet: How to Get Sale Push Notifications in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1255
* Tested with WooCommerce 10.7.0
* "Register service worker"
*/ function wcsuccess_register_service_worker() { if (current_user_can('shop_manager') || current_user_can('administrator')) { echo '<script> if ("serviceWorker" in navigator) { navigator.serviceWorker.register("' . get_template_directory_uri() . '/sw.js") .then(function(registration) { console.log("Service Worker registered with scope:", registration.scope); }, function(error) { console.log("Service Worker registration failed:", error); }); } </script>'; } } add_action('wp_footer', 'wcsuccess_register_service_worker');

Step 2: Getting a Server Key and Public VAPID Key

To send push notifications, you need to obtain a server key and a VAPID key pair. We will use Firebase Cloud Messaging (FCM) for this purpose.

Obtaining a Server Key
  1. Go to the Firebase Console.
  2. Create a new project or select an existing one.
  3. Navigate to the Project settings (gear icon).
  4. Go to the Cloud Messaging tab.
  5. Copy the Server key. You will need this key to send push notifications.
See also  How to add a custom WooCommerce order status 2026
Generating VAPID Keys

You can generate VAPID keys using various online tools or command-line tools. Here’s how you can generate VAPID keys using an online tool:

  1. Visit Web-Push-Keys.
  2. Click Generate VAPID Keys.
  3. Copy the generated Public VAPID Key and Private VAPID Key.
Subscribe to Push Notifications

Add the following JavaScript to your theme to request permission for notifications and subscribe the user to push notifications.

/*
 * Snippet: How to Get Sale Push Notifications in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1255
* Tested with WooCommerce 10.7.0
* "Subscribe to push notifications"
*/ document.addEventListener('DOMContentLoaded', function() { if (current_user_can('shop_manager') || current_user_can('administrator')) { navigator.serviceWorker.ready.then(function(registration) { return registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: '<YOUR_PUBLIC_VAPID_KEY>' }); }).then(function(subscription) { fetch('url-to-your-server-endpoint', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(subscription) }); }); } });

Replace '<YOUR_PUBLIC_VAPID_KEY>' with your actual VAPID key.

Step 3: Setting Up Push Notification Logic

Next, we need to handle the push notifications on the server side. We’ll add code to our theme’s functions.php file to send push notifications when a sale is made.

Send Push Notifications on Sale

Add the following code to your functions.php file to send notifications when a new order is created.

/*
 * Snippet: How to Get Sale Push Notifications in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1255
* Tested with WooCommerce 10.7.0
* "Send push notifications on sale"
*/ function wcsuccess_send_push_notification($order_id) { $order = wc_get_order($order_id); $users = get_users(array('role__in' => array('administrator', 'shop_manager'))); foreach ($users as $user) { $subscription = get_user_meta($user->ID, 'push_subscription', true); if ($subscription) { $notification = array( 'title' => 'New Order Received!', 'body' => 'Order #' . $order_id . ' has been placed.', 'icon' => 'path-to-your-icon' ); $response = wp_remote_post($subscription['endpoint'], array( 'headers' => array( 'Authorization' => 'key=' . '<YOUR_SERVER_KEY>', 'Content-Type' => 'application/json' ), 'body' => json_encode(array( 'subscription' => $subscription, 'payload' => $notification )) )); } } } add_action('woocommerce_thankyou', 'wcsuccess_send_push_notification');

Replace '<YOUR_SERVER_KEY>' with your actual server key and 'path-to-your-icon' with the path to your notification icon.

See also  How to Customise the WooCommerce Order Confirmation Email with Product-Specific Messages - 2026

Step 4: Storing Push Subscriptions

We need to store the push subscription details for each user.

/*
 * Snippet: How to Get Sale Push Notifications in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1255
* Tested with WooCommerce 10.7.0
* "Store push subscriptions"
*/ add_action('wp_ajax_wcsuccess_store_push_subscription', 'wcsuccess_store_push_subscription'); add_action('wp_ajax_nopriv_wcsuccess_store_push_subscription', 'wcsuccess_store_push_subscription'); function wcsuccess_store_push_subscription() { if (is_user_logged_in() && (current_user_can('shop_manager') || current_user_can('administrator'))) { $user_id = get_current_user_id(); $subscription = json_decode(file_get_contents('php://input'), true); update_user_meta($user_id, 'push_subscription', $subscription); wp_send_json_success(); } else { wp_send_json_error('Unauthorized'); } }

Conclusion

By following these steps, you can implement a robust system for sending push notifications for WooCommerce sales without using a plugin. This system ensures that only shop managers and administrators receive the notifications, keeping them informed about new sales in real-time.

This setup is beneficial as it provides immediate updates, allowing for quick responses to new orders and enhancing overall store management efficiency. With this implementation, you can stay on top of your sales activities effortlessly.

Feel free to customize and expand this functionality to fit your specific needs, ensuring a seamless and efficient notification system for your WooCommerce store.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×