1

I am trying to get the order id in the checkout page when i choose shipping methods.

I have some logic which needs an order id and must be displayed in the chekout page.

However I wasn't able to get the order id no matter how I tried.

I am already aware of this but wasn't able to use it : https://stackoverflow.com/a/43318152/11328573

I tried to add this code in functions.php

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 3);
function action_checkout_order_processed( $order_id, $posted_data, $order ) {
    echo "<script>alert(".$order_id.")</script>";
}

But my alert wasn't executed.

EDIT :

I am already aware that an order doesn't exist yet so I want to"predict" the order Id ,if that makes since, for example if woocommerce orders ids are inceremented by 1 I can get it by: id_order = idLastOrder+1; something like that.

Otherwise I would like to get a cart Id I know that a cart id doesn't exist in woocommerce but how can I create one and then use it in REST API to get orders data.

Please help

Nicole
  • 134
  • 15
  • 2
    You can not get the order ID before payment as it doesn't exist yet – LoicTheAztec Apr 08 '19 at 11:09
  • Did you check [Woocommerce: Get order id in checkout page](https://stackoverflow.com/questions/33610832/woocommerce-get-order-id-in-checkout-page?rq=1)? – Masivuye Cokile Apr 08 '19 at 11:12
  • LoicTheAztec I am pretty sure there must be a workaround for that and this is what I am looking for. Thanks for the reply – Nicole Apr 08 '19 at 11:20
  • Masivuye Cokile Yes I did but if I echo the $order_id It's empty. Thanks for the reply – Nicole Apr 08 '19 at 11:23
  • Just to make it a little bit clear the order is generated after payment. the code of generation gives the order an Id and other parameters I want something similar to that code but stops before creating the order. There must be a way that woocommerce generate IDs If i can access that I will be able to get the id which will be given to my order. – Nicole Apr 08 '19 at 11:28
  • The order ID and order data **is generated just before payment**. You can see that on `WC_Checkout` [`process_checkout()`](https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#940-995) method source code, where you will see [`create_order()`](https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#976) method that create the Order object, save the data generating the Order ID. But you will not be able to get any Order ID in Checkout page. – LoicTheAztec Apr 08 '19 at 12:58
  • I see... thanks alot this somehow gave me an idea, I want to use the session key : send it to my server and then in payment I send a request to myserver to update the session key by the order_id, will I be able to do that I mean will an order_id and session_key exist at the same time ? If yes in what hook ? I really appreciate your help !! : I want to do this with session_key since it is unique please correct me if I am wrong – Nicole Apr 08 '19 at 13:15

0 Answers0