WooCommerce 在订单接收端点获取订单 ID 和订单密钥

WooCommerce get order id and order key at order-received endpoint

我试图在重定向客户之前在结账时获取订单号和订单码(wc_order=xxx 参数),但我不确定该怎么做。我的代码在下面,但它不起作用:

add_action( 'template_redirect', 'ui_redirect' );

function ui_redirect(){
    global $woocommerce;
    //if the current page is the order received and if there's an order key

    if (is_wc_endpoint_url( 'order-received' ) ) {    

        $order_key = wc_get_order_id_by_order_key( $_GET['key'] );
        $order_id = wc_get_order( $order_id );

        wp_redirect( 'redirection here with parameters');
        exit;
    }
}

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_redirect', 4 );

function woocommerce_thankyou_redirect( $order_id ) {

    //$order_id. // This contains the specific ID of the order
    $order       = wc_get_order( $order_id );
    $order_key   = $order->get_order_key();

    wp_redirect( 'redirection here with parameters' );
    exit;
}

试试这个代码片段。