WC_Customer_Data_Store::get_total_spent()publicWC 3.0.0

Return how much money this customer has spent.

Метод класса: WC_Customer_Data_Store{}

Возвращает

float.

Использование

$WC_Customer_Data_Store = new WC_Customer_Data_Store();
$WC_Customer_Data_Store->get_total_spent( $customer );
$customer(WC_Customer) (обязательный) (передается по ссылке — &)
Customer object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Customer_Data_Store::get_total_spent() WC 7.3.0

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() );
		$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 ' . OrdersTableDataStore::get_orders_table_name() . "
				WHERE customer_id = %d
				AND status in $statuses_sql",
				$customer->get_id()
			);
		} else {
			$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->get_id() ) . "'
				AND     posts.post_type     = 'shop_order'
				AND     posts.post_status   IN $statuses_sql
				AND     meta2.meta_key      = '_order_total'";
		}

		//phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
		/**
		 * Filters the SQL query used to get the combined total of all the orders from a given customer.
		 *
		 * @param string The SQL query to use.
		 * @param WC_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 );
		//phpcs:enable WooCommerce.Commenting.CommentHooks.MissingSinceComment

		$spent = $wpdb->get_var( $sql );
		//phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared

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

	return wc_format_decimal( $spent, 2 );
}