WC_Admin_Dashboard::sales_sparklineprivateWC 1.0

Gets the sales performance data from the new WooAdmin store.

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

Хуков нет.

Возвращает

stdClass|WP_Error|WP_REST_Response.

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

// private - только в коде основоного (родительского) класса
$result = $this->get_wc_admin_performance_data();

Код WC_Admin_Dashboard::sales_sparkline() WC 9.9.3

private function get_wc_admin_performance_data() {
	$request    = new \WP_REST_Request( 'GET', '/wc-analytics/reports/performance-indicators' );
	$start_date = gmdate( 'Y-m-01 00:00:00', current_time( 'timestamp' ) );
	$end_date   = gmdate( 'Y-m-d 23:59:59', current_time( 'timestamp' ) );
	$request->set_query_params(
		array(
			'before' => $end_date,
			'after'  => $start_date,
			'stats'  => 'revenue/total_sales,revenue/net_revenue,orders/orders_count,products/items_sold,variations/items_sold',
		)
	);
	$response = rest_do_request( $request );

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

	if ( 200 !== $response->get_status() ) {
		return new \WP_Error( 'woocommerce_analytics_performance_indicators_result_failed', __( 'Sorry, fetching performance indicators failed.', 'woocommerce' ) );
	}
	$report_keys      = array(
		'net_revenue' => 'net_sales',
	);
	$performance_data = new stdClass();
	foreach ( $response->get_data() as $indicator ) {
		if ( isset( $indicator['chart'] ) && isset( $indicator['value'] ) ) {
			$key                    = isset( $report_keys[ $indicator['chart'] ] ) ? $report_keys[ $indicator['chart'] ] : $indicator['chart'];
			$performance_data->$key = $indicator['value'];
		}
	}
	return $performance_data;
}

/**
 * Prepares the data for a sparkline to show sales in the last X days.
 *
 * @param  int    $id ID of the product to show. Blank to get all orders.
 * @param  int    $days Days of stats to get. Default to 7 days.
 * @param  string $type Type of sparkline to get. Ignored if ID is not set.
 * @return array
 */
private function get_sales_sparkline( $id = '', $days = 7, $type = 'sales' ) {
	$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,