Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

php - WooCommerce hook for order creation from admin

In my custom plugin (working in WooCommerce 2.6.x and 3.x), I need to get the order ID when a new order is created. I tried different hooks but they work only when the customer creates an order and not when an order is created from admin.

I tried:

  • woocommerce_new_order
  • woocommerce_thankyou
  • woocommerce_checkout_order_processed
  • woocommerce_checkout_update_order_meta

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 is:

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 the action isn't executed when you start to create the order. I use !did_action('woocommerce_checkout_order_processed') because I don't want that the action is executed if the order is created by a customer (I have a specific action for that, using woocommerce_checkout_order_processed).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

woocommerce_new_order hook is called after order creation:

add_action('woocommerce_new_order', function ($order_id) {
    // ...
}, 10, 1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...