WC_Admin_Dashboard::sales_sparkline()privateWC 1.0

Overwrites the original sparkline to use the new reports data if WooAdmin is enabled. Prepares a sparkline to show sales in the last X days.

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

Хуков нет.

Возвращает

Строку.

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

// private - только в коде основоного (родительского) класса
$result = $this->sales_sparkline( $reports, $is_wc_admin_disabled, $id, $type );
$reports(WC_Admin_Report) (обязательный)
old class for getting reports.
$is_wc_admin_disabled(true|false)
If WC Admin is disabled or not.
По умолчанию: false
$id(int)
ID of the product to show. Blank to get all orders.
По умолчанию: ''
$type(строка)
Type of sparkline to get. Ignored if ID is not set.
По умолчанию: 'sales'

Код WC_Admin_Dashboard::sales_sparkline() WC 8.7.0

private function sales_sparkline( $reports, $is_wc_admin_disabled = false, $id = '', $type = 'sales' ) {
	$days = max( 7, gmdate( 'd', current_time( 'timestamp' ) ) );
	if ( $is_wc_admin_disabled ) {
		return $reports->sales_sparkline( $id, $days, $type );
	}
	$sales_endpoint = '/wc-analytics/reports/revenue/stats';
	$start_date     = gmdate( 'Y-m-d 00:00:00', current_time( 'timestamp' ) - ( ( $days - 1 ) * DAY_IN_SECONDS ) );
	$end_date       = gmdate( 'Y-m-d 23:59:59', current_time( 'timestamp' ) );
	$meta_key       = 'net_revenue';
	$params         = array(
		'order'    => 'asc',
		'interval' => 'day',
		'per_page' => 100,
		'before'   => $end_date,
		'after'    => $start_date,
	);
	if ( $id ) {
		$sales_endpoint     = '/wc-analytics/reports/products/stats';
		$meta_key           = ( 'sales' === $type ) ? 'net_revenue' : 'items_sold';
		$params['products'] = $id;
	}
	$request          = new \WP_REST_Request( 'GET', $sales_endpoint );
	$params['fields'] = array( $meta_key );
	$request->set_query_params( $params );

	$response = rest_do_request( $request );

	if ( is_wp_error( $response ) ) {
		return $response;
	}

	$resp_data = $response->get_data();
	$data      = $resp_data['intervals'];

	$sparkline_data = array();
	$total          = 0;
	foreach ( $data as $d ) {
		$total += $d['subtotals']->$meta_key;
		array_push( $sparkline_data, array( strval( strtotime( $d['interval'] ) * 1000 ), $d['subtotals']->$meta_key ) );
	}

	if ( 'sales' === $type ) {
		/* translators: 1: total income 2: days */
		$tooltip = sprintf( __( 'Sold %1$s worth in the last %2$d days', 'woocommerce' ), strip_tags( wc_price( $total ) ), $days );
	} else {
		/* translators: 1: total items sold 2: days */
		$tooltip = sprintf( _n( 'Sold %1$d item in the last %2$d days', 'Sold %1$d items in the last %2$d days', $total, 'woocommerce' ), $total, $days );
	}

	return '<span class="wc_sparkline ' . ( ( 'sales' === $type ) ? 'lines' : 'bars' ) . ' tips" data-color="#777" data-tip="' . esc_attr( $tooltip ) . '" data-barwidth="' . 60 * 60 * 16 * 1000 . '" data-sparkline="' . wc_esc_json( wp_json_encode( $sparkline_data ) ) . '"></span>';
}