WC_Admin_Report::get_sales_sparkline()
Prepares the data for a sparkline to show sales in the last X days.
Method of the class: WC_Admin_Report{}
No Hooks.
Return
Array
.
Usage
$WC_Admin_Report = new WC_Admin_Report(); $WC_Admin_Report->get_sales_sparkline( $id, $days, $type );
- $id(int)
- ID of the product to show. Blank to get all orders.
Default: '' - $days(int)
- Days of stats to get.
Default: 7 days - $type(string)
- Type of sparkline to get. Ignored if ID is not set.
Default: 'sales'
WC_Admin_Report::get_sales_sparkline() WC Admin Report::get sales sparkline code WC 9.5.1
public function get_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; } $sparkline_data = array_values( $this->prepare_chart_data( $data, 'post_date', 'sparkline_value', $days - 1, strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ), 'day' ) ); // phpcs:enable WordPress.DateTime.RestrictedFunctions.date_date, WordPress.DateTime.CurrentTimeTimestamp.Requested return array( 'total' => $total, 'data' => $sparkline_data, ); }