In 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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Change WooCommerce Checkout Error Messages
- How to Remove Sidebar from Product Page in WooCommerce Themes
- How to Add Checkout WooCommerce Geolocation Feature
- How to Add Custom Order Status in WooCommerce
- How to Hide Price When Out of Stock In WooCommerce
- How to Create WordPress Database in PHPMyAdmin
- How to Hide Prices From Google In WooCommerce
- How to Hide Weight of Product in WooCommerce
- How to Setup WooCommerce Storefront Blog
- How to Check if User is Logged In in WordPress
- How to Check If It Is Checkout Page WooCommerce
- WooCommerce Checkout Optimization Quick Guide
- How to Hide Shipping Methods for Certain Conditions
- How to Temporarily Disable Checkout In WooCommerce
- How to Add WooCommerce Checkout Default Country
- How to Hide Errors From Visitors WooCommerce
- How to Get Current Product Category Name in WooCommerce
- How to Create MySQL Database WordPress via Command Line
Comments are closed.