/** * woocommerce_category_sales function. * * @access public * @return void */ function woocommerce_category_sales() { global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale; $first_year = $wpdb->get_var("SELECT post_date FROM {$wpdb->posts} WHERE post_date != 0 ORDER BY post_date ASC LIMIT 1;"); $first_year = $first_year ? date('Y', strtotime($first_year)) : date('Y'); $current_year = isset($_POST['show_year']) ? $_POST['show_year'] : date('Y', current_time('timestamp')); $categories = get_terms('product_cat', array('orderby' => 'name')); ?> <form method="post" action="" class="report_filters"> <p> <label for="show_year"><?php _e('Show:', 'woocommerce'); ?> </label> <select name="show_year" id="show_year"> <?php for ($i = $first_year; $i <= date('Y'); $i++) { printf('<option value="%s" %s>%s</option>', $i, selected($current_year, $i, false), $i); } ?> </select> <select multiple="multiple" class="chosen_select" id="show_categories" name="show_categories[]" style="width: 300px;"> <?php $r = array(); $r['pad_counts'] = 1; $r['hierarchal'] = 1; $r['hide_empty'] = 1; $r['value'] = 'id'; $r['selected'] = isset($_POST['show_categories']) ? $_POST['show_categories'] : ''; include_once $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-dropdown-walker.php'; echo woocommerce_walk_category_dropdown_tree($categories, 0, $r); ?> </select> <input type="submit" class="button" value="<?php _e('Show', 'woocommerce'); ?> " /> </p> </form> <?php $item_sales = array(); // Get order items $order_items = apply_filters('woocommerce_reports_category_sales_order_items', $wpdb->get_results($wpdb->prepare("\n\t\tSELECT order_item_meta_2.meta_value as product_id, posts.post_date, SUM( order_item_meta.meta_value ) as line_total\n\t\tFROM {$wpdb->prefix}woocommerce_order_items as order_items\n\n\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id\n\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id\n\t\tLEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID\n\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\n\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\n\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\n\n\t\tWHERE \tposts.post_type \t= 'shop_order'\n\t\tAND \tposts.post_status \t= 'publish'\n\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\n\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\n\t\tAND\t\tdate_format(posts.post_date,'%%Y') = %s\n\t\tAND \torder_items.order_item_type = 'line_item'\n\t\tAND \torder_item_meta.meta_key = '_line_total'\n\t\tAND \torder_item_meta_2.meta_key = '_product_id'\n\t\tGROUP BY order_items.order_item_id\n\t\tORDER BY posts.post_date ASC\n\t", $current_year))); if ($order_items) { foreach ($order_items as $order_item) { $month = date('m', strtotime($order_item->post_date)) - 1; $item_sales[$month][$order_item->product_id] = isset($item_sales[$month][$order_item->product_id]) ? $item_sales[$month][$order_item->product_id] + $order_item->line_total : $order_item->line_total; } } if (!empty($_POST['show_categories']) && sizeof($_POST['show_categories']) > 0) { $show_categories = $include_categories = array_map('absint', $_POST['show_categories']); foreach ($show_categories as $cat) { $include_categories = array_merge($include_categories, get_term_children($cat, 'product_cat')); } $categories = get_terms('product_cat', array('include' => array_unique($include_categories))); ?> <div class="woocommerce-wide-reports-wrap"> <table class="widefat"> <thead> <tr> <th><?php _e('Category', 'woocommerce'); ?> </th> <?php $column_count = 0; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { continue; } $column_count++; ?> <th><?php echo date('F', strtotime('2012-' . ($count + 1) . '-01')); ?> </th> <?php } ?> <th><strong><?php _e('Total', 'woocommerce'); ?> </strong></th> </tr> </thead> <tbody><?php // While outputting, lets store them for the chart $chart_data = $month_totals = $category_totals = array(); $top_cat = $bottom_cat = $top_cat_name = $bottom_cat_name = ''; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { break; } else { $month_totals[$count] = 0; } } foreach ($categories as $category) { $cat_total = 0; $category_chart_data = $term_ids = array(); $term_ids = get_term_children($category->term_id, 'product_cat'); $term_ids[] = $category->term_id; $product_ids = get_objects_in_term($term_ids, 'product_cat'); if ($category->parent > 0) { $prepend = '— '; } else { $prepend = ''; } $category_sales_html = '<tr><th>' . $prepend . $category->name . '</th>'; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { continue; } if (!empty($item_sales[$count])) { $matches = array_intersect_key($item_sales[$count], array_flip($product_ids)); $total = array_sum($matches); $cat_total += $total; } else { $total = 0; } if (sizeof(array_intersect($include_categories, get_ancestors($category->term_id, 'product_cat'))) == 0) { $month_totals[$count] += $total; } $category_sales_html .= '<td>' . woocommerce_price($total) . '</td>'; $category_chart_data[] = array(strtotime(date('Ymd', strtotime('2012-' . ($count + 1) . '-01'))) . '000', $total); } if ($cat_total == 0) { continue; } $category_totals[] = $cat_total; $category_sales_html .= '<td><strong>' . woocommerce_price($cat_total) . '</strong></td>'; $category_sales_html .= '</tr>'; echo $category_sales_html; $chart_data[$category->name] = $category_chart_data; if ($cat_total > $top_cat) { $top_cat = $cat_total; $top_cat_name = $category->name; } if ($cat_total < $bottom_cat || $bottom_cat === '') { $bottom_cat = $cat_total; $bottom_cat_name = $category->name; } } sort($category_totals); echo '<tr><th><strong>' . __('Total', 'woocommerce') . '</strong></th>'; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { break; } else { echo '<td><strong>' . woocommerce_price($month_totals[$count]) . '</strong></td>'; } } echo '<td><strong>' . woocommerce_price(array_sum($month_totals)) . '</strong></td></tr>'; ?> </tbody> </table> </div> <div id="poststuff" class="woocommerce-reports-wrap"> <div class="woocommerce-reports-sidebar"> <div class="postbox"> <h3><span><?php _e('Top category', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php echo $top_cat_name . ' (' . woocommerce_price($top_cat) . ')'; ?> </p> </div> </div> <?php if (sizeof($category_totals) > 1) { ?> <div class="postbox"> <h3><span><?php _e('Worst category', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php echo $bottom_cat_name . ' (' . woocommerce_price($bottom_cat) . ')'; ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Category sales average', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if (sizeof($category_totals) > 0) { echo woocommerce_price(array_sum($category_totals) / sizeof($category_totals)); } else { echo __('N/A', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Category sales median', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if (sizeof($category_totals) == 0) { echo __('N/A', 'woocommerce'); } elseif (sizeof($category_totals) % 2) { echo woocommerce_price(($category_totals[floor(sizeof($category_totals) / 2)] + $category_totals[ceil(sizeof($category_totals) / 2)]) / 2); } else { echo woocommerce_price($category_totals[sizeof($category_totals) / 2]); } ?> </p> </div> </div> <?php } ?> </div> <div class="woocommerce-reports-main"> <div class="postbox"> <h3><span><?php _e('Monthly sales by category', 'woocommerce'); ?> </span></h3> <div class="inside chart"> <div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div> <div id="cart_legend"></div> </div> </div> </div> </div> <script type="text/javascript"> jQuery(function(){ <?php // Variables foreach ($chart_data as $name => $data) { $varname = 'cat_' . str_replace('-', '_', sanitize_title($name)) . '_data'; echo 'var ' . $varname . ' = jQuery.parseJSON( \'' . json_encode($data) . '\' );'; } ?> var placeholder = jQuery("#placeholder"); var plot = jQuery.plot(placeholder, [ <?php $labels = array(); foreach ($chart_data as $name => $data) { $labels[] = '{ label: "' . esc_js($name) . '", data: ' . 'cat_' . str_replace('-', '_', sanitize_title($name)) . '_data }'; } echo implode(',', $labels); ?> ], { legend: { container: jQuery('#cart_legend'), noColumns: 2 }, series: { lines: { show: true, fill: true }, points: { show: true, align: "left" } }, grid: { show: true, aboveData: false, color: '#aaa', backgroundColor: '#fff', borderWidth: 2, borderColor: '#aaa', clickable: false, hoverable: true }, xaxis: { mode: "time", timeformat: "%b", monthNames: <?php echo json_encode(array_values($wp_locale->month_abbrev)); ?> , tickLength: 1, minTickSize: [1, "month"] }, yaxes: [ { min: 0, tickDecimals: 2 } ] }); placeholder.resize(); <?php woocommerce_tooltip_js(); ?> }); </script> <?php } ?> <script type="text/javascript"> jQuery(function(){ jQuery("select.chosen_select").chosen(); }); </script> <?php }
/** * Output the monthly sales chart. * * @access public * @return void */ function woocommerce_monthly_sales() { global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale; $first_year = $wpdb->get_var("SELECT post_date FROM {$wpdb->posts} WHERE post_date != 0 ORDER BY post_date ASC LIMIT 1;"); if ($first_year) { $first_year = date('Y', strtotime($first_year)); } else { $first_year = date('Y'); } $current_year = isset($_POST['show_year']) ? $_POST['show_year'] : date('Y', current_time('timestamp')); $start_date = strtotime($current_year . '0101'); $total_sales = $total_orders = $order_items = 0; $order_counts = $order_amounts = array(); for ($count = 0; $count < 12; $count++) { $time = strtotime(date('Ym', strtotime('+ ' . $count . ' MONTH', $start_date)) . '01') . '000'; if ($time > current_time('timestamp') . '000') { continue; } $month = date('Ym', strtotime(date('Ym', strtotime('+ ' . $count . ' MONTH', $start_date)) . '01')); $months_orders = $wpdb->get_row("\r\n\t\t\tSELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts\r\n\r\n\t\t\tLEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id\r\n\t\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID\r\n\t\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\t\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\r\n\t\t\tWHERE \tmeta.meta_key \t\t= '_order_total'\r\n\t\t\tAND \tposts.post_type \t= 'shop_order'\r\n\t\t\tAND \tposts.post_status \t= 'publish'\r\n\t\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\r\n\t\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\r\n\t\t\tAND\t\t'{$month}' \t\t\t= date_format(posts.post_date,'%Y%m')\r\n\t\t"); $order_counts[$time] = (int) $months_orders->total_orders; $order_amounts[$time] = (double) $months_orders->total_sales; $total_orders += (int) $months_orders->total_orders; $total_sales += (double) $months_orders->total_sales; // Count order items $order_items_serialized = $wpdb->get_col("\r\n\t\t\tSELECT meta.meta_value AS items FROM {$wpdb->posts} AS posts\r\n\r\n\t\t\tLEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id\r\n\t\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID\r\n\t\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\t\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\r\n\t\t\tWHERE \tmeta.meta_key \t\t= '_order_items'\r\n\t\t\tAND \tposts.post_type \t= 'shop_order'\r\n\t\t\tAND \tposts.post_status \t= 'publish'\r\n\t\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\r\n\t\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\r\n\t\t\tAND\t\t'{$month}' \t\t\t= date_format(posts.post_date,'%Y%m')\r\n\t\t"); if ($order_items_serialized) { foreach ($order_items_serialized as $order_items_array) { $order_items_array = maybe_unserialize($order_items_array); if (is_array($order_items_array)) { foreach ($order_items_array as $item) { $order_items += (int) $item['qty']; } } } } } ?> <form method="post" action=""> <p><label for="show_year"><?php _e('Year:', 'woocommerce'); ?> </label> <select name="show_year" id="show_year"> <?php for ($i = $first_year; $i <= date('Y'); $i++) { printf('<option value="%s" %s>%s</option>', $i, selected($current_year, $i, false), $i); } ?> </select> <input type="submit" class="button" value="<?php _e('Show', 'woocommerce'); ?> " /></p> </form> <div id="poststuff" class="woocommerce-reports-wrap"> <div class="woocommerce-reports-sidebar"> <div class="postbox"> <h3><span><?php _e('Total sales for year', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_sales > 0) { echo woocommerce_price($total_sales); } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Total orders for year', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_orders > 0) { echo $total_orders . ' (' . $order_items . ' ' . __('items', 'woocommerce') . ')'; } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Average order total for year', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_orders > 0) { echo woocommerce_price($total_sales / $total_orders); } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Average order items for year', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_orders > 0) { echo number_format($order_items / $total_orders, 2); } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> </div> <div class="woocommerce-reports-main"> <div class="postbox"> <h3><span><?php _e('Monthly sales for year', 'woocommerce'); ?> </span></h3> <div class="inside chart"> <div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div> </div> </div> </div> </div> <?php $order_counts_array = array(); foreach ($order_counts as $key => $count) { $order_counts_array[] = array($key, $count); } $order_amounts_array = array(); foreach ($order_amounts as $key => $amount) { $order_amounts_array[] = array($key, $amount); } $order_data = array('order_counts' => $order_counts_array, 'order_amounts' => $order_amounts_array); $chart_data = json_encode($order_data); ?> <script type="text/javascript"> jQuery(function(){ var order_data = jQuery.parseJSON( '<?php echo $chart_data; ?> ' ); var d = order_data.order_counts; var d2 = order_data.order_amounts; var placeholder = jQuery("#placeholder"); var plot = jQuery.plot(placeholder, [ { label: "<?php echo esc_js(__('Number of sales', 'woocommerce')); ?> ", data: d }, { label: "<?php echo esc_js(__('Sales amount', 'woocommerce')); ?> ", data: d2, yaxis: 2 } ], { series: { lines: { show: true }, points: { show: true, align: "left" } }, grid: { show: true, aboveData: false, color: '#ccc', backgroundColor: '#fff', borderWidth: 2, borderColor: '#ccc', clickable: false, hoverable: true }, xaxis: { mode: "time", timeformat: "%b %y", monthNames: <?php echo json_encode(array_values($wp_locale->month_abbrev)); ?> , tickLength: 1, minTickSize: [1, "month"] }, yaxes: [ { min: 0, tickSize: 10, tickDecimals: 0 }, { position: "right", min: 0, tickDecimals: 2 } ], colors: ["#8a4b75", "#47a03e"] }); placeholder.resize(); <?php woocommerce_tooltip_js(); ?> }); </script> <?php }
function wcaf_report_monthly() { global $start_date, $end_date, $woocommerce, $wpdb; $first_year = $wpdb->get_var("SELECT post_date FROM {$wpdb->posts} ORDER BY post_date ASC LIMIT 1;"); if ($first_year) { $first_year = date('Y', strtotime($first_year)); } else { $first_year = date('Y'); } $current_year = isset($_POST['show_year']) ? $_POST['show_year'] : date('Y', current_time('timestamp')); $start_date = isset($_POST['start_date']) ? $_POST['start_date'] : ''; $end_date = isset($_POST['end_date']) ? $_POST['end_date'] : ''; if (!$start_date) { $start_date = $current_year . '0101'; } if (!$end_date) { $end_date = date('Ym', current_time('timestamp')) . '31'; } $start_date = strtotime($start_date); $end_date = strtotime($end_date); $amounts_deposited = 0; $num_deposits = 0; if (version_compare(WC_VERSION, '2.2.0', '<')) { $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'shop_order', 'suppress_filters' => 0, 'meta_query' => array(array('key' => '_funds_deposited', 'value' => '1')), 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'terms' => array('completed', 'processing', 'on-hold'), 'field' => 'slug', 'operator' => 'IN'))); } else { $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'shop_order', 'post_status' => array('wc-completed', 'wc-processing', 'wc-on-hold'), 'suppress_filters' => 0, 'meta_query' => array(array('key' => '_funds_deposited', 'value' => '1'))); } $orders = get_posts($args); $deposit_counts = array(); $deposit_amounts = array(); // Blank date ranges to begin $count = 0; $months = ($end_date - $start_date) / (60 * 60 * 24 * 7 * 4); while ($count < $months) { $time = strtotime(date('Ym', strtotime('+ ' . $count . ' MONTH', $start_date)) . '01') . '000'; $deposit_counts[$time] = 0; $deposit_amounts[$time] = 0; $count++; } if ($orders) { foreach ($orders as $order) { $order_obj = new WC_Order($order->ID); $time = strtotime(date('Ym', strtotime($order->post_date)) . '01') . '000'; $order_items_array = $order_obj->get_items(); foreach ($order_items_array as $item) { $item_id = isset($item['product_id']) ? $item['product_id'] : $item['id']; $product = X3M_AccountFunds::get_product($item_id); $is_deposit = get_post_meta($item_id, '_is_deposit', true); if ($is_deposit == 'yes') { $num_deposits++; $amounts_deposited += $product->get_price() * $item['qty']; if (isset($deposit_counts[$time])) { $deposit_counts[$time]++; } else { $deposit_counts[$time] = 1; } if (isset($deposit_amounts[$time])) { $deposit_amounts[$time] = $deposit_amounts[$time] + $product->get_price() * $item['qty']; } else { $deposit_amounts[$time] = $product->get_price() * $item['qty']; } } } } } ?> <form method="post" action=""> <p><label for="show_year"><?php _e('Year:', 'woocommerce'); ?> </label> <select name="show_year" id="show_year"> <?php for ($i = $first_year; $i <= date('Y'); $i++) { printf('<option value="%s" %s>%s</option>', $i, selected($current_year, $i, false), $i); } ?> </select> <input type="submit" class="button" value="<?php _e('Show', 'woocommerce'); ?> " /></p> </form> <div id="poststuff" class="woocommerce-reports-wrap"> <div class="woocommerce-reports-sidebar"> <div class="postbox"> <h3><span><?php _e('Total deposits for year', 'wc_account_funds'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($amounts_deposited > 0) { echo woocommerce_price($amounts_deposited); } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Number of deposits for year', 'wc_account_funds'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($num_deposits > 0) { echo $num_deposits; } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Average deposit for year', 'wc_account_funds'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($amounts_deposited > 0) { echo woocommerce_price($amounts_deposited / $num_deposits); } else { _e('n/a', 'woocommerce'); } ?> </p> </div> </div> </div> <div class="woocommerce-reports-main"> <div class="postbox"> <h3><span><?php _e('Monthly deposits for year', 'wc_account_funds'); ?> </span></h3> <div class="inside chart"> <div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div> </div> </div> </div> </div> <?php $deposit_counts_array = array(); foreach ($deposit_counts as $key => $count) { $deposit_counts_array[] = array($key, $count); } $deposit_amounts_array = array(); foreach ($deposit_amounts as $key => $amount) { $deposit_amounts_array[] = array($key, $amount); } $deposit_data = array('deposit_counts' => $deposit_counts_array, 'deposit_amounts' => $deposit_amounts_array); $chart_data = json_encode($deposit_data); ?> <script type="text/javascript"> jQuery(function(){ var deposit_data = jQuery.parseJSON( '<?php echo $chart_data; ?> ' ); var d = deposit_data.deposit_counts; var d2 = deposit_data.deposit_amounts; var placeholder = jQuery("#placeholder"); var plot = jQuery.plot(placeholder, [ { label: "Number of deposits", data: d }, { label: "Deposit amount", data: d2, yaxis: 2 } ], { series: { lines: { show: true }, points: { show: true, align: "left" } }, grid: { show: true, aboveData: false, color: '#ccc', backgroundColor: '#fff', borderWidth: 2, borderColor: '#ccc', clickable: false, hoverable: true }, xaxis: { mode: "time", timeformat: "%b %y", tickLength: 1, minTickSize: [1, "month"] }, yaxes: [ { min: 0, tickSize: 10, tickDecimals: 0 }, { position: "right", min: 0, tickDecimals: 2 } ], colors: ["#8a4b75", "#47a03e"] }); placeholder.resize(); <?php woocommerce_tooltip_js(); ?> }); </script> <?php }
/** * Monthly sales chart */ function woocommerce_monthly_sales() { global $start_date, $end_date, $woocommerce, $wpdb; $first_year = $wpdb->get_var("SELECT post_date FROM {$wpdb->posts} ORDER BY post_date ASC LIMIT 1;"); if ($first_year) { $first_year = date('Y', strtotime($first_year)); } else { $first_year = date('Y'); } $current_year = isset($_POST['show_year']) ? $_POST['show_year'] : date('Y', current_time('timestamp')); $start_date = isset($_POST['start_date']) ? $_POST['start_date'] : ''; $end_date = isset($_POST['end_date']) ? $_POST['end_date'] : ''; if (!$start_date) { $start_date = $current_year . '0101'; } if (!$end_date) { $end_date = date('Ym', current_time('timestamp')) . '31'; } $start_date = strtotime($start_date); $end_date = strtotime($end_date); $total_sales = 0; $total_orders = 0; $order_items = 0; // Get orders to display in widget add_filter('posts_where', 'orders_within_range'); $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'shop_order', 'post_status' => 'publish', 'suppress_filters' => 0, 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'terms' => array('completed', 'processing', 'on-hold'), 'field' => 'slug', 'operator' => 'IN'))); $orders = get_posts($args); $order_counts = array(); $order_amounts = array(); // Blank date ranges to begin $count = 0; $months = ($end_date - $start_date) / (60 * 60 * 24 * 7 * 4); while ($count < $months) { $time = strtotime(date('Ym', strtotime('+ ' . $count . ' MONTH', $start_date)) . '01') . '000'; $order_counts[$time] = 0; $order_amounts[$time] = 0; $count++; } if ($orders) { foreach ($orders as $order) { $order_total = get_post_meta($order->ID, '_order_total', true); $time = strtotime(date('Ym', strtotime($order->post_date)) . '01') . '000'; $order_items_array = (array) get_post_meta($order->ID, '_order_items', true); foreach ($order_items_array as $item) { $order_items += (int) $item['qty']; } $total_sales += $order_total; $total_orders++; if (isset($order_counts[$time])) { $order_counts[$time]++; } else { $order_counts[$time] = 1; } if (isset($order_amounts[$time])) { $order_amounts[$time] = $order_amounts[$time] + $order_total; } else { $order_amounts[$time] = (double) $order_total; } } } remove_filter('posts_where', 'orders_within_range'); ?> <form method="post" action=""> <p><label for="show_year"><?php _e('Year:', 'woothemes'); ?> </label> <select name="show_year" id="show_year"> <?php for ($i = $first_year; $i <= date('Y'); $i++) { printf('<option value="%u" %u>%u</option>', $i, selected($current_year, $i, false), $i); } ?> </select> <input type="submit" class="button" value="<?php _e('Show', 'woothemes'); ?> " /></p> </form> <div id="poststuff" class="woocommerce-reports-wrap"> <div class="woocommerce-reports-sidebar"> <div class="postbox"> <h3><span><?php _e('Total sales for year', 'woothemes'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_sales > 0) { echo woocommerce_price($total_sales); } else { _e('n/a', 'woothemes'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Total orders for year', 'woothemes'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_orders > 0) { echo $total_orders . ' (' . $order_items . __(' items', 'woothemes') . ')'; } else { _e('n/a', 'woothemes'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Average order total for year', 'woothemes'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_orders > 0) { echo woocommerce_price($total_sales / $total_orders); } else { _e('n/a', 'woothemes'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Average order items for year', 'woothemes'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if ($total_orders > 0) { echo number_format($order_items / $total_orders, 2); } else { _e('n/a', 'woothemes'); } ?> </p> </div> </div> </div> <div class="woocommerce-reports-main"> <div class="postbox"> <h3><span><?php _e('Monthly sales for year', 'woothemes'); ?> </span></h3> <div class="inside chart"> <div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div> </div> </div> </div> </div> <?php $order_counts_array = array(); foreach ($order_counts as $key => $count) { $order_counts_array[] = array($key, $count); } $order_amounts_array = array(); foreach ($order_amounts as $key => $amount) { $order_amounts_array[] = array($key, $amount); } $order_data = array('order_counts' => $order_counts_array, 'order_amounts' => $order_amounts_array); $chart_data = json_encode($order_data); ?> <script type="text/javascript"> jQuery(function(){ var order_data = jQuery.parseJSON( '<?php echo $chart_data; ?> ' ); var d = order_data.order_counts; var d2 = order_data.order_amounts; var placeholder = jQuery("#placeholder"); var plot = jQuery.plot(placeholder, [ { label: "Number of sales", data: d }, { label: "Sales amount", data: d2, yaxis: 2 } ], { series: { lines: { show: true }, points: { show: true, align: "left" } }, grid: { show: true, aboveData: false, color: '#ccc', backgroundColor: '#fff', borderWidth: 2, borderColor: '#ccc', clickable: false, hoverable: true }, xaxis: { mode: "time", timeformat: "%b %y", tickLength: 1, minTickSize: [1, "month"] }, yaxes: [ { min: 0, tickSize: 1, tickDecimals: 0 }, { position: "right", min: 0, tickDecimals: 2 } ], colors: ["#8a4b75", "#47a03e"] }); placeholder.resize(); <?php woocommerce_tooltip_js(); ?> }); </script> <?php }
/** * woocommerce_category_sales function. * * @access public * @return void */ function woocommerce_category_sales() { global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale; $first_year = $wpdb->get_var("SELECT post_date FROM {$wpdb->posts} WHERE post_date != 0 ORDER BY post_date ASC LIMIT 1;"); $first_year = $first_year ? date('Y', strtotime($first_year)) : date('Y'); $current_year = isset($_POST['show_year']) ? $_POST['show_year'] : date('Y', current_time('timestamp')); $start_date = strtotime($current_year . '0101'); ?> <form method="post" action=""> <p><label for="show_year"><?php _e('Year:', 'woocommerce'); ?> </label> <select name="show_year" id="show_year"> <?php for ($i = $first_year; $i <= date('Y'); $i++) { printf('<option value="%s" %s>%s</option>', $i, selected($current_year, $i, false), $i); } ?> </select> <input type="submit" class="button" value="<?php _e('Show', 'woocommerce'); ?> " /></p> </form> <?php $item_sales = array(); for ($count = 0; $count < 12; $count++) { $time = strtotime(date('Ym', strtotime('+ ' . $count . ' MONTH', $start_date)) . '01') . '000'; if ($time > current_time('timestamp') . '000') { continue; } $month = date('Ym', strtotime(date('Ym', strtotime('+ ' . $count . ' MONTH', $start_date)) . '01')); // Get order items $order_items_serialized = $wpdb->get_col("\n\t\t\tSELECT meta.meta_value AS items FROM {$wpdb->posts} AS posts\n\n\t\t\tLEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id\n\t\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID\n\t\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\n\t\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\n\n\t\t\tWHERE \tmeta.meta_key \t\t= '_order_items'\n\t\t\tAND \tposts.post_type \t= 'shop_order'\n\t\t\tAND \tposts.post_status \t= 'publish'\n\t\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\n\t\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\n\t\t\tAND\t\t'{$month}' \t\t\t= date_format(posts.post_date,'%Y%m')\n\t\t"); if ($order_items_serialized) { foreach ($order_items_serialized as $order_items_array) { $order_items_array = maybe_unserialize($order_items_array); if (is_array($order_items_array)) { foreach ($order_items_array as $item) { if (!isset($item_sales[$count][$item['id']])) { $item_sales[$count][$item['id']] = 0; } if (!empty($item['line_total'])) { $item_sales[$count][$item['id']] += $item['line_total']; } } } } } } $categories = get_terms('product_cat', array('parent' => 0)); ?> <table class="widefat"> <thead> <tr> <th><?php _e('Category', 'woocommerce'); ?> </th> <?php for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { continue; } ?> <th><?php echo date('F', strtotime('2012-' . ($count + 1) . '-01')); ?> </th> <?php } ?> <th><strong><?php _e('Total', 'woocommerce'); ?> </strong></th> </tr> </thead> <tbody><?php // While outputting, lets store them for the chart $chart_data = $month_totals = $category_totals = array(); $top_cat = $bottom_cat = $top_cat_name = $bottom_cat_name = ''; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { break; } else { $month_totals[$count] = 0; } } foreach ($categories as $category) { $cat_total = 0; $category_chart_data = $term_ids = array(); $term_ids = get_term_children($category->term_id, 'product_cat'); $term_ids[] = $category->term_id; $product_ids = get_objects_in_term($term_ids, 'product_cat'); $category_sales_html = '<tr><th>' . $category->name . '</th>'; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { continue; } if (!empty($item_sales[$count])) { $matches = array_intersect_key($item_sales[$count], array_flip($product_ids)); $total = array_sum($matches); $cat_total += $total; } else { $total = 0; } $month_totals[$count] += $total; $category_sales_html .= '<td>' . woocommerce_price($total) . '</td>'; $category_chart_data[] = array(strtotime(date('Ymd', strtotime('2012-' . ($count + 1) . '-01'))) . '000', $total); } if ($cat_total == 0) { continue; } $category_totals[] = $cat_total; $category_sales_html .= '<td><strong>' . woocommerce_price($cat_total) . '</strong></td>'; $category_sales_html .= '</tr>'; echo $category_sales_html; $chart_data[$category->name] = $category_chart_data; if ($cat_total > $top_cat) { $top_cat = $cat_total; $top_cat_name = $category->name; } if ($cat_total < $bottom_cat || $bottom_cat === '') { $bottom_cat = $cat_total; $bottom_cat_name = $category->name; } } sort($category_totals); echo '<tr><th><strong>' . __('Total', 'woocommerce') . '</strong></th>'; for ($count = 0; $count < 12; $count++) { if ($count >= date('m') && $current_year == date('Y')) { break; } else { echo '<td><strong>' . woocommerce_price($month_totals[$count]) . '</strong></td>'; } } echo '<td><strong>' . woocommerce_price(array_sum($month_totals)) . '</strong></td></tr>'; ?> </tbody> </table> <br/> <div id="poststuff" class="woocommerce-reports-wrap"> <div class="woocommerce-reports-sidebar"> <div class="postbox"> <h3><span><?php _e('Top category', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php echo $top_cat_name . ' (' . woocommerce_price($top_cat) . ')'; ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Bottom category', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php echo $bottom_cat_name . ' (' . woocommerce_price($bottom_cat) . ')'; ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Category sales average', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if (sizeof($category_totals) > 0) { echo woocommerce_price(array_sum($category_totals) / sizeof($category_totals)); } else { echo __('N/A', 'woocommerce'); } ?> </p> </div> </div> <div class="postbox"> <h3><span><?php _e('Category sales median', 'woocommerce'); ?> </span></h3> <div class="inside"> <p class="stat"><?php if (sizeof($category_totals) == 0) { echo __('N/A', 'woocommerce'); } elseif (sizeof($category_totals) % 2) { echo woocommerce_price(($category_totals[floor(sizeof($category_totals) / 2)] + $category_totals[ceil(sizeof($category_totals) / 2)]) / 2); } else { echo woocommerce_price($category_totals[sizeof($category_totals) / 2]); } ?> </p> </div> </div> </div> <div class="woocommerce-reports-main"> <div class="postbox"> <h3><span><?php _e('Monthly sales by category', 'woocommerce'); ?> </span></h3> <div class="inside chart"> <div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div> </div> </div> </div> </div> <script type="text/javascript"> jQuery(function(){ <?php // Variables foreach ($chart_data as $name => $data) { $varname = str_replace('-', '_', sanitize_title($name)) . '_data'; echo 'var ' . $varname . ' = jQuery.parseJSON( \'' . json_encode($data) . '\' );'; } ?> var placeholder = jQuery("#placeholder"); var plot = jQuery.plot(placeholder, [ <?php $labels = array(); foreach ($chart_data as $name => $data) { $labels[] = '{ label: "' . esc_js($name) . '", data: ' . str_replace('-', '_', sanitize_title($name)) . '_data }'; } echo implode(',', $labels); ?> ], { series: { lines: { show: true }, points: { show: true, align: "left" } }, grid: { show: true, aboveData: false, color: '#ccc', backgroundColor: '#fff', borderWidth: 2, borderColor: '#ccc', clickable: false, hoverable: true }, xaxis: { mode: "time", timeformat: "%b %y", monthNames: <?php echo json_encode(array_values($wp_locale->month_abbrev)); ?> , tickLength: 1, minTickSize: [1, "month"] }, yaxes: [ { min: 0, tickDecimals: 2 } ] }); placeholder.resize(); <?php woocommerce_tooltip_js(); ?> }); </script> <?php }