How to Get Order ID on Checkout Page WooCommerce

WooCommerce Get Order ID on Checkout PageIn the previous post, I shared with you how to check if is checkout page in WooCommerce. In this post, I want to go further and illustrate how to get order id on checkout page in WooCommerce.

In most cased when you want to create some checkout logic like redirect after checkout based on product or any other condition, you may want to know how to get order id on checkout page. Its rather a simple and straightforward trick as you will see in the code snippet I have shared below.

If you want to create redirect after checkout you may want to checkout a brilliant plugin I developed for this work and it has been very useful for more than 1000 users – WooCommerce Redirect After Checkout Plugin. 

WooCommerce Get Order ID on Checkout Page

To get order ID on checkout age, you need to first determine if the user is on the checkout page and if that is the case you go ahead and get the order ID as shared on the code snippet below :

function njengah_get_order_id(){            
                    
    if ( is_wc_endpoint_url( 'order-received' ) ) {

      global $wp;

      //Get Order ID 
      $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
                        
      // Get an instance of the WC_Order object
      $order = new WC_Order( $order_id );
             
   }

}

Conclusion

In this post, we have highlighted the way to get order ID on the checkout page. You can go ahead to add more logic to this code and use it to check the items on the order and create the redirect after checkout based on the order items.  If you are completely stuck and would like some help with this, please feel free to get in touch with me.

Similar Articles

  1. How to Create MySQL Database WordPress via Command Line

Comments are closed.