Automattic\WooCommerce\Admin\API

Leaderboards::get_categories_leaderboard()protectedWC 1.0

Get the data for the categories leaderboard.

Method of the class: Leaderboards{}

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->get_categories_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_categories_leaderboard() code WC 8.6.1

protected function get_categories_leaderboard( $per_page, $after, $before, $persisted_query ) {
	$categories_data_store = new CategoriesDataStore();
	$categories_data       = $per_page > 0 ? $categories_data_store->get_data(
		apply_filters(
			'woocommerce_analytics_categories_query_args',
			array(
				'orderby'       => 'items_sold',
				'order'         => 'desc',
				'after'         => $after,
				'before'        => $before,
				'per_page'      => $per_page,
				'extended_info' => true,
			)
		)
	)->data : array();

	$rows = array();
	foreach ( $categories_data as $category ) {
		$url_query     = wp_parse_args(
			array(
				'filter'     => 'single_category',
				'categories' => $category['category_id'],
			),
			$persisted_query
		);
		$category_url  = wc_admin_url( '/analytics/categories', $url_query );
		$category_name = isset( $category['extended_info'] ) && isset( $category['extended_info']['name'] ) ? $category['extended_info']['name'] : '';
		$rows[]        = array(
			array(
				'display' => "<a href='{$category_url}'>{$category_name}</a>",
				'value'   => $category_name,
			),
			array(
				'display' => wc_admin_number_format( $category['items_sold'] ),
				'value'   => $category['items_sold'],
			),
			array(
				'display' => wc_price( $category['net_revenue'] ),
				'value'   => $category['net_revenue'],
			),
		);
	}

	return array(
		'id'      => 'categories',
		'label'   => __( 'Top categories - Items sold', 'woocommerce' ),
		'headers' => array(
			array(
				'label' => __( 'Category', 'woocommerce' ),
			),
			array(
				'label' => __( 'Items sold', 'woocommerce' ),
			),
			array(
				'label' => __( 'Net sales', 'woocommerce' ),
			),
		),
		'rows'    => $rows,
	);
}