WC_Admin_Report::sales_sparkline()publicWC 1.0

Prepares a sparkline to show sales in the last X days.

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

Хуков нет.

Возвращает

Строку.

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

$WC_Admin_Report = new WC_Admin_Report();
$WC_Admin_Report->sales_sparkline( $id, $days, $type );
$id(int)
ID of the product to show. Blank to get all orders.
По умолчанию: ''
$days(int)
Days of stats to get.
По умолчанию: 7
$type(строка)
Type of sparkline to get. Ignored if ID is not set.
По умолчанию: 'sales'

Код WC_Admin_Report::sales_sparkline() WC 9.4.2

public function sales_sparkline( $id = '', $days = 7, $type = 'sales' ) {

	// phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date, WordPress.DateTime.CurrentTimeTimestamp.Requested

	if ( $id ) {
		$meta_key = ( 'sales' === $type ) ? '_line_total' : '_qty';

		$data = $this->get_order_report_data(
			array(
				'data'         => array(
					'_product_id' => array(
						'type'            => 'order_item_meta',
						'order_item_type' => 'line_item',
						'function'        => '',
						'name'            => 'product_id',
					),
					$meta_key     => array(
						'type'            => 'order_item_meta',
						'order_item_type' => 'line_item',
						'function'        => 'SUM',
						'name'            => 'sparkline_value',
					),
					'post_date'   => array(
						'type'     => 'post_data',
						'function' => '',
						'name'     => 'post_date',
					),
				),
				'where'        => array(
					array(
						'key'      => 'post_date',
						'value'    => date( 'Y-m-d', strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ) ),
						'operator' => '>',
					),
					array(
						'key'      => 'order_item_meta__product_id.meta_value',
						'value'    => $id,
						'operator' => '=',
					),
				),
				'group_by'     => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',
				'query_type'   => 'get_results',
				'filter_range' => false,
			)
		);
	} else {

		$data = $this->get_order_report_data(
			array(
				'data'         => array(
					'_order_total' => array(
						'type'     => 'meta',
						'function' => 'SUM',
						'name'     => 'sparkline_value',
					),
					'post_date'    => array(
						'type'     => 'post_data',
						'function' => '',
						'name'     => 'post_date',
					),
				),
				'where'        => array(
					array(
						'key'      => 'post_date',
						'value'    => date( 'Y-m-d', strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ) ),
						'operator' => '>',
					),
				),
				'group_by'     => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',
				'query_type'   => 'get_results',
				'filter_range' => false,
			)
		);
	}

	$total = 0;
	foreach ( $data as $d ) {
		$total += $d->sparkline_value;
	}

	if ( 'sales' === $type ) {
		/* translators: 1: total income 2: days */
		$tooltip = sprintf( __( 'Sold %1$s worth in the last %2$d days', 'woocommerce' ), wp_strip_all_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 );
	}

	$sparkline_data = array_values( $this->prepare_chart_data( $data, 'post_date', 'sparkline_value', $days - 1, strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ), 'day' ) );

	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>';

	// phpcs:enable WordPress.DateTime.RestrictedFunctions.date_date, WordPress.DateTime.CurrentTimeTimestamp.Requested
}