I am creating a report of sales per product for the current month which is displaying correctly.
I would like to also display the last 3 months sales too though. Here is my current code:
global $woocommerce, $wpdb, $product;
include_once($woocommerce->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php');
// WooCommerce Admin Report
$wc_report = new WC_Admin_Report();
// Set date parameters for the current month
$start_date = strtotime(date('Y-m', current_time('timestamp')) . '-01 midnight');
$end_date = strtotime('+1month', $start_date) - 86400;
$wc_report->start_date = $start_date;
$wc_report->end_date = $end_date;
// Avoid max join size error
$wpdb->query('SET SQL_BIG_SELECTS=1');
// Get data for current month sold products
$sold_products = $wc_report->get_order_report_data(array(
'data' => array(
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'quantity'
)
),
'query_type' => 'get_results',
'group_by' => 'product_id',
'where_meta' => '',
'order_by' => 'quantity DESC',
'order_types' => wc_get_order_types('order_count'),
'filter_range' => TRUE,
'order_status' => array('completed','processing','on-hold'),
));
and displaying the data:
<div>
<table>
<thead>
<tr>
<th><strong><?php _e('Name', 'text-domain') ?></strong></th>
<th><strong><?php _e('Current Month', 'text-domain') ?></strong></th>
<th><strong><?php _e('Last Month', 'text-domain') ?></strong></th>
<th><strong><?php _e('2 Months ago', 'text-domain') ?></strong></th>
<th><strong><?php _e('3 Months ago', 'text-domain') ?></strong></th>
</tr>
</thead>
<tbody >
<?php foreach ($sold_products as $product) { ?>
<tr>
<td><a href="<?php echo esc_url(get_edit_post_link(intval($product->product_id))); ?>"><strong><?php echo html_entity_decode(get_the_title($product->product_id)); ?></strong></a></td>
<td><?php echo intval($product->quantity); ?></td>
<td>...last months sales...</td>
<td>...2 months ago sales...</td>
<td>...3 months ago sales...</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
But how do I query different dates seeing as the start and end date are defined before the loop?