How can I get the order ID in WooCommerce?

后端 未结 5 1159
再見小時候
再見小時候 2020-12-03 02:52

How do I retrieve the order ID in WooCommerce?

相关标签:
5条回答
  • 2020-12-03 03:34
    $order = new WC_Order( $post_id ); 
    

    If you

    echo $order->id;
    

    then you'll be returned the id of the post from which the order is made. As you've already got that, it's probably not what you want.

    echo $order->get_order_number();
    

    will return the id of the order (with a # in front of it). To get rid of the #,

    echo trim( str_replace( '#', '', $order->get_order_number() ) );
    

    as per the accepted answer.

    0 讨论(0)
  • 2020-12-03 03:36

    As of woocommerce 3.0

    $order->id;
    

    will not work, it will generate notice, use getter function:

    $order->get_id();
    

    The same applies for other woocommerce objects like procut.

    0 讨论(0)
  • 2020-12-03 03:45

    it worked. Just modified it

    global $woocommerce, $post;
    
    $order = new WC_Order($post->ID);
    
    //to escape # from order id 
    
    $order_id = trim(str_replace('#', '', $order->get_order_number()));
    
    0 讨论(0)
  • 2020-12-03 03:50

    This is quite an old question now, but someone may come here looking for an answer:

    echo $order->id;
    

    This should return the order id without "#".

    EDIT (feb/2018)

    The current way of accomplishing this is by using:

    $order->get_id();
    
    0 讨论(0)
  • 2020-12-03 03:50

    I didnt test it and dont know were you need it, but:

    $order = new WC_Order(post->ID);
    echo $order->get_order_number();
    

    Let me know if it works. I belive order number echoes with the "#" but you can split that if only need only the number.

    0 讨论(0)
提交回复
热议问题