Does the REST API trigger save_post or not?
-
Hi
We are using Linnworks on a WooCommerce project.
We have code in place that is hooked to the
save_post()hook and whenever we update a product, it performs some custom logic, and then updates a custom meta we added to the Products.This works smoothly of course, but as soon as the product is “updated” by Linnworks, our code, hooked to
save_post()does not fire anymore.We reached out to Linnworks and they say:
we don’t access WooCommerce backend directly like that (using
save_post()action) – we call it via the API.
http://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-product
The request we send setsmanaging_stockto true, and setsstock_quantityto the quantity.With this they explain the fact that
save_post()is not triggered, but this makes no sense to us.
As far we know save_post is triggered by any REST request updating any post. How else would REST update a post, if not thru save_post, even if it is not accessed directly?After all,
save_postis fired at the end ofwp_insert_post()which is the core function that’s run whenever a post is inserted or updated (wp_update_post()calls it internally). This includes when the post is updated via the classic editor and the block editor (Gutenberg), as well whenever it’s updated via the REST API. The only reason it wouldn’t fire is if the post was being updated via SQL directly (via a plugin or otherwise), or when only post meta is updated via a function.So, my question is:
Is it true that when any external service such as Linnworks updates a Product thru the REST API, save_post() is NOT fired?
And, how would we then “listen” to the updating process of that external service?
It MUST be possible to somehow detect when a product is updated, wether it is updated with a REST or PHP call should really not matter!We would really appreciate if WooCommerce could shed some light into this, because to us it seems very much wrong that a “REST Api call does not trigger save_post” like Linnworks states, and yet we can see with our own eyes that our hooks do not work whenever a product is updated by them, so there must be some truth to it, however in that case, please could you instruct how we can detect a update process happening in REST?
We need to hook our own code logic to each update post event no matter if its thru REST or backend.
Thanks a lot
The topic ‘Does the REST API trigger save_post or not?’ is closed to new replies.