If you want to display products purchased by user or display all the purchase history for a specific user, you can achieved this using the code shared in this post. Ideally, all the customer orders or purchase history is saved to the posts table in the WordPress database –
wp_posts. You can therefore get the data from the database table using the
get_post().
function as I will illustrate using an example.
WooCommerce Display Products Purchased By User
The first step to display products purchased by customer, we need to get the details of the customer and we can make use of the
wp_get_current_user() function that returns the object of the current user.
Step 1 : Create the Function to Display Customer History & Get User Details
For example the function and get the current user details as follows :
function njengah_get_customer_purchase_history(){ // Get the current user Object $current_user = wp_get_current_user(); // Check if the user is valid if ( 0 == $current_user->ID ) return; }
The
wp_get_current_user() function retrieves the user object and you can alternatively use a function like get_current_user_id()
that directly retrieves the current user ID.
Step 2 : Get the User Orders Both Completed and Processing
In this step we need to use the get_posts() function and pass the args with the user ID we obtained in the first step and in this case the user id will be passed as the meta_value in the $args. So we begin by creating the arguments array that we will pass to the get_posts() function.
//Create $args array $args = array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $current_user->ID, 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_is_paid_statuses() ), );
After creating the arguments we not pass the $args array to the get_posts() function as a parameter as follows :
$customer_orders = get_posts( $args);
Before we move to the next step we combine the code in step 1 and step 2 and the combined code should be as follows :
function njengah_get_customer_purchase_history(){ // Get the current user Object $current_user = wp_get_current_user(); // Check if the user is valid if ( 0 == $current_user->ID ) return; //Create $args array $args = array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $current_user->ID, 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_is_paid_statuses() ), ); // Pass the $args to get_posts() function $customer_orders = get_posts( $args); }
In this step we have now got all the purchase history of the previous customer as an array that we will loop through to get the products IDs in the next step.
Step 3 : Loop Through Customer Orders & Return Products IDs Ready for Display
In this step we need to loop through the orders we obtained in the previous step and return an array with the product IDs. We can do so using the following code using foreach loop.
// loop through the orders and return the IDs if ( ! $customer_orders ) return; $product_ids = array(); foreach ( $customer_orders as $customer_order ) { $order = wc_get_order( $customer_order->ID ); $items = $order->get_items(); foreach ( $items as $item ) { $product_id = $item->get_product_id(); $product_ids[] = $product_id; } } return $product_ids;
We can now combine the code in step 1, step 2 and step 3 to have the complete code as follows :
function njengah_get_customer_purchase_history(){ // Get the current user Object $current_user = wp_get_current_user(); // Check if the user is valid if ( 0 == $current_user->ID ) return; //Create $args array $args = array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $current_user->ID, 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_is_paid_statuses() ), ); // Pass the $args to get_posts() function $customer_orders = get_posts( $args); // loop through the orders and return the IDs if ( ! $customer_orders ) return; $product_ids = array(); foreach ( $customer_orders as $customer_order ) { $order = wc_get_order( $customer_order->ID ); $items = $order->get_items(); foreach ( $items as $item ) { $product_id = $item->get_product_id(); $product_ids[] = $product_id; } } return $product_ids; }
Step 4 : Testing the Function Return
In this step we can check to see if the data is displayed from the function in the step 3 using the print_r() function as follows:
print_r(njengah_get_customer_purchase_history());
If you followed all the steps in the right way, you should see the data displayed as seen on the image below where I have added the data display to the wp_head action hook .
You can now go ahead and use this data in a shortcode or in your theme or plugin development to display the purchase history anywhere you wish.
Conclusion
In this post I have explained how to display the product purchased by user in WooCommerce in step by step. The ideal use of this code can be in any logic where you want to compare the current order products to the previously ordered products. In most practical applications include the allocation of discounts based on previous orders or cross sells and up sells.
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 Redirect On Refresh WordPress Page » Detect Page Refresh PHP
- How to Get Post ID by Slug in WordPress With a Practical Example
- How to Get Current Product Category Name in WooCommerce
- How to Get Logged In User Information in WordPress
- How to Hide a Product in WooCommerce or Hide Products by Category or Roles
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Remove WordPress Admin Menu Items Programmatically Without Plugin
- How to Hide Admin Bar for WooCommerce Customers or By User Roles
- How to Create Number Pagination in WordPress Without Using Plugin
- How to Change Return to Shop Link in Woocommerce
- WooCommerce Logout without Confirmation : How to Remove “Are you sure you want to log out?”
- How to Get Current User Role in WordPress & Display Roles
- How to Redirect a WordPress Page Without Plugins?
- How to Check If Plugin is Active In WordPress [ 3 WAYS ]
- How to Add a Sidebar to WordPress » Ultimate Step by Step Guide
- How to Redirect Users after Successful Login in WordPress without Using a Plugin
- How to Register a Sidebar in WordPress Theme » Step by Step [ Header Widget Example ]
- How to Redirect to Cart after Login in WooCommerce
- How to Check if User is Logged In in WordPress
- How to Display Stock Availability Text in WooCommerce » In Stock & Out
Comments are closed.