WC_Customer_Data_Store::get_total_spent
Return how much money this customer has spent.
Method of the class: WC_Customer_Data_Store{}
Hooks from the method
Returns
float.
Usage
$WC_Customer_Data_Store = new WC_Customer_Data_Store(); $WC_Customer_Data_Store->get_total_spent( $customer );
- $customer(WC_Customer) (required) (passed by reference — &)
- Customer object.
Changelog
| Since 3.0.0 | Introduced. |
WC_Customer_Data_Store::get_total_spent() WC Customer Data Store::get total spent code WC 10.6.2
public function get_total_spent( &$customer ) {
$customer_id = $customer->get_id();
/**
* Filters total spent value for a given customer.
*
* @since 3.1.0
*
* @param mixed $money_spent The cached money spent value (from user meta).
* @param WC_Customer $customer The customer to get the total spent for.
* @return mixed The actual value to use.
*/
$spent = apply_filters(
'woocommerce_customer_get_total_spent',
Users::get_site_user_meta( $customer_id, 'wc_money_spent', true ),
$customer
);
if ( '' === $spent ) {
global $wpdb;
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$statuses_sql = "( 'wc-" . implode( "','wc-", $statuses ) . "' )";
//phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( $this->is_cot_in_use() ) {
$sql = $wpdb->prepare(
"SELECT SUM(total_amount) FROM %i WHERE customer_id = %d AND status IN $statuses_sql",
OrdersTableDataStore::get_orders_table_name(),
$customer_id
);
} else {
$has_sql_modification_filter = has_filter( 'woocommerce_customer_get_total_spent_query' );
if ( $has_sql_modification_filter ) {
// For backward compatibility: external filters might rely onto the query structure.
$sql = "SELECT SUM(meta2.meta_value)
FROM {$wpdb->posts} as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = '" . esc_sql( $customer_id ) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN $statuses_sql
AND meta2.meta_key = '_order_total'";
} else {
$sql = "SELECT SUM(postmeta.meta_value)
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id
WHERE posts.ID IN (
SELECT posts.ID as order_id
FROM {$wpdb->posts} AS posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id
WHERE postmeta.meta_key = '_customer_user'
AND postmeta.meta_value = '" . esc_sql( $customer_id ) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN $statuses_sql
)
AND postmeta.meta_key = '_order_total'";
}
}
/**
* Filters the SQL query used to get the combined total of all the orders from a given customer.
*
* @since 3.1.0
*
* @param string $sql The SQL query to use.
* @param WC_Customer $customer The customer to get the total spent for.
* @return string The actual SQL query to use.
*/
$sql = apply_filters( 'woocommerce_customer_get_total_spent_query', $sql, $customer );
$spent = $wpdb->get_var( $sql );
//phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( ! $spent ) {
$spent = 0;
}
Users::update_site_user_meta( $customer_id, 'wc_money_spent', $spent );
}
return wc_format_decimal( $spent, 2 );
}