class-wc-admin-report.php
1 year ago
class-wc-report-coupon-usage.php
5 years ago
class-wc-report-customer-list.php
9 months ago
class-wc-report-customers.php
9 months ago
class-wc-report-downloads.php
5 years ago
class-wc-report-low-in-stock.php
5 years ago
class-wc-report-most-stocked.php
5 years ago
class-wc-report-out-of-stock.php
5 years ago
class-wc-report-sales-by-category.php
2 years ago
class-wc-report-sales-by-date.php
1 year ago
class-wc-report-sales-by-product.php
3 months ago
class-wc-report-stock.php
1 year ago
class-wc-report-taxes-by-code.php
1 year ago
class-wc-report-taxes-by-date.php
1 year ago
class-wc-report-customer-list.php
299 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Report_Customer_List file. |
| 4 | * |
| 5 | * @package WooCommerce\Reports |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 13 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * WC_Report_Customer_List. |
| 18 | * |
| 19 | * @package WooCommerce\Admin\Reports |
| 20 | * @version 2.1.0 |
| 21 | */ |
| 22 | class WC_Report_Customer_List extends WP_List_Table { |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | |
| 29 | parent::__construct( |
| 30 | array( |
| 31 | 'singular' => 'customer', |
| 32 | 'plural' => 'customers', |
| 33 | 'ajax' => false, |
| 34 | ) |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * No items found text. |
| 40 | */ |
| 41 | public function no_items() { |
| 42 | esc_html_e( 'No customers found.', 'woocommerce' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Output the report. |
| 47 | */ |
| 48 | public function output_report() { |
| 49 | $this->prepare_items(); |
| 50 | |
| 51 | echo '<div id="poststuff" class="woocommerce-reports-wide">'; |
| 52 | |
| 53 | if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 54 | $linked = wc_update_new_customer_past_orders( absint( $_GET['link_orders'] ) ); |
| 55 | /* translators: single or plural number of orders */ |
| 56 | echo '<div class="updated"><p>' . esc_html( sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) ) . '</p></div>'; |
| 57 | } |
| 58 | |
| 59 | if ( ! empty( $_GET['refresh'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'refresh' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 60 | $user_id = absint( $_GET['refresh'] ); |
| 61 | $user = get_user_by( 'id', $user_id ); |
| 62 | |
| 63 | delete_user_meta( $user_id, '_money_spent' ); |
| 64 | delete_user_meta( $user_id, '_order_count' ); |
| 65 | delete_user_meta( $user_id, '_last_order' ); |
| 66 | /* translators: User display name */ |
| 67 | echo '<div class="updated"><p>' . sprintf( esc_html__( 'Refreshed stats for %s', 'woocommerce' ), esc_html( $user->display_name ) ) . '</p></div>'; |
| 68 | } |
| 69 | |
| 70 | echo '<form method="post" id="woocommerce_customers">'; |
| 71 | |
| 72 | $this->search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' ); |
| 73 | $this->display(); |
| 74 | |
| 75 | echo '</form>'; |
| 76 | echo '</div>'; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get column value. |
| 81 | * |
| 82 | * @param WP_User $user WP User object. |
| 83 | * @param string $column_name Column name. |
| 84 | * @return string |
| 85 | */ |
| 86 | public function column_default( $user, $column_name ) { |
| 87 | switch ( $column_name ) { |
| 88 | |
| 89 | case 'customer_name': |
| 90 | if ( $user->last_name && $user->first_name ) { |
| 91 | return $user->last_name . ', ' . $user->first_name; |
| 92 | } else { |
| 93 | return '-'; |
| 94 | } |
| 95 | |
| 96 | case 'username': |
| 97 | return $user->user_login; |
| 98 | |
| 99 | case 'location': |
| 100 | $state_code = get_user_meta( $user->ID, 'billing_state', true ); |
| 101 | $country_code = get_user_meta( $user->ID, 'billing_country', true ); |
| 102 | |
| 103 | $state = isset( WC()->countries->states[ $country_code ][ $state_code ] ) ? WC()->countries->states[ $country_code ][ $state_code ] : $state_code; |
| 104 | $country = isset( WC()->countries->countries[ $country_code ] ) ? WC()->countries->countries[ $country_code ] : $country_code; |
| 105 | |
| 106 | $value = ''; |
| 107 | |
| 108 | if ( $state ) { |
| 109 | $value .= $state . ', '; |
| 110 | } |
| 111 | |
| 112 | $value .= $country; |
| 113 | |
| 114 | if ( $value ) { |
| 115 | return $value; |
| 116 | } else { |
| 117 | return '-'; |
| 118 | } |
| 119 | |
| 120 | case 'email': |
| 121 | return '<a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a>'; |
| 122 | |
| 123 | case 'spent': |
| 124 | return wc_price( wc_get_customer_total_spent( $user->ID ) ); |
| 125 | |
| 126 | case 'orders': |
| 127 | return wc_get_customer_order_count( $user->ID ); |
| 128 | |
| 129 | case 'last_order': |
| 130 | $orders = wc_get_orders( |
| 131 | array( |
| 132 | 'limit' => 1, |
| 133 | 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), |
| 134 | 'customer' => $user->ID, |
| 135 | ) |
| 136 | ); |
| 137 | |
| 138 | if ( ! empty( $orders ) ) { |
| 139 | $order = $orders[0]; |
| 140 | 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() ); |
| 141 | } else { |
| 142 | return '-'; |
| 143 | } |
| 144 | |
| 145 | break; |
| 146 | |
| 147 | case 'wc_actions': |
| 148 | ob_start(); |
| 149 | ?><p> |
| 150 | <?php |
| 151 | do_action( 'woocommerce_admin_user_actions_start', $user ); |
| 152 | |
| 153 | $actions = array(); |
| 154 | |
| 155 | $actions['refresh'] = array( |
| 156 | 'url' => wp_nonce_url( add_query_arg( 'refresh', $user->ID ), 'refresh' ), |
| 157 | 'name' => __( 'Refresh stats', 'woocommerce' ), |
| 158 | 'action' => 'refresh', |
| 159 | ); |
| 160 | |
| 161 | $actions['edit'] = array( |
| 162 | 'url' => admin_url( 'user-edit.php?user_id=' . $user->ID ), |
| 163 | 'name' => __( 'Edit', 'woocommerce' ), |
| 164 | 'action' => 'edit', |
| 165 | ); |
| 166 | |
| 167 | $actions['view'] = array( |
| 168 | 'url' => admin_url( 'edit.php?post_type=shop_order&_customer_user=' . $user->ID ), |
| 169 | 'name' => __( 'View orders', 'woocommerce' ), |
| 170 | 'action' => 'view', |
| 171 | ); |
| 172 | |
| 173 | $orders = wc_get_orders( |
| 174 | array( |
| 175 | 'limit' => 1, |
| 176 | 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), |
| 177 | 'customer' => array( array( 0, $user->user_email ) ), |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | if ( $orders ) { |
| 182 | $actions['link'] = array( |
| 183 | 'url' => wp_nonce_url( add_query_arg( 'link_orders', $user->ID ), 'link_orders' ), |
| 184 | 'name' => __( 'Link previous orders', 'woocommerce' ), |
| 185 | 'action' => 'link', |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | $actions = apply_filters( 'woocommerce_admin_user_actions', $actions, $user ); |
| 190 | |
| 191 | foreach ( $actions as $action ) { |
| 192 | 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'] ) ); |
| 193 | } |
| 194 | |
| 195 | do_action( 'woocommerce_admin_user_actions_end', $user ); |
| 196 | ?> |
| 197 | </p> |
| 198 | <?php |
| 199 | $user_actions = ob_get_contents(); |
| 200 | ob_end_clean(); |
| 201 | |
| 202 | return $user_actions; |
| 203 | } |
| 204 | |
| 205 | return ''; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get columns. |
| 210 | * |
| 211 | * @return array |
| 212 | */ |
| 213 | public function get_columns() { |
| 214 | $columns = array( |
| 215 | 'customer_name' => __( 'Name (Last, First)', 'woocommerce' ), |
| 216 | 'username' => __( 'Username', 'woocommerce' ), |
| 217 | 'email' => __( 'Email', 'woocommerce' ), |
| 218 | 'location' => __( 'Location', 'woocommerce' ), |
| 219 | 'orders' => __( 'Orders', 'woocommerce' ), |
| 220 | 'spent' => __( 'Money spent', 'woocommerce' ), |
| 221 | 'last_order' => __( 'Last order', 'woocommerce' ), |
| 222 | 'wc_actions' => __( 'Actions', 'woocommerce' ), |
| 223 | ); |
| 224 | |
| 225 | return $columns; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Order users by name. |
| 230 | * |
| 231 | * @param WP_User_Query $query Query that gets passed through. |
| 232 | * @return WP_User_Query |
| 233 | */ |
| 234 | public function order_by_last_name( $query ) { |
| 235 | global $wpdb; |
| 236 | |
| 237 | $s = ! empty( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 238 | |
| 239 | $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta2 ON ({$wpdb->users}.ID = meta2.user_id) "; |
| 240 | $query->query_where .= " AND meta2.meta_key = 'last_name' "; |
| 241 | $query->query_orderby = ' ORDER BY meta2.meta_value, user_login ASC '; |
| 242 | |
| 243 | if ( $s ) { |
| 244 | $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta3 ON ({$wpdb->users}.ID = meta3.user_id)"; |
| 245 | $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 ) ) . "%' ) "; |
| 246 | $query->query_orderby = ' GROUP BY ID ' . $query->query_orderby; |
| 247 | } |
| 248 | |
| 249 | return $query; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Prepare customer list items. |
| 254 | */ |
| 255 | public function prepare_items() { |
| 256 | $current_page = absint( $this->get_pagenum() ); |
| 257 | $per_page = 20; |
| 258 | |
| 259 | /** |
| 260 | * Init column headers. |
| 261 | */ |
| 262 | $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); |
| 263 | |
| 264 | add_action( 'pre_user_query', array( $this, 'order_by_last_name' ) ); |
| 265 | |
| 266 | $privileged_users = new WP_User_Query( |
| 267 | array( |
| 268 | 'fields' => 'ID', |
| 269 | 'role__in' => array( 'administrator', 'shop_manager' ), |
| 270 | ), |
| 271 | ); |
| 272 | $query = new WP_User_Query( |
| 273 | apply_filters( |
| 274 | 'woocommerce_admin_report_customer_list_user_query_args', |
| 275 | array( |
| 276 | 'exclude' => $privileged_users->get_results(), |
| 277 | 'number' => $per_page, |
| 278 | 'offset' => ( $current_page - 1 ) * $per_page, |
| 279 | ) |
| 280 | ) |
| 281 | ); |
| 282 | |
| 283 | $this->items = $query->get_results(); |
| 284 | |
| 285 | remove_action( 'pre_user_query', array( $this, 'order_by_last_name' ) ); |
| 286 | |
| 287 | /** |
| 288 | * Pagination. |
| 289 | */ |
| 290 | $this->set_pagination_args( |
| 291 | array( |
| 292 | 'total_items' => $query->total_users, |
| 293 | 'per_page' => $per_page, |
| 294 | 'total_pages' => ceil( $query->total_users / $per_page ), |
| 295 | ) |
| 296 | ); |
| 297 | } |
| 298 | } |
| 299 |