Created
June 11, 2018 13:06
-
-
Save tommyshellberg/b30b65c30a9f2c409ec0feddf85dca17 to your computer and use it in GitHub Desktop.
WooCommerce - display the last 30 days of sales using shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Monthly Sales Custom Shortcode | |
*/ | |
add_shortcode( 'display_monthly_sales', 'print_monthly_sales_frontend' ); | |
function print_monthly_sales_frontend() { | |
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'); | |
$last_month = (array) $wc_report->get_order_report_data( | |
array( | |
'data' => array( | |
'_order_total' => array( | |
'type' => 'meta', | |
'function' => 'SUM', | |
'name' => 'total_sales', | |
), | |
), | |
'group_by' => $wc_report->group_by_query, | |
'order_by' => 'post_date ASC', | |
'query_type' => 'get_results', | |
'filter_range' => 'month', | |
'order_types' => wc_get_order_types( 'sales-reports' ), | |
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), | |
) | |
); | |
?> | |
<div class="total-sales"> | |
<h1><?php echo get_woocommerce_currency_symbol() . intval($last_month[0]->total_sales); ?></h1> | |
<p>Last 30 days in sales</p> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment