- Published on
Show customers how much they have spent on your Easy Digital Downloads store
- Authors
- Name
- Andrew Munro
- @andrewmunro_
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:
/** * 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 ); ?>