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).
3
Answers
Please use this hook
woocommerce_process_shop_order_meta
instead ofwoocommerce_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.On v2 there is the
woocommerce_rest_insert_{$this->post_type}_object
filterSo the correct hook for what you need is
woocommerce_rest_insert_shop_order_object
You have tried
woocommerce_rest_insert_shop_object
,but the hook is
woocommerce_rest_insert_shop_order_object