Automattic\WooCommerce\Admin\API

Leaderboards::get_coupons_leaderboard()protectedWC 1.0

Get the data for the coupons leaderboard.

Method of the class: Leaderboards{}

Return

null. Nothing (null).

Usage

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

protected function get_coupons_leaderboard( $per_page, $after, $before, $persisted_query ) {
	$coupons_data_store = new CouponsDataStore();
	$coupons_data       = $per_page > 0 ? $coupons_data_store->get_data(
		apply_filters(
			'woocommerce_analytics_coupons_query_args',
			array(
				'orderby'       => 'orders_count',
				'order'         => 'desc',
				'after'         => $after,
				'before'        => $before,
				'per_page'      => $per_page,
				'extended_info' => true,
			)
		)
	)->data : array();

	$rows = array();
	foreach ( $coupons_data as $coupon ) {
		$url_query   = wp_parse_args(
			array(
				'filter'  => 'single_coupon',
				'coupons' => $coupon['coupon_id'],
			),
			$persisted_query
		);
		$coupon_url  = wc_admin_url( '/analytics/coupons', $url_query );
		$coupon_code = isset( $coupon['extended_info'] ) && isset( $coupon['extended_info']['code'] ) ? $coupon['extended_info']['code'] : '';
		$rows[]      = array(
			array(
				'display' => "<a href='{$coupon_url}'>{$coupon_code}</a>",
				'value'   => $coupon_code,
			),
			array(
				'display' => wc_admin_number_format( $coupon['orders_count'] ),
				'value'   => $coupon['orders_count'],
				'format'  => 'number',
			),
			array(
				'display' => wc_price( $coupon['amount'] ),
				'value'   => $coupon['amount'],
				'format'  => 'currency',
			),
		);
	}

	return array(
		'id'      => 'coupons',
		'label'   => __( 'Top Coupons - Number of Orders', 'woocommerce' ),
		'headers' => array(
			array(
				'label' => __( 'Coupon code', 'woocommerce' ),
			),
			array(
				'label' => __( 'Orders', 'woocommerce' ),
			),
			array(
				'label' => __( 'Amount discounted', 'woocommerce' ),
			),
		),
		'rows'    => $rows,
	);
}