Reputation: 7299
I have the following code snippet:
add_action('woocommerce_new_order', 'foo_function', 10);
If I create a new order from the admin panel, this fires just fine.
However, creating it via the REST API will not fire the function.
Why is that?
Update
I've tried using the following:
woocommerce_rest_insert_shop_object
– this one doesn't fire at all.
wp_insert_post
and save_post
– they do fire, but don't contain the line items... (on the first run, it's an empty list, and on the second run (where the $update flag is true) there is an item but that item has no data (like product id or quantity).
Upvotes: 5
Views: 2388
Reputation: 2247
add_action( "woocommerce_rest_insert_shop_order_object", 'your_prefix_on_insert_rest_api', 10, 3 );
function your_prefix_on_insert_rest_api( $object, $request, $is_creating ) {
if ( ! $is_creating ) {
return;
}
$order_id = $object->get_id();
$wc_order = new WC_Order( $order_id );
do_action( 'woocommerce_new_order', $order_id, $wc_order );
}
You have tried woocommerce_rest_insert_shop_object
,
but the hook is woocommerce_rest_insert_shop_order_object
Upvotes: 3
Reputation: 566
On v2 there is the woocommerce_rest_insert_{$this->post_type}_object
filter
So the correct hook for what you need is woocommerce_rest_insert_shop_order_object
Upvotes: 0
Reputation: 248
Please use this hook woocommerce_process_shop_order_meta
instead of woocommerce_new_order
As far as I can determine, woocommerce_new_order is only fired when an order is processed through the checkout. In a lot of instances, staff were creating orders in the wp-admin and assigning them straight to processing or completed, which meant that hook wasn't doing what I thought it would. Using woocommerce_process_shop_order_meta will solve it for you.
Upvotes: 1