How can I get the order ID from the order Key in WooCommerce?
Asked Answered
Y

1

4

How do I retrieve the order ID from the order KEY in WooCommerce? (PHP code)

e.g.

I have the order KEY which looks like 'wc_order_qkTc2RVyGtVil' and I want to get the order ID which looks like '1950'.

background: I only get an link which contains the order KEY. Thats why I cant use the order OBJECT to get the ID.

Yes answered 20/4, 2021 at 15:34 Comment(0)
M
4

On Order Received, Order Pay and View Order pages, you can get the order id from URL like:

1). On Order Received (Thankyou) page:

$order_id = absint( get_query_var('order-received') );

2). On Order Pay page:

$order_id = absint( get_query_var('order-pay') );

3). On View Order pages:

$order_id = absint( get_query_var('view-order') );

Now you can use wc_get_order_id_by_order_key() WooCommerce function like:

$order_id = wc_get_order_id_by_order_key( $order_key );
Moire answered 20/4, 2021 at 15:51 Comment(1)
Was looking really long for this. It's not mentioned in the WC docs at all. Many thanksGossip

© 2022 - 2025 — McMap. All rights reserved.