How to Get Last Order by User Id WooCommerce

woocommerce get last order by user id

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:

WooCommerce Get Last Order By user id current user

 

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.

Similar Articles