Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <?php
  2. include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
  3. add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
  4. function my_custom_woocommerce_admin_reports( $reports ) {
  5. $sales_by_country = array(
  6. 'sales_by_country' => array(
  7. 'title' => 'Sales By Country',
  8. 'description' => '',
  9. 'hide_title' => true,
  10. 'callback' => 'sales_by_country_callback',
  11. ),
  12. );
  13. // This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page
  14. $reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $sales_by_country);
  15. return $reports;
  16. }
  17.  
  18. function sales_by_country_callback() {
  19. $report = new WC_Report_Sales_By_Country();
  20. $report->output_report();
  21. }
  22.  
  23. class WC_Report_Sales_By_Country extends WC_Admin_Report {
  24.  
  25. /**
  26. * Output the report.
  27. */
  28. public function output_report() {
  29. $ranges = array(
  30. 'year' => __( 'Year', 'woocommerce' ),
  31. 'last_month' => __( 'Last month', 'woocommerce' ),
  32. 'month' => __( 'This month', 'woocommerce' ),
  33. );
  34. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';
  35. if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
  36. $current_range = 'month';
  37. }
  38. $this->check_current_range_nonce( $current_range );
  39. $this->calculate_current_range( $current_range );
  40. $hide_sidebar = true;
  41. include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
  42. }
  43.  
  44. /**
  45. * Get the main chart.
  46. */
  47. public function get_main_chart() {
  48. global $wpdb;
  49.  
  50. $query_data = array(
  51. 'ID' => array(
  52. 'type' => 'post_data',
  53. 'function' => 'COUNT',
  54. 'name' => 'total_orders',
  55. 'distinct' => true,
  56. ),
  57. '_billing_country' => array(
  58. 'type' => 'meta',
  59. 'function' => '',
  60. 'name' => 'country'
  61. ),
  62. '_order_total' => array(
  63. 'type' => 'meta',
  64. 'function' => 'SUM',
  65. 'name' => 'order_total'
  66. ),
  67. );
  68.  
  69. $sales_by_country_orders = $this->get_order_report_data( array(
  70. 'data' => $query_data,
  71. 'query_type' => 'get_results',
  72. 'group_by' => 'country',
  73. 'filter_range' => true,
  74. 'order_types' => wc_get_order_types( 'sales-reports' ),
  75. 'order_status' => array( 'completed' ),
  76. 'parent_order_status' => false,
  77. ) );
  78. ?>
  79. <table class="widefat">
  80. <thead>
  81. <tr>
  82. <th><strong>Country</strong></th>
  83. <th><strong>Number Of Orders</strong></th>
  84. <th><strong>Sales</strong></th>
  85. </tr>
  86. </thead>
  87. <tbody>
  88. <?php foreach( $sales_by_country_orders as $order ) {
  89. ?>
  90. <tr>
  91. <td><?php echo WC()->countries->countries[$order->country]; ?></td>
  92. <td><?php echo $order->total_orders; ?></td>
  93. <td><?php echo wc_price($order->order_total); ?></td>
  94. </tr>
  95. <?php } ?>
  96. </tbody>
  97. </table>
  98. <?php
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement