Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Easy Digital Downloads - Get the total spent for a specific customer
/**
* 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 sumobi_edd_get_total_spent_for_customer( $user_id ); ?>
*/
function sumobi_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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment