1

I'm currently trying to receive the current order id within a custom class. First, I'm checking if I'm on a view-order page. If yes, I'm trying to receive the id. The problem is, that the returned id is 19 but it should be 6456, really strange. I'm initializing my class in my functions.php of my child theme:

public function __construct() {
    if ( is_view_order_page() ) {
        error_log( get_the_ID() );
    }
}

Does someone has an idea how I can solve this?

Update:

Another idea I had was to read the request uri which is the following:

https://localhost/dashboard/view-order/6264/

So I came out with this idea:

public function __construct() {
    if ( is_view_order_page() ) {
        $uri_path     = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
        $uri_segments = explode( '/', $uri_path );

        error_log(  $uri_segments[3] );
    }
}

But this is not that safe and good I think. If there is a better way to do this, please let me know. Thanks a lot!

Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • Did you try this solution to get the order ID? https://stackoverflow.com/a/27963922/3623080 – gael Jan 22 '20 at 15:28
  • This is exactly what the `get_the_ID()` function does I think. I only skipped the part getting the order because I can't even get the right ID https://developer.wordpress.org/reference/functions/get_the_id/ – Mr. Jo Jan 22 '20 at 15:31

1 Answers1

0

You can try this way:

public function __construct() {
    if ( is_view_order_page() ) {
        global $wp;
        $order_id = wc_clean( $wp->query_vars['view-order'] );
        $_order = wc_get_order($order_id);
        // some logic

        error_log(  $_order->get_id() );
    }
}

I checked. It's work fine

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42