WC_Report_Customer_List

WC_Report_Customer_List.

Defined (1)

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

/includes/admin/reports/class-wc-report-customer-list.php  
  1. class WC_Report_Customer_List extends WP_List_Table { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7.  
  8. parent::__construct( array( 
  9. 'singular' => 'customer',  
  10. 'plural' => 'customers',  
  11. 'ajax' => false,  
  12. ) ); 
  13.  
  14. /** 
  15. * No items found text. 
  16. */ 
  17. public function no_items() { 
  18. _e( 'No customers found.', 'woocommerce' ); 
  19.  
  20. /** 
  21. * Output the report. 
  22. */ 
  23. public function output_report() { 
  24. $this->prepare_items(); 
  25.  
  26. echo '<div id="poststuff" class="woocommerce-reports-wide">'; 
  27.  
  28. if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) { 
  29. $linked = wc_update_new_customer_past_orders( absint( $_GET['link_orders'] ) ); 
  30.  
  31. echo '<div class="updated"><p>' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '</p></div>'; 
  32.  
  33. if ( ! empty( $_GET['refresh'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'refresh' ) ) { 
  34. $user_id = absint( $_GET['refresh'] ); 
  35. $user = get_user_by( 'id', $user_id ); 
  36.  
  37. delete_user_meta( $user_id, '_money_spent' ); 
  38. delete_user_meta( $user_id, '_order_count' ); 
  39.  
  40. echo '<div class="updated"><p>' . sprintf( __( 'Refreshed stats for %s', 'woocommerce' ), $user->display_name ) . '</p></div>'; 
  41.  
  42. echo '<form method="post" id="woocommerce_customers">'; 
  43.  
  44. $this->search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' ); 
  45. $this->display(); 
  46.  
  47. echo '</form>'; 
  48. echo '</div>'; 
  49.  
  50. /** 
  51. * Get column value. 
  52. * @param WP_User $user 
  53. * @param string $column_name 
  54. * @return string 
  55. */ 
  56. public function column_default( $user, $column_name ) { 
  57. global $wpdb; 
  58.  
  59. switch ( $column_name ) { 
  60.  
  61. case 'customer_name' : 
  62. if ( $user->last_name && $user->first_name ) { 
  63. return $user->last_name . ', ' . $user->first_name; 
  64. } else { 
  65. return '-'; 
  66.  
  67. case 'username' : 
  68. return $user->user_login; 
  69.  
  70. case 'location' : 
  71.  
  72. $state_code = get_user_meta( $user->ID, 'billing_state', true ); 
  73. $country_code = get_user_meta( $user->ID, 'billing_country', true ); 
  74.  
  75. $state = isset( WC()->countries->states[ $country_code ][ $state_code ] ) ? WC()->countries->states[ $country_code ][ $state_code ] : $state_code; 
  76. $country = isset( WC()->countries->countries[ $country_code ] ) ? WC()->countries->countries[ $country_code ] : $country_code; 
  77.  
  78. $value = ''; 
  79.  
  80. if ( $state ) { 
  81. $value .= $state . ', '; 
  82.  
  83. $value .= $country; 
  84.  
  85. if ( $value ) { 
  86. return $value; 
  87. } else { 
  88. return '-'; 
  89.  
  90. case 'email' : 
  91. return '<a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a>'; 
  92.  
  93. case 'spent' : 
  94. return wc_price( wc_get_customer_total_spent( $user->ID ) ); 
  95.  
  96. case 'orders' : 
  97. return wc_get_customer_order_count( $user->ID ); 
  98.  
  99. case 'last_order' : 
  100.  
  101. $orders = wc_get_orders( array( 
  102. 'limit' => 1,  
  103. 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),  
  104. 'customer' => $user->ID,  
  105. ) ); 
  106.  
  107. if ( ! empty( $orders ) ) { 
  108. $order = $orders[0]; 
  109. return '<a href="' . admin_url( 'post.php?post=' . $order->get_id() . '&action=edit' ) . '">' . _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() . '</a> – ' . wc_format_datetime( $order->get_date_created() ); 
  110. } else { 
  111. return '-'; 
  112.  
  113. break; 
  114.  
  115. case 'user_actions' : 
  116. ob_start(); 
  117. ?><p> 
  118. <?php 
  119. do_action( 'woocommerce_admin_user_actions_start', $user ); 
  120.  
  121. $actions = array(); 
  122.  
  123. $actions['refresh'] = array( 
  124. 'url' => wp_nonce_url( add_query_arg( 'refresh', $user->ID ), 'refresh' ),  
  125. 'name' => __( 'Refresh stats', 'woocommerce' ),  
  126. 'action' => "refresh",  
  127. ); 
  128.  
  129. $actions['edit'] = array( 
  130. 'url' => admin_url( 'user-edit.php?user_id=' . $user->ID ),  
  131. 'name' => __( 'Edit', 'woocommerce' ),  
  132. 'action' => "edit",  
  133. ); 
  134.  
  135. $actions['view'] = array( 
  136. 'url' => admin_url( 'edit.php?post_type=shop_order&_customer_user=' . $user->ID ),  
  137. 'name' => __( 'View orders', 'woocommerce' ),  
  138. 'action' => "view",  
  139. ); 
  140.  
  141. $orders = wc_get_orders( array( 
  142. 'limit' => 1,  
  143. 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),  
  144. 'customer' => array( array( 0, $user->user_email ) ),  
  145. ) ); 
  146.  
  147. if ( $orders ) { 
  148. $actions['link'] = array( 
  149. 'url' => wp_nonce_url( add_query_arg( 'link_orders', $user->ID ), 'link_orders' ),  
  150. 'name' => __( 'Link previous orders', 'woocommerce' ),  
  151. 'action' => "link",  
  152. ); 
  153.  
  154. $actions = apply_filters( 'woocommerce_admin_user_actions', $actions, $user ); 
  155.  
  156. foreach ( $actions as $action ) { 
  157. printf( '<a class="button tips %s" href="%s" data-tip="%s">%s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) ); 
  158.  
  159. do_action( 'woocommerce_admin_user_actions_end', $user ); 
  160. ?> 
  161. </p><?php 
  162. $user_actions = ob_get_contents(); 
  163. ob_end_clean(); 
  164.  
  165. return $user_actions; 
  166.  
  167. return ''; 
  168.  
  169. /** 
  170. * Get columns. 
  171. * @return array 
  172. */ 
  173. public function get_columns() { 
  174. $columns = array( 
  175. 'customer_name' => __( 'Name (Last, First)', 'woocommerce' ),  
  176. 'username' => __( 'Username', 'woocommerce' ),  
  177. 'email' => __( 'Email', 'woocommerce' ),  
  178. 'location' => __( 'Location', 'woocommerce' ),  
  179. 'orders' => __( 'Orders', 'woocommerce' ),  
  180. 'spent' => __( 'Money spent', 'woocommerce' ),  
  181. 'last_order' => __( 'Last order', 'woocommerce' ),  
  182. 'user_actions' => __( 'Actions', 'woocommerce' ),  
  183. ); 
  184.  
  185. return $columns; 
  186.  
  187. /** 
  188. * Order users by name. 
  189. * @param WP_User_Query $query 
  190. */ 
  191. public function order_by_last_name( $query ) { 
  192. global $wpdb; 
  193.  
  194. $s = ! empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : ''; 
  195.  
  196. $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta2 ON ({$wpdb->users}.ID = meta2.user_id) "; 
  197. $query->query_where .= " AND meta2.meta_key = 'last_name' "; 
  198. $query->query_orderby = " ORDER BY meta2.meta_value, user_login ASC "; 
  199.  
  200. if ( $s ) { 
  201. $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta3 ON ({$wpdb->users}.ID = meta3.user_id)"; 
  202. $query->query_where .= " AND ( user_login LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' OR user_nicename LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' OR meta3.meta_value LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' ) "; 
  203. $query->query_orderby = " GROUP BY ID " . $query->query_orderby; 
  204.  
  205. return $query; 
  206.  
  207. /** 
  208. * Prepare customer list items. 
  209. */ 
  210. public function prepare_items() { 
  211. global $wpdb; 
  212.  
  213. $current_page = absint( $this->get_pagenum() ); 
  214. $per_page = 20; 
  215.  
  216. /** 
  217. * Init column headers. 
  218. */ 
  219. $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); 
  220.  
  221. add_action( 'pre_user_query', array( $this, 'order_by_last_name' ) ); 
  222.  
  223. /** 
  224. * Get users. 
  225. */ 
  226. $admin_users = new WP_User_Query( 
  227. array( 
  228. 'role' => 'administrator1',  
  229. 'fields' => 'ID',  
  230. ); 
  231.  
  232. $manager_users = new WP_User_Query( 
  233. array( 
  234. 'role' => 'shop_manager',  
  235. 'fields' => 'ID',  
  236. ); 
  237.  
  238. $query = new WP_User_Query( array( 
  239. 'exclude' => array_merge( $admin_users->get_results(), $manager_users->get_results() ),  
  240. 'number' => $per_page,  
  241. 'offset' => ( $current_page - 1 ) * $per_page,  
  242. ) ); 
  243.  
  244. $this->items = $query->get_results(); 
  245.  
  246. remove_action( 'pre_user_query', array( $this, 'order_by_last_name' ) ); 
  247.  
  248. /** 
  249. * Pagination. 
  250. */ 
  251. $this->set_pagination_args( array( 
  252. 'total_items' => $query->total_users,  
  253. 'per_page' => $per_page,  
  254. 'total_pages' => ceil( $query->total_users / $per_page ),  
  255. ) );