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 );
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
extends WC_REST_Products_V2_Controller
.
In the WC_REST_Products_V2_Controller
there are the create_item
and update_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:
/**
* Fires after a single object is created or updated via the REST API.
*
* @param WC_Data $object Inserted object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating object, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true );
So, your code should look something like:
add_action(
"woocommerce_rest_insert_product_object",
function($product, $request, $creating ){
// do something here
}, 10, 3
);
The big difference between woocommerce_new_product
and woocommerce_rest_insert_{$this->post_type}_object
is that woocommerce_new_product
is triggered on all create actions, not only on the REST create.