How to Integrate Webhooks for Real-Time Inventory Updates in WooCommerce – 2026

Last updated on December 29th, 2024 at 08:54 am

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:

  1. Log in to WordPress Admin:
    • Go to WooCommerce > Settings > Advanced > Webhooks.
  2. 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.
  3. 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.
See also  How to Programmatically Manage Product Visibility in WooCommerce Based on Stock Status - 2026

Step 4: Test the Webhook

To ensure the webhook is functioning correctly:

  1. Trigger the Webhook:
    • Update the stock quantity of a product in WooCommerce.
  2. Check the Listener:
    • Verify that the webhook listener logs or processes the update correctly.
  3. 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:

  1. Use HTTPS: Always use an HTTPS URL for the webhook listener to encrypt data transmission.
  2. Validate Signatures: Ensure the webhook listener verifies the request using the secret key.
  3. Limit Access: Restrict access to the webhook listener to trusted IP addresses or domains.
See also  How to Manage Stock Programmatically in WooCommerce - 2026

Example Workflow

  1. Product Stock Update:
    • The stock quantity for a product is updated in WooCommerce.
  2. Webhook Trigger:
    • WooCommerce sends a POST request to the specified webhook listener URL.
  3. Listener Processes Data:
    • The webhook listener validates the request, extracts the stock data, and updates the inventory in the external system.
  4. Confirmation:
    • The webhook listener responds with a 200 OK to confirm successful processing.

Example Use Case

EventWebhook DataAction
Product stock updatedProduct ID: 123, Stock: 10External system updates stock.
Product out of stockProduct ID: 456, Stock: 0External system marks as “Out of Stock.”
Product price updatedProduct ID: 789, Price: $29.99External 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.

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
×