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
- Go to the Firebase Console.
- Create a new project or select an existing one.
- Navigate to the Project settings (gear icon).
- Go to the Cloud Messaging tab.
- Copy the Server key. You will need this key to send push notifications.
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:
- Visit Web-Push-Keys.
- Click Generate VAPID Keys.
- 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.
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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

