Grabbing WooCommerce to create order from admin
In my custom plugin (running in WooCommerce 2.6.x and 3.x) I need to get the order id when creating a new order. I have tried different hooks, but they only work when the customer creates an order, not when the order is created from admin.
I tried:
- woocommerce_new_order
- woocommerce_thankyou
- woocommerce_checkout_order_processed
- woocommerce_checkout_update_order_meta li>
Update
Finally, I used this:
add_action('wp_insert_post', function($order_id)
{
if(!did_action('woocommerce_checkout_order_processed')
&& get_post_type($order_id) == 'shop_order'
&& validate_order($order_id))
{
order_action($order_id);
}
});
where validate_order:
function validate_order($order_id)
{
$order = new \WC_Order($order_id);
$user_meta = get_user_meta($order->get_user_id());
if($user_meta)
return true;
return false;
}
Thanks to validate_order, no action is taken when you start creating an order. I use !did_action('woocommerce_checkout_order_processed')
because I do not want the action to be executed if the order is created by the customer (for this I use a specific action using woocommerce_checkout_order_processed
).
source to share
If you are using the admin page .../wp-admin/post-new.php?post_type=shop_order
to create a new order, there may be no hook for WooCommerce
that, as this order is created by the WordPress core.
However, the WordPress action 'save_post_shop_order'
will be called with $post_ID
, which is the order id.
See function wp_insert_post()
in ...\wp-includes\post.php
.
source to share