skip to Main Content

I am trying to get wooCommerce order id. I wrote the following script in functions.php but i getting 0.Can you tell how to get the Id.

global $woocommerce, $post;
  $order = new WC_Order($post->ID);
  $order_id=$order->get_id();

2

Answers


  1. You can get order id by this function. get_order_number()
    You can use the code given below.

    global $woocommerce, $post;
    $order = new WC_Order($post->ID);
    $order_id = $order->get_order_number();
    

    You can check additional documentation from this link.

    Let me know if it works for you.

    Login or Signup to reply.
  2. Have you tried this ?

    $order_data = wc_get_order( $order_id );
    echo $order_data->get_id();
    

    How you are calling the above code(like hook) in functions.php…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search