Synchronising inventory in real time is crucial for WooCommerce stores that need accurate stock levels across multiple platforms. Webhooks allow WooCommerce to communicate with external systems, such as inventory management tools or warehouses, whenever stock changes occur.
In this guide, we’ll show you how to integrate webhooks for real-time inventory updates in WooCommerce.
Step 1: Understand Webhooks in WooCommerce
Webhooks are automated messages sent from WooCommerce to a specified URL whenever a defined event occurs. For inventory updates, WooCommerce can send webhooks when stock levels change, ensuring external systems stay updated in real time.
Step 2: Create a Webhook in WooCommerce
To set up a webhook in WooCommerce:
- Log in to WordPress Admin:
- Go to WooCommerce > Settings > Advanced > Webhooks.
- Add a New Webhook:
- Click Add Webhook and fill in the details:
- Name: Enter a descriptive name (e.g., “Inventory Updates”).
- Status: Set to Active.
- Topic: Select Product Updated or Order Updated, depending on your use case.
- Delivery URL: Enter the URL of your external system’s webhook listener.
- Secret: Generate a secret key for secure communication.
- Click Add Webhook and fill in the details:
- Save the Webhook:
- Click Save Webhook. WooCommerce will now send POST requests to the specified URL when the selected event occurs.
Step 3: Configure the Webhook Listener
The external system needs to have a webhook listener to process the incoming POST requests. Here’s an example of how to create a webhook listener using PHP.
Webhook Listener Example in PHP
Save the following code as webhook-listener.php on your server:
/*
* Snippet: How to Integrate Webhooks for Real-Time Inventory Updates in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1827
* Tested with WooCommerce 10.7.0
* "This script listens for WooCommerce webhook events and updates inventory"
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = file_get_contents('php://input');
$data = json_decode($input, true);
// Validate webhook request (using the WooCommerce secret)
$secret = 'your-webhook-secret'; // Replace with your WooCommerce webhook secret
$signature = $_SERVER['HTTP_X_WC_WEBHOOK_SIGNATURE'];
$hash = base64_encode(hash_hmac('sha256', $input, $secret, true));
if ($signature === $hash) {
// Process inventory update
$product_id = $data['id']; // WooCommerce product ID
$stock_quantity = $data['stock_quantity']; // Updated stock quantity
// Update inventory in your external system
// Replace this with your inventory update logic
file_put_contents('inventory-log.txt', "Product ID: $product_id, Stock: $stock_quantity\n", FILE_APPEND);
http_response_code(200); // Respond with 200 OK
} else {
http_response_code(403); // Forbidden
}
} else {
http_response_code(405); // Method Not Allowed
}
Explanation:
- Validation: Uses the secret key to validate the webhook’s authenticity.
- Inventory Update: Extracts product details and logs them (replace this with your actual inventory update logic).
- Response Codes: Ensures WooCommerce receives a success (
200 OK) or failure (403 Forbidden) response.
Step 4: Test the Webhook
To ensure the webhook is functioning correctly:
- Trigger the Webhook:
- Update the stock quantity of a product in WooCommerce.
- Check the Listener:
- Verify that the webhook listener logs or processes the update correctly.
- Debug Errors:
- Check the WooCommerce webhook logs under WooCommerce > Status > Logs for delivery success or failure.
Step 5: Automate Inventory Sync for All Products
If you want to ensure all inventory updates are synchronised automatically, you can extend the webhook functionality with additional customisations.
Code to Add a Custom Webhook for Stock Updates
If the default WooCommerce webhooks are insufficient, you can programmatically create a custom webhook.
Add this to your functions.php file:
/*
* Snippet: How to Integrate Webhooks for Real-Time Inventory Updates in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1827
* Tested with WooCommerce 10.7.0
* "This function programmatically creates a webhook for inventory updates"
*/
function wcsuccess_create_inventory_webhook() {
$webhook = new WC_Webhook();
$webhook->set_name( 'Inventory Updates' );
$webhook->set_status( 'active' );
$webhook->set_topic( 'product.updated' ); // Trigger on product updates
$webhook->set_delivery_url( 'https://your-listener-url.com/webhook-listener.php' ); // Replace with your webhook listener URL
$webhook->set_secret( 'your-webhook-secret' ); // Replace with your secret
$webhook->save();
}
add_action( 'init', 'wcsuccess_create_inventory_webhook' );
Explanation:
- Custom Webhook: Automates the creation of a webhook via code.
- Trigger Event: Listens for product updates to track inventory changes.
- Delivery URL: Sends the webhook to your custom listener.
Step 6: Secure Your Webhook Integration
To ensure your webhook integration is secure:
- Use HTTPS: Always use an HTTPS URL for the webhook listener to encrypt data transmission.
- Validate Signatures: Ensure the webhook listener verifies the request using the secret key.
- Limit Access: Restrict access to the webhook listener to trusted IP addresses or domains.
Example Workflow
- Product Stock Update:
- The stock quantity for a product is updated in WooCommerce.
- Webhook Trigger:
- WooCommerce sends a POST request to the specified webhook listener URL.
- Listener Processes Data:
- The webhook listener validates the request, extracts the stock data, and updates the inventory in the external system.
- Confirmation:
- The webhook listener responds with a 200 OK to confirm successful processing.
Example Use Case
| Event | Webhook Data | Action |
|---|---|---|
| Product stock updated | Product ID: 123, Stock: 10 | External system updates stock. |
| Product out of stock | Product ID: 456, Stock: 0 | External system marks as “Out of Stock.” |
| Product price updated | Product ID: 789, Price: $29.99 | External system updates price. |
Conclusion
Integrating webhooks for real-time inventory updates in WooCommerce ensures accurate stock synchronisation across platforms. By following this guide, you can set up and test a webhook integration that communicates stock changes to an external system in real time.
Test your webhook setup thoroughly in a staging environment before deploying it live. For more WooCommerce customisation tips, explore our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.
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

