douzi7890 2017-06-13 13:25
浏览 22
已采纳

从管理员创建订单的WooCommerce钩子

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).

  • 写回答

2条回答 默认 最新

  • dtiu94034 2017-06-14 05:47
    关注

    If you are using the admin page .../wp-admin/post-new.php?post_type=shop_order to create the new order then there may not be a WooCommerce hook to do this as this order is created by the WordPress core.

    However, the WordPress action 'save_post_shop_order' will be called with the $post_ID which is the order id.

    See function wp_insert_post() in ...\wp-includes\post.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题