Automattic\WooCommerce\Admin\API
Leaderboards::get_products_leaderboard
Get the data for the products leaderboard.
Method of the class: Leaderboards{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->get_products_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_products_leaderboard() Leaderboards::get products leaderboard code WC 10.3.6
protected function get_products_leaderboard( $per_page, $after, $before, $persisted_query ) {
$products_data_store = new ProductsDataStore();
$products_data = $per_page > 0 ? $products_data_store->get_data(
apply_filters(
'woocommerce_analytics_products_query_args',
array(
'orderby' => 'items_sold',
'order' => 'desc',
'after' => $after,
'before' => $before,
'per_page' => $per_page,
'extended_info' => true,
)
)
)->data : array();
$rows = array();
foreach ( $products_data as $product ) {
$url_query = wp_parse_args(
array(
'filter' => 'single_product',
'products' => $product['product_id'],
),
$persisted_query
);
$product_url = wc_admin_url( '/analytics/products', $url_query );
$product_name = isset( $product['extended_info'] ) && isset( $product['extended_info']['name'] ) ? $product['extended_info']['name'] : '';
$rows[] = array(
array(
'display' => "<a href='{$product_url}'>{$product_name}</a>",
'value' => $product_name,
),
array(
'display' => wc_admin_number_format( $product['items_sold'] ),
'value' => $product['items_sold'],
'format' => 'number',
),
array(
'display' => wc_price( $product['net_revenue'] ),
'value' => $product['net_revenue'],
'format' => 'currency',
),
);
}
return array(
'id' => 'products',
'label' => __( 'Top products - Items sold', 'woocommerce' ),
'headers' => array(
array(
'label' => __( 'Product', 'woocommerce' ),
),
array(
'label' => __( 'Items sold', 'woocommerce' ),
),
array(
'label' => __( 'Net sales', 'woocommerce' ),
),
),
'rows' => $rows,
);
}