Fix WooCommerce webhook loop when using WooCommerce API

Problem (Quick)

Updating a product via the WooCommerce API would fire an update webhook, causing a loop of webhook and API activity which never ended.

Problem

Created a script which keeps stock levels synced between WooCommerce and a stock management system, WooCommerce WebHooks are used to set products stock levels in near real time. Using WooCommerce API new stock levels are pushed to WooCommerce, however with each API push this would trigger a product updated webhook, causing my script to reinforce the stock levels via API (causing an unlimited updating loop).

Solution

We can use  woocommerce_webhook_should_deliver  filter to control if a webhook should be delivered.

The following example shows how I used the clients IP address (in this example my scripts server) to stop webhook delivery for any requests from that IP address, this allows my script to use the API and webhooks will never be delivered.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment

  1. Thank you! I had an infinite loop because my backend updates the order metadata when an order is updated, and that triggers the order.updated again. Hooking woocommerce_webhook_should_deliver as described in the example allowed me to prevent it.