/** * Get sales report data. * @return object */ private function get_sales_report_data() { include_once dirname(__FILE__) . '/reports/class-wc-report-sales-by-date.php'; $sales_by_date = new WC_Report_Sales_By_Date(); $sales_by_date->start_date = strtotime(date('Y-m-01', current_time('timestamp'))); $sales_by_date->end_date = current_time('timestamp'); $sales_by_date->chart_groupby = 'day'; $sales_by_date->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)'; return $sales_by_date->get_report_data(); }
/** * Setup the report object and parse any date filtering * * @since 2.5.0 * @param array $assoc_args Arguments provided in when invoking the command * @return WC_Report_Sales_By_Date */ private function get_reporter($assoc_args) { include_once WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php'; include_once WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php'; $report = new WC_Report_Sales_By_Date(); if (empty($assoc_args['period'])) { // custom date range $assoc_args['period'] = 'custom'; if (!empty($assoc_args['date_min']) || !empty($assoc_args['date_max'])) { // overwrite _GET to make use of WC_Admin_Report::calculate_current_range() for custom date ranges $_GET['start_date'] = $this->parse_datetime($assoc_args['date_min']); $_GET['end_date'] = isset($assoc_args['date_max']) ? $this->parse_datetime($assoc_args['date_max']) : null; } else { // default custom range to today $_GET['start_date'] = $_GET['end_date'] = date('Y-m-d', current_time('timestamp')); } } else { // ensure period is valid if (!in_array($assoc_args['period'], array('week', 'month', 'last_month', 'year'))) { $assoc_args['period'] = 'week'; } // TODO: change WC_Admin_Report class to use "week" instead, as it's more consistent with other periods // allow "week" for period instead of "7day" if ('week' === $assoc_args['period']) { $assoc_args['period'] = '7day'; } } $report->calculate_current_range($assoc_args['period']); return $report; }
/** * Show status widget. */ public function status_widget() { global $wpdb; include_once dirname(__FILE__) . '/reports/class-wc-admin-report.php'; include_once dirname(__FILE__) . '/reports/class-wc-report-sales-by-date.php'; $reports = new WC_Admin_Report(); $sales_by_date = new WC_Report_Sales_By_Date(); $sales_by_date->start_date = strtotime(date('Y-m-01', current_time('timestamp'))); $sales_by_date->end_date = current_time('timestamp'); $sales_by_date->chart_groupby = 'day'; $sales_by_date->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)'; $report_data = $sales_by_date->get_report_data(); // Get top seller $query = array(); $query['fields'] = "SELECT SUM( order_item_meta.meta_value ) as qty, order_item_meta_2.meta_value as product_id\n\t\t\tFROM {$wpdb->posts} as posts"; $query['join'] = "INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id "; $query['join'] .= "INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id "; $query['join'] .= "INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id "; $query['where'] = "WHERE posts.post_type IN ( '" . implode("','", wc_get_order_types('order-count')) . "' ) "; $query['where'] .= "AND posts.post_status IN ( 'wc-" . implode("','wc-", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "' ) "; $query['where'] .= "AND order_item_meta.meta_key = '_qty' "; $query['where'] .= "AND order_item_meta_2.meta_key = '_product_id' "; $query['where'] .= "AND posts.post_date >= '" . date('Y-m-01', current_time('timestamp')) . "' "; $query['where'] .= "AND posts.post_date <= '" . date('Y-m-d H:i:s', current_time('timestamp')) . "' "; $query['groupby'] = "GROUP BY product_id"; $query['orderby'] = "ORDER BY qty DESC"; $query['limits'] = "LIMIT 1"; $top_seller = $wpdb->get_row(implode(' ', apply_filters('woocommerce_dashboard_status_widget_top_seller_query', $query))); // Counts $on_hold_count = 0; $processing_count = 0; foreach (wc_get_order_types('order-count') as $type) { $counts = (array) wp_count_posts($type); $on_hold_count += isset($counts['wc-on-hold']) ? $counts['wc-on-hold'] : 0; $processing_count += isset($counts['wc-processing']) ? $counts['wc-processing'] : 0; } // Get products using a query - this is too advanced for get_posts :( $stock = absint(max(get_option('woocommerce_notify_low_stock_amount'), 1)); $nostock = absint(max(get_option('woocommerce_notify_no_stock_amount'), 0)); $transient_name = 'wc_low_stock_count'; if (false === ($lowinstock_count = get_transient($transient_name))) { $query_from = apply_filters('woocommerce_report_low_in_stock_query_from', "FROM {$wpdb->posts} as posts\n\t\t\t\tINNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id\n\t\t\t\tINNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id\n\t\t\t\tWHERE 1=1\n\t\t\t\tAND posts.post_type IN ( 'product', 'product_variation' )\n\t\t\t\tAND posts.post_status = 'publish'\n\t\t\t\tAND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes'\n\t\t\t\tAND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}'\n\t\t\t\tAND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}'\n\t\t\t"); $lowinstock_count = absint($wpdb->get_var("SELECT COUNT( DISTINCT posts.ID ) {$query_from};")); set_transient($transient_name, $lowinstock_count, DAY_IN_SECONDS * 30); } $transient_name = 'wc_outofstock_count'; if (false === ($outofstock_count = get_transient($transient_name))) { $query_from = apply_filters('woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts\n\t\t\t\tINNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id\n\t\t\t\tINNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id\n\t\t\t\tWHERE 1=1\n\t\t\t\tAND posts.post_type IN ( 'product', 'product_variation' )\n\t\t\t\tAND posts.post_status = 'publish'\n\t\t\t\tAND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes'\n\t\t\t\tAND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$nostock}'\n\t\t\t"); $outofstock_count = absint($wpdb->get_var("SELECT COUNT( DISTINCT posts.ID ) {$query_from};")); set_transient($transient_name, $outofstock_count, DAY_IN_SECONDS * 30); } ?> <ul class="wc_status_list"> <li class="sales-this-month"> <a href="<?php echo admin_url('admin.php?page=wc-reports&tab=orders&range=month'); ?> "> <?php echo $reports->sales_sparkline('', max(7, date('d', current_time('timestamp')))); ?> <?php printf(__("<strong>%s</strong> net sales this month", 'woocommerce'), wc_price($report_data->net_sales)); ?> </a> </li> <?php if ($top_seller && $top_seller->qty) { ?> <li class="best-seller-this-month"> <a href="<?php echo admin_url('admin.php?page=wc-reports&tab=orders&report=sales_by_product&range=month&product_ids=' . $top_seller->product_id); ?> "> <?php echo $reports->sales_sparkline($top_seller->product_id, max(7, date('d', current_time('timestamp'))), 'count'); ?> <?php printf(__("%s top seller this month (sold %d)", 'woocommerce'), "<strong>" . get_the_title($top_seller->product_id) . "</strong>", $top_seller->qty); ?> </a> </li> <?php } ?> <li class="processing-orders"> <a href="<?php echo admin_url('edit.php?post_status=wc-processing&post_type=shop_order'); ?> "> <?php printf(_n("<strong>%s order</strong> awaiting processing", "<strong>%s orders</strong> awaiting processing", $processing_count, 'woocommerce'), $processing_count); ?> </a> </li> <li class="on-hold-orders"> <a href="<?php echo admin_url('edit.php?post_status=wc-on-hold&post_type=shop_order'); ?> "> <?php printf(_n("<strong>%s order</strong> on-hold", "<strong>%s orders</strong> on-hold", $on_hold_count, 'woocommerce'), $on_hold_count); ?> </a> </li> <li class="low-in-stock"> <a href="<?php echo admin_url('admin.php?page=wc-reports&tab=stock&report=low_in_stock'); ?> "> <?php printf(_n("<strong>%s product</strong> low in stock", "<strong>%s products</strong> low in stock", $lowinstock_count, 'woocommerce'), $lowinstock_count); ?> </a> </li> <li class="out-of-stock"> <a href="<?php echo admin_url('admin.php?page=wc-reports&tab=stock&report=out_of_stock'); ?> "> <?php printf(_n("<strong>%s product</strong> out of stock", "<strong>%s products</strong> out of stock", $outofstock_count, 'woocommerce'), $outofstock_count); ?> </a> </li> <?php do_action('woocommerce_after_dashboard_status_widget', $reports); ?> </ul> <?php }