I have purchased the Woocommerce Subscriptions plugin and i want to be able to get the order ID in checkout page. I want this, because when a subscription is about to end, an email with a payment link is sent to the customer and an order is automatically added. When customer clicks the link, it redirects to the checkout page in order to pay for the order. I need the order ID to retrieve meta data from the specific order using some function like wc_get_order_item_meta().
Asked
Active
Viewed 1.8k times
1
-
The order ID assume with be in the URL params? If it's in the docs please look for yourself. https://docs.woothemes.com/wc-apidocs/ – Matt The Ninja Nov 09 '15 at 14:04
-
1@MattTheNinja The order ID is in the URL params of the "Pay now" link contained in the email sent to the customer. But when you click this link, it redirects to the checkout page with no params in the URL. It would be a nice idea to get the order ID from this link, but i don't know how to do it. Thank you. – Nikos Nov 09 '15 at 15:49
-
IS **NOT POSSIBLE TO GET THE ORDER ID IN CHECKOUT PAGE** … IT DOESN'T EXIST YET. So I have changed the thread title a bit regarding answers in here and make it as a duplicated thread of an existing one. – LoicTheAztec Apr 08 '19 at 11:21
-
This is not a duplicate question of the linked question: https://stackoverflow.com/q/21633120/2540235. Note that the accepted answer for the linked question does NOT work here. – Design.Garden Aug 21 '19 at 15:23
3 Answers
7
You can get it like this:
global $wp;
$order_id = $wp->query_vars['order-pay'];
$order = new WC_Order( $order_id );

ababak
- 1,685
- 1
- 11
- 23
-
Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Jun 08 '16 at 11:50
-
That's just the way to get the order id. Then the id is the used to populate the order. If you have some specific questions, I'll try to answer. – ababak Jun 09 '16 at 12:56
-
1If on the "thankyou.php" page then use: $order_id = $wp->query_vars['order-received']; – Design.Garden Aug 21 '19 at 15:17
-
1
This is stored in the WC_Order
class as a property. This means you can use:
WC()->order->id;

rnevius
- 26,578
- 10
- 58
- 86
-
6Unfortunately, when i execute `echo WC()->order->id;` , i get this notice: "Trying to get property of non-object". @rnevius – Nikos Nov 09 '15 at 16:02
1
Get an order at the checkout page, mmmm, don't think an order exists at this point, only a cart.

user25794
- 716
- 7
- 14