Automattic\WooCommerce\Admin\API

Leaderboards::get_customers_leaderboard()protectedWC 1.0

Get the data for the customers leaderboard.

Method of the class: Leaderboards{}

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->get_customers_leaderboard( $per_page, $after, $before, $persisted_query );
$per_page(int) (required)
Number of rows.
$after(string) (required)
Items after date.
$before(string) (required)
Items before date.
$persisted_query(string) (required)
URL query string.

Leaderboards::get_customers_leaderboard() code WC 8.7.0

protected function get_customers_leaderboard( $per_page, $after, $before, $persisted_query ) {
	$customers_data_store = new CustomersDataStore();
	$customers_data       = $per_page > 0 ? $customers_data_store->get_data(
		apply_filters(
			'woocommerce_analytics_customers_query_args',
			array(
				'orderby'      => 'total_spend',
				'order'        => 'desc',
				'order_after'  => $after,
				'order_before' => $before,
				'per_page'     => $per_page,
			)
		)
	)->data : array();

	$rows = array();
	foreach ( $customers_data as $customer ) {
		$url_query    = wp_parse_args(
			array(
				'filter'    => 'single_customer',
				'customers' => $customer['id'],
			),
			$persisted_query
		);
		$customer_url = wc_admin_url( '/analytics/customers', $url_query );
		$rows[]       = array(
			array(
				'display' => "<a href='{$customer_url}'>{$customer['name']}</a>",
				'value'   => $customer['name'],
			),
			array(
				'display' => wc_admin_number_format( $customer['orders_count'] ),
				'value'   => $customer['orders_count'],
			),
			array(
				'display' => wc_price( $customer['total_spend'] ),
				'value'   => $customer['total_spend'],
			),
		);
	}

	return array(
		'id'      => 'customers',
		'label'   => __( 'Top Customers - Total Spend', 'woocommerce' ),
		'headers' => array(
			array(
				'label' => __( 'Customer Name', 'woocommerce' ),
			),
			array(
				'label' => __( 'Orders', 'woocommerce' ),
			),
			array(
				'label' => __( 'Total Spend', 'woocommerce' ),
			),
		),
		'rows'    => $rows,
	);
}