Do you want to use WooCommerce to get the last order by user id logic in your WooCommerce plugin or theme and still cannot figure out how to get this code to work?
In this post, I want to show you how to get the last order when you have the User ID of a specific user.
In most cases when you are creating conditional logic in WooCommerce development you may want to get the user details and also the ID.
In the previous post on how to get a user ID in WordPress, I highlighted the step by steps, you should take to get the current user ID.
This can be a great place to start to understand how you can get the WooCommerce user ID that you can now apply in the WooCommerce get last order by user_id logic.
WooCommerce Get Last Order By user-id
The first step is to check if the user is logged and you can do this using the code I shared in the previous tutorial – Check if a user is logged in.
After checking if the user is logged in you can now get the user ID and then use the WC_Customer class to get the current customer data.
The following is the code you should use to get the last order of the current customer.
if ( is_user_logged_in() ) : $user_id = get_current_user_id(); // The current user ID // Get the WC_Customer instance Object for the current user $customer = new WC_Customer( $user_id ); // Get the last WC_Order Object instance from current customer $last_order = $customer->get_last_order(); $order_id = $last_order->get_id(); // Get the order id $order_data = $last_order->get_data(); // Get the order unprotected data in an array $order_status = $last_order->get_status(); // Get the order status endif;
WooCommerce Get Last Order By user id Example
You can now go ahead and add this code in the header using an action hook and display the last order ID to just see if this code works. The code below can be added to your functions.php file to test and see if it works :
add_action('wp_head', 'display_example_code_header'); function display_example_code_header(){ // For logged-in users only if ( is_user_logged_in() ) : $user_id = get_current_user_id(); // The current user ID // Get the WC_Customer instance Object for the current user $customer = new WC_Customer( $user_id ); // Get the last WC_Order Object instance from current customer $last_order = $customer->get_last_order(); $order_id = $last_order->get_id(); // Get the order id $order_data = $last_order->get_data(); // Get the order unprotected data in an array $order_status = $last_order->get_status(); // Get the order status print('<pre>'); print 'Last Order ID for current user is' . " " . $order_id ; print('</pre>'); endif; }
If you have added the code above in your functions.php file, you should see the last order ID displayed as shown in the image below:
Conclusion
In this post, we have looked at how you can get the last order of the current user or when you have an ID of a user how you can get the last order of that user.
This can be a useful snippet when you want to customize the customer order display or you want to create some conditional rules for orders such as discounted prices based on the last order.
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 Delete a Category in WordPress
- How to Remove Gravatar Storefront Blog WooCommerce
- How to Set Up WooCommerce Role-Based Payment Methods
- How to Create Super Admin In WordPress
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Check If WooCommerce Plugin Is Active
- How to Change Out Of Stock Text in WooCommerce
- How to Change Checkout Endpoints WooCommerce
- How to Create Custom Order Received Page WooCommerce
- How to Change Text in WooCommerce Email
- How to Add New Column On WooCommerce Orders Page
- How to Hide Customer Order Email For Free Orders WooCommerce
- How to Display All Products Purchased by User – Purchase History
- How to Get Product ID on the Checkout Page Or Order Page In WooCommerce
- How to Change Return to Shop Link in Woocommerce
- How to Check If Current Page Is Category WordPress
- How to Get Order ID on Checkout Page WooCommerce
- How to Auto Approve Orders in WooCommerce