Although Easy Digital Downloads shows the total spent for each customer under Downloads → Reports → Customers, there’s no way for a customer themselves to see how much they have spent on your store.

The following code will allow you to show a logged in customer how much they’ve spent, anywhere on your website.

Copy and paste the following function (mind the opening PHP tag) into your child theme’s functions.php or custom plugin:

<?php

/**
 * Get the total spent for a customer.
 * When no customer is passed into the function, it will get the currently logged in user's total spent
 *
 * Usage: <?php echo andrew_edd_get_total_spent_for_customer(); ?>
 */
function andrew_edd_get_total_spent_for_customer( $user_id = '' ) {

	// if no user ID is passed in, default to the currently logged in user.
	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	// Get customers.
	$customers = EDD()->customers->get_customers(
		array(
			'number'  => -1,
			'user_id' => $user_id
		)
	);

	// Get customers purchase values.
	$purchase_values = array();

	if ( $customers ) {
		foreach ( $customers as $customer ) {
			$purchase_values[] = $customer->purchase_value;
		}
	}

	// Get the total spent and format it.
	$total_spent = edd_currency_filter( edd_format_amount( array_sum( $purchase_values ) ) );

	// Return the amount the customer has spent.
	return $total_spent;
}

Then, you can simply call:

<?php echo andrew_edd_get_total_spent_for_customer(); ?>

Alternatively, if you need to show only a specific customer’s total spent, you can pass a user ID into the function like this:

<?php echo andrew_edd_get_total_spent_for_customer( 50 ); ?>

2 comments

  1. Andrew Munro

    This is amazing. Thank you for writing this. Is there any similar tutorials on how to show amount made or sold. I would love to display amount contribute and amount sold for each user next to each other.

  2. Andrew Munro

    Thanks so much Andrew for publishing this post 🙂 It’s very useful indeed.
    I’m trying to add a new row at the bottom of the table shown on purchase history page for each user, and publish the total amount they spent during a specific time period (e.g. April 1st, 2014 till March 31st, 2015). I’d be really grateful if you could provide your input.

    Cheers,
    Mason

Comments are closed.