如何获得woocommerce_payment_complete挂钩中的订单ID

时间:2020-07-09 11:31:34

标签: php wordpress woocommerce hook-woocommerce orders

我已经完成了WooCommerce订单付款成功的功能。

add_action( 'woocommerce_payment_complete', 'do_it' ); 
function do_it( $order_id ){
    // Here will run the function
    $order_id="//I want the order id here of woocommerce";
    $order = wc_get_order( $order_id ); //Then I can get order details from here
}

如何通过此钩子在这里获得$ order_id?

2 个答案:

答案 0 :(得分:1)

您的函数中已经具有订单ID作为参数。

add_action('woocommerce_payment_complete', 'do_it', 10, 1);
function do_it($order_id) {
    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {
        if ($item['product_id']==24) {
          // Do something clever
        }
    }
    return $order_id;
}

答案 1 :(得分:0)

尝试一下。

add_action( 'woocommerce_payment_complete', 'do_it' ); 
function do_it( $orderId ){
    // Get an instance of the WC_Order object (same as before)
    $order = wc_get_order( $order_id );
    $orderId = $order->get_id(); // Get the order ID
}