2013-06-05 15:04:09 +00:00
< ? php
2013-12-30 16:28:39 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2013-06-05 15:04:09 +00:00
exit ; // Exit if accessed directly
2013-12-30 16:28:39 +00:00
}
2013-06-05 15:04:09 +00:00
2013-12-30 16:28:39 +00:00
if ( ! class_exists ( 'WP_List_Table' ) ) {
2013-06-05 15:04:09 +00:00
require_once ( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
2013-12-30 16:28:39 +00:00
}
2013-06-05 15:04:09 +00:00
/**
2013-07-09 14:45:42 +00:00
* WC_Report_Customer_List class
2013-06-05 15:04:09 +00:00
*/
2013-07-09 14:45:42 +00:00
class WC_Report_Customer_List extends WP_List_Table {
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
/**
* __construct function .
*
* @ access public
*/
function __construct (){
global $status , $page ;
parent :: __construct ( array (
'singular' => __ ( 'Customer' , 'woocommerce' ),
'plural' => __ ( 'Customers' , 'woocommerce' ),
'ajax' => false
) );
}
/**
* No items found text
*/
public function no_items () {
_e ( 'No customers found.' , 'woocommerce' );
}
2013-07-09 14:45:42 +00:00
/**
* Output the report
*/
public function output_report () {
$this -> prepare_items ();
echo '<div id="poststuff" class="woocommerce-reports-wide">' ;
2013-12-30 16:25:35 +00:00
if ( ! empty ( $_GET [ 'link_orders' ] ) && wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'link_orders' ) ) {
2013-11-25 12:52:53 +00:00
$linked = wc_update_new_customer_past_orders ( absint ( $_GET [ 'link_orders' ] ) );
2013-07-09 14:45:42 +00:00
echo '<div class="updated"><p>' . sprintf ( _n ( '%s previous order linked' , '%s previous orders linked' , $linked , 'woocommerce' ), $linked ) . '</p></div>' ;
}
2013-12-30 16:25:35 +00:00
echo '<form method="post" id="woocommerce_customers">' ;
2013-07-09 14:45:42 +00:00
$this -> search_box ( __ ( 'Search customers' , 'woocommerce' ), 'customer_search' );
2013-12-30 16:25:35 +00:00
$this -> display ();
2013-07-09 14:45:42 +00:00
echo '</form>' ;
echo '</div>' ;
}
2013-11-28 16:49:30 +00:00
/**
* column_default function .
* @ access public
* @ param mixed $user
* @ param string $column_name
* @ return int | string
2013-12-02 11:34:27 +00:00
* @ todo Inconsistent return types , and void return at the end . Needs a rewrite .
2013-11-28 16:49:30 +00:00
*/
2013-12-30 16:25:35 +00:00
function column_default ( $user , $column_name ) {
global $wpdb ;
switch ( $column_name ) {
case 'customer_name' :
2013-12-30 16:28:39 +00:00
if ( $user -> last_name && $user -> first_name ) {
2013-12-30 16:25:35 +00:00
return $user -> last_name . ', ' . $user -> first_name ;
2013-12-30 16:28:39 +00:00
} else {
2013-12-30 16:25:35 +00:00
return '-' ;
2013-12-30 16:28:39 +00:00
}
2013-12-30 16:25:35 +00:00
case 'username' :
return $user -> user_login ;
break ;
case 'location' :
2013-06-05 15:04:09 +00:00
$state_code = get_user_meta ( $user -> ID , 'billing_state' , true );
$country_code = get_user_meta ( $user -> ID , 'billing_country' , true );
2013-12-30 16:28:39 +00:00
$state = isset ( WC () -> countries -> states [ $country_code ][ $state_code ] ) ? WC () -> countries -> states [ $country_code ][ $state_code ] : $state_code ;
2013-11-25 14:01:32 +00:00
$country = isset ( WC () -> countries -> countries [ $country_code ] ) ? WC () -> countries -> countries [ $country_code ] : $country_code ;
2013-06-05 15:04:09 +00:00
$value = '' ;
2013-12-30 16:28:39 +00:00
if ( $state ) {
2013-06-05 15:04:09 +00:00
$value .= $state . ', ' ;
2013-12-30 16:28:39 +00:00
}
2013-06-05 15:04:09 +00:00
$value .= $country ;
2013-12-30 16:28:39 +00:00
if ( $value ) {
2013-07-09 14:45:42 +00:00
return $value ;
2013-12-30 16:28:39 +00:00
} else {
2013-12-30 16:25:35 +00:00
return '-' ;
2013-12-30 16:28:39 +00:00
}
2013-12-30 16:25:35 +00:00
break ;
case 'email' :
return '<a href="mailto:' . $user -> user_email . '">' . $user -> user_email . '</a>' ;
2013-06-05 15:04:09 +00:00
case 'spent' :
if ( ! $spent = get_user_meta ( $user -> ID , '_money_spent' , true ) ) {
$spent = $wpdb -> get_var ( " SELECT SUM(meta2.meta_value)
FROM $wpdb -> posts as posts
LEFT JOIN { $wpdb -> postmeta } AS meta ON posts . ID = meta . post_id
LEFT JOIN { $wpdb -> postmeta } AS meta2 ON posts . ID = meta2 . post_id
LEFT JOIN { $wpdb -> term_relationships } AS rel ON posts . ID = rel . object_ID
LEFT JOIN { $wpdb -> term_taxonomy } AS tax USING ( term_taxonomy_id )
LEFT JOIN { $wpdb -> terms } AS term USING ( term_id )
WHERE meta . meta_key = '_customer_user'
AND meta . meta_value = $user -> ID
AND posts . post_type = 'shop_order'
AND posts . post_status = 'publish'
AND tax . taxonomy = 'shop_order_status'
AND term . slug IN ( 'completed' )
AND meta2 . meta_key = '_order_total'
" );
update_user_meta ( $user -> ID , '_money_spent' , $spent );
}
2013-11-25 13:34:21 +00:00
return wc_price ( $spent );
2013-06-05 15:04:09 +00:00
break ;
case 'orders' :
if ( ! $count = get_user_meta ( $user -> ID , '_order_count' , true ) ) {
$count = $wpdb -> get_var ( " SELECT COUNT(*)
FROM $wpdb -> posts as posts
LEFT JOIN { $wpdb -> postmeta } AS meta ON posts . ID = meta . post_id
LEFT JOIN { $wpdb -> term_relationships } AS rel ON posts . ID = rel . object_ID
LEFT JOIN { $wpdb -> term_taxonomy } AS tax USING ( term_taxonomy_id )
LEFT JOIN { $wpdb -> terms } AS term USING ( term_id )
WHERE meta . meta_key = '_customer_user'
AND posts . post_type = 'shop_order'
AND posts . post_status = 'publish'
AND tax . taxonomy = 'shop_order_status'
AND term . slug IN ( 'completed' )
AND meta_value = $user -> ID
" );
update_user_meta ( $user -> ID , '_order_count' , $count );
}
2013-06-05 15:32:50 +00:00
return absint ( $count );
2013-06-05 15:04:09 +00:00
break ;
case 'last_order' :
$order_ids = get_posts ( array (
'posts_per_page' => 1 ,
'post_type' => 'shop_order' ,
'orderby' => 'date' ,
'order' => 'desc' ,
'meta_query' => array (
array (
'key' => '_customer_user' ,
'value' => $user -> ID
)
),
'fields' => 'ids'
) );
if ( $order_ids ) {
$order = new WC_Order ( $order_ids [ 0 ] );
2014-02-07 14:50:11 +00:00
echo '<a href="' . admin_url ( 'post.php?post=' . $order -> id . '&action=edit' ) . '">' . $order -> get_order_number () . '</a> – ' . date_i18n ( get_option ( 'date_format' ), strtotime ( $order -> order_date ) );
2013-07-09 14:45:42 +00:00
} else echo '-' ;
2013-06-05 15:04:09 +00:00
break ;
case 'user_actions' :
?> <p>
< ? php
do_action ( 'woocommerce_admin_user_actions_start' , $user );
$actions = array ();
$actions [ 'edit' ] = array (
'url' => admin_url ( 'user-edit.php?user_id=' . $user -> ID ),
'name' => __ ( 'Edit' , 'woocommerce' ),
'action' => " edit "
);
$actions [ 'view' ] = array (
'url' => admin_url ( 'edit.php?post_type=shop_order&_customer_user=' . $user -> ID ),
'name' => __ ( 'View orders' , 'woocommerce' ),
'action' => " view "
);
$order_ids = get_posts ( array (
'posts_per_page' => 1 ,
'post_type' => 'shop_order' ,
'meta_query' => array (
array (
'key' => '_customer_user' ,
'value' => array ( 0 , '' ),
'compare' => 'IN'
),
array (
'key' => '_billing_email' ,
'value' => $user -> user_email
)
),
'fields' => 'ids'
) );
if ( $order_ids ) {
$actions [ 'link' ] = array (
'url' => wp_nonce_url ( add_query_arg ( 'link_orders' , $user -> ID ), 'link_orders' ),
'name' => __ ( 'Link previous orders' , 'woocommerce' ),
'action' => " link "
);
}
$actions = apply_filters ( 'woocommerce_admin_user_actions' , $actions , $user );
foreach ( $actions as $action ) {
2013-12-05 13:16:34 +00:00
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' ] ) );
2013-06-05 15:04:09 +00:00
}
do_action ( 'woocommerce_admin_user_actions_end' , $user );
?>
</ p >< ? php
break ;
2013-12-30 16:25:35 +00:00
}
2013-06-05 15:04:09 +00:00
}
2013-12-30 16:25:35 +00:00
/**
* get_columns function .
*
* @ access public
*/
function get_columns (){
$columns = array (
2013-06-05 15:04:09 +00:00
'customer_name' => __ ( 'Name (Last, First)' , 'woocommerce' ),
'username' => __ ( 'Username' , 'woocommerce' ),
2013-07-09 14:45:42 +00:00
'email' => __ ( 'Email' , 'woocommerce' ),
2013-06-05 15:04:09 +00:00
'location' => __ ( 'Location' , 'woocommerce' ),
2013-07-09 14:45:42 +00:00
'orders' => __ ( 'Orders' , 'woocommerce' ),
'spent' => __ ( 'Spent' , 'woocommerce' ),
2013-06-05 15:04:09 +00:00
'last_order' => __ ( 'Last order' , 'woocommerce' ),
'user_actions' => __ ( 'Actions' , 'woocommerce' )
2013-12-30 16:25:35 +00:00
);
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
return $columns ;
}
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
/**
* Order users by name
*/
2013-06-05 15:04:09 +00:00
public function order_by_last_name ( $query ) {
2013-12-30 16:25:35 +00:00
global $wpdb ;
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
$s = ! empty ( $_REQUEST [ 's' ] ) ? stripslashes ( $_REQUEST [ 's' ] ) : '' ;
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
$query -> query_from .= " LEFT JOIN { $wpdb -> usermeta } as meta2 ON ( { $wpdb -> users } .ID = meta2.user_id) " ;
$query -> query_where .= " AND meta2.meta_key = 'last_name' " ;
$query -> query_orderby = " ORDER BY meta2.meta_value, user_login ASC " ;
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
if ( $s ) {
$query -> query_from .= " LEFT JOIN { $wpdb -> usermeta } as meta3 ON ( { $wpdb -> users } .ID = meta3.user_id) " ;
$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 ) ) . " %' ) " ;
$query -> query_orderby = " GROUP BY ID " . $query -> query_orderby ;
}
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
return $query ;
2013-06-05 15:04:09 +00:00
}
2013-12-30 16:25:35 +00:00
/**
* prepare_items function .
*
* @ access public
*/
public function prepare_items () {
global $wpdb ;
2013-06-05 15:04:09 +00:00
$current_page = absint ( $this -> get_pagenum () );
$per_page = 20 ;
2013-12-30 16:25:35 +00:00
/**
* Init column headers
*/
$this -> _column_headers = array ( $this -> get_columns (), array (), $this -> get_sortable_columns () );
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
add_action ( 'pre_user_query' , array ( $this , 'order_by_last_name' ) );
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
/**
* Get users
*/
$admin_users = new WP_User_Query (
2013-07-09 14:45:42 +00:00
array (
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$manager_users = new WP_User_Query (
array (
'role' => 'shop_manager' ,
'fields' => 'ID'
)
);
2013-06-05 15:04:09 +00:00
$query = new WP_User_Query ( array (
2013-07-10 11:05:45 +00:00
'exclude' => array_merge ( $admin_users -> get_results (), $manager_users -> get_results () ),
2013-06-05 15:04:09 +00:00
'number' => $per_page ,
'offset' => ( $current_page - 1 ) * $per_page
) );
$this -> items = $query -> get_results ();
remove_action ( 'pre_user_query' , array ( $this , 'order_by_last_name' ) );
2013-12-30 16:25:35 +00:00
/**
* Pagination
*/
$this -> set_pagination_args ( array (
'total_items' => $query -> total_users ,
'per_page' => $per_page ,
'total_pages' => ceil ( $query -> total_users / $per_page )
) );
}
2013-12-02 11:34:27 +00:00
}