0

I'm trying to get order number and order key (wc_order=xxx parameter) at checkout before redirect customer, but I'm not sure what to do. My code is below but it's not working:

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;
    }
}

mujuonly
  • 11,370
  • 5
  • 45
  • 75
PLazzo
  • 169
  • 2
  • 13

1 Answers1

2
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;
}

Try this code snippet.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • It's ok for order_key, but for ```$order_id``` I did receive the full data of order, as example: ```order: {"id":1685,"parent_id":0,"status":"processing","currency":"USD","version":"4.1.0","prices_include_tax":false,"date_created":{"date":"2020-05-10 15:23:12.000000","timezone_type": (etc)...``` – PLazzo May 10 '20 at 18:25
  • I want just the order number, not the full array. In above example, just the data "1685" (order-id). – PLazzo May 10 '20 at 18:28
  • Great! Thank you very much! – PLazzo May 10 '20 at 18:32