WC_Customer_Data_Store::get_total_spent( WC_Customer $customer )

Return how much money this customer has spent.


Description Description


Parameters Parameters

$customer

(Required) Customer object.


Top ↑

Return Return

(float)


Top ↑

Source Source

File: includes/data-stores/class-wc-customer-data-store.php

	public function get_total_spent( &$customer ) {
		$spent = apply_filters(
			'woocommerce_customer_get_total_spent',
			get_user_meta( $customer->get_id(), '_money_spent', true ),
			$customer
		);

		if ( '' === $spent ) {
			global $wpdb;

			$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
			$spent    = $wpdb->get_var(
				// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
				apply_filters(
					'woocommerce_customer_get_total_spent_query',
					"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->get_id() ) . "'
					AND     posts.post_type     = 'shop_order'
					AND     posts.post_status   IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
					AND     meta2.meta_key      = '_order_total'",
					$customer
				)
				// phpcs:enable
			);

			if ( ! $spent ) {
				$spent = 0;
			}
			update_user_meta( $customer->get_id(), '_money_spent', $spent );
		}

		return wc_format_decimal( $spent, 2 );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.