I have an ERP system creating products in WooCommerce and I need them to be private instead of published.
I tried the hook woocommerce_rest_insert_product
but it’s not doing anything. I’ve tried adding it on a plugin and on a mu-plugin using plugins_loaded
action.
I found the hook inside the class WC_REST_Products_V1_Controller
, in theory it should work…
/**
* Fires after a single item is created or updated via the REST API.
*
* @param WP_Post $post Post data.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( 'woocommerce_rest_insert_product', $post, $request, false );
2
Answers
I couldn't find the answer to "why" it's not working.
What I found is this WordPress.org forum post showing an alternative that works:
I know this is a late answer but it could make sense for others getting here to read this.
I am pretty sure you are using the latest
v3
of the API.The problem is that the action you mentioned can be found in
WC_REST_Products_V1_Controller
which has the endpoint namespace set to:protected $namespace = 'wc/v1';
This means it is not usable on the v3.
If you go through the Woocommerce REST controllers in Version 3 you will reach this file:
includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php
which has the namespace set to
wc/v3
;Here, the class
WC_REST_Products_Controller
extendsWC_REST_Products_V2_Controller
.In the
WC_REST_Products_V2_Controller
there are thecreate_item
andupdate_item
methods. Both of these methods look very similar to what was in v1 but, the action name you are looking for is changed to:So, your code should look something like:
The big difference between
woocommerce_new_product
andwoocommerce_rest_insert_{$this->post_type}_object
is thatwoocommerce_new_product
is triggered on all create actions, not only on the REST create.