How to Display All Products Purchased by User – Purchase History

Display Products Purchased by UserIf 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 .

display products purchased by user

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.

Similar  Articles

 

Comments are closed.