Last active
May 5, 2021 16:38
-
-
Save Musilda/b4b9fb4b9330a6f667071d3c12282214 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php'); | |
add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 ); | |
function my_custom_woocommerce_admin_reports( $reports ) { | |
$sales_by_country = array( | |
'sales_by_country' => array( | |
'title' => 'Sales By Country', | |
'description' => '', | |
'hide_title' => true, | |
'callback' => 'sales_by_country_callback', | |
), | |
); | |
// This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page | |
$reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $sales_by_country); | |
return $reports; | |
} | |
function sales_by_country_callback() { | |
$report = new WC_Report_Sales_By_Country(); | |
$report->output_report(); | |
} | |
class WC_Report_Sales_By_Country extends WC_Admin_Report { | |
/** | |
* Output the report. | |
*/ | |
public function output_report() { | |
$ranges = array( | |
'year' => __( 'Year', 'woocommerce' ), | |
'last_month' => __( 'Last month', 'woocommerce' ), | |
'month' => __( 'This month', 'woocommerce' ), | |
); | |
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month'; | |
if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) { | |
$current_range = 'month'; | |
} | |
$this->check_current_range_nonce( $current_range ); | |
$this->calculate_current_range( $current_range ); | |
$hide_sidebar = true; | |
include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' ); | |
} | |
/** | |
* Get the main chart. | |
*/ | |
public function get_main_chart() { | |
global $wpdb; | |
$query_data = array( | |
'ID' => array( | |
'type' => 'post_data', | |
'function' => 'COUNT', | |
'name' => 'total_orders', | |
'distinct' => true, | |
), | |
'_billing_country' => array( | |
'type' => 'meta', | |
'function' => '', | |
'name' => 'country' | |
), | |
'_order_total' => array( | |
'type' => 'meta', | |
'function' => 'SUM', | |
'name' => 'order_total' | |
), | |
); | |
$sales_by_country_orders = $this->get_order_report_data( array( | |
'data' => $query_data, | |
'query_type' => 'get_results', | |
'group_by' => 'country', | |
'filter_range' => true, | |
'order_types' => wc_get_order_types( 'sales-reports' ), | |
'order_status' => array( 'completed' ), | |
'parent_order_status' => false, | |
) ); | |
?> | |
<table class="widefat"> | |
<thead> | |
<tr> | |
<th><strong>Country</strong></th> | |
<th><strong>Number Of Orders</strong></th> | |
<th><strong>Sales</strong></th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach( $sales_by_country_orders as $order ) { | |
?> | |
<tr> | |
<td><?php echo WC()->countries->countries[$order->country]; ?></td> | |
<td><?php echo $order->total_orders; ?></td> | |
<td><?php echo wc_price($order->order_total); ?></td> | |
</tr> | |
<?php } ?> | |
</tbody> | |
</table> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment