WCPBC_Admin_Dashboard

WCPBC_Admin_Dashboard Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wcpbc-admin-dashboard.php  
  1. class WCPBC_Admin_Dashboard { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. protected $query = NULL; 
  7.  
  8. /** 
  9. * @var array 
  10. */ 
  11. protected $active_currency = NULL; 
  12.  
  13. /** 
  14. * Constructor 
  15. */ 
  16. public function __construct() { 
  17. add_filter( 'woocommerce_dashboard_status_widget_sales_query', array( $this, 'default_currency_sales_query' ) );  
  18. add_filter( 'wc_price_args', array( $this, 'add_currency_to_wc_price_args') ); 
  19. add_filter( 'woocommerce_reports_get_order_report_query', array( $this, 'order_report_query' ) ); 
  20. add_action( 'woocommerce_after_dashboard_status_widget', array( $this, 'dashboard_status_widget' ) ); 
  21.  
  22. /** 
  23. * Return sales month by currency 
  24. * @param string $currency 
  25. * @return number 
  26. */ 
  27. protected function get_sales_month( $currency ) { 
  28. global $wpdb; 
  29.  
  30. $sales = 0; 
  31.  
  32. if ( ! is_null($this->query) ) { 
  33. $query = $this->query; 
  34. $query['where'] .= "AND meta__order_currency.meta_value='{$currency}' "; 
  35.  
  36. $sales = $wpdb->get_var( implode( ' ', $query ) ); 
  37.  
  38. return $sales; 
  39.  
  40. /** 
  41. * If exists a active_currency add this currency to wc_price args 
  42. * @return array 
  43. */ 
  44. public function add_currency_to_wc_price_args( $wc_price_args ) {  
  45. if ( ! is_null( $this->active_currency ) ) { 
  46. $wc_price_args['currency'] = $this->active_currency; 
  47.  
  48. return $wc_price_args; 
  49.  
  50. /** 
  51. * Add to query default currency filter 
  52. * @param array $query 
  53. * @return array 
  54. */  
  55. public function default_currency_sales_query( $query ) { 
  56. global $wpdb;  
  57. //Add post_meta order currency  
  58. $query['join'] .= " INNER JOIN {$wpdb->postmeta} AS meta__order_currency ON posts.ID = meta__order_currency.post_id AND meta__order_currency.meta_key='_order_currency'"; 
  59.  
  60. //save the currency to use in action 
  61. $this->query = $query; 
  62.  
  63. //Add default currency filter 
  64. $default_currency = wcpbc_get_base_currency(); 
  65. $query['where'] .= "AND meta__order_currency.meta_value='{$default_currency}' "; 
  66.  
  67. return $query; 
  68.  
  69. /** 
  70. * Add to query active currency filter 
  71. * @param array $query 
  72. * @return array 
  73. */  
  74. public function order_report_query( $query ) { 
  75. global $wpdb;  
  76.  
  77. $currency = is_null( $this->active_currency ) ? wcpbc_get_base_currency() : $this->active_currency; 
  78.  
  79. $query['join'] .= " INNER JOIN {$wpdb->postmeta} AS meta__order_currency ON posts.ID = meta__order_currency.post_id AND meta__order_currency.meta_key='_order_currency'";  
  80. $query['where'] .= "AND meta__order_currency.meta_value='{$currency}' "; 
  81.  
  82. return $query; 
  83.  
  84. /** 
  85. * Show sales this month per installed currency 
  86. * @param object $reports 
  87. */ 
  88. public function dashboard_status_widget( $reports ) { 
  89.  
  90. foreach ( wcpbc_get_installed_currencies() as $currency ) { 
  91.  
  92. $sales = $this->get_sales_month( $currency ); 
  93.  
  94. $this->active_currency = $currency; 
  95.  
  96. ?> 
  97. <li class="sales-this-month"> 
  98. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=orders&range=month' ); ?>"> 
  99. <?php echo $reports->sales_sparkline( '', max( 7, date( 'd', current_time( 'timestamp' ) ) ) ); ?> 
  100. <?php printf( __( "<strong>%s</strong> sales this month", 'woocommerce' ), wc_price( $sales, array( 'currency' => $currency ) ) ); ?> 
  101. </a> 
  102. </li> 
  103. <?php 
  104.  
  105. $this->active_currency = NULL;