Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 631
Marv560 Avatar asked Oct 31 '25 12:10

Marv560


1 Answers

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 );
like image 74
LoicTheAztec Avatar answered Nov 03 '25 05:11

LoicTheAztec