From b085df33029d2d0c5cad7637d48b7b38a273c683 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 9 Jul 2013 15:45:42 +0100 Subject: [PATCH] customer list moved to reports --- admin/class-wc-admin-reports.php | 369 +----------------- .../class-wc-report-customer-list.php} | 79 ++-- admin/woocommerce-admin-init.php | 33 -- 3 files changed, 62 insertions(+), 419 deletions(-) rename admin/{woocommerce-admin-customers.php => reports/class-wc-report-customer-list.php} (84%) diff --git a/admin/class-wc-admin-reports.php b/admin/class-wc-admin-reports.php index 0027a9d6511..e78dc85de3b 100644 --- a/admin/class-wc-admin-reports.php +++ b/admin/class-wc-admin-reports.php @@ -104,7 +104,13 @@ class WC_Admin_Reports { 'title' => __( 'Customers', 'woocommerce' ), 'reports' => array( "customers" => array( - 'title' => __( 'Overview', 'woocommerce' ), + 'title' => __( 'Customers vs. Guests', 'woocommerce' ), + 'description' => '', + 'hide_title' => true, + 'callback' => array( $this, 'get_report' ) + ), + "customer_list" => array( + 'title' => __( 'Customer List', 'woocommerce' ), 'description' => '', 'hide_title' => true, 'callback' => array( $this, 'get_report' ) @@ -211,367 +217,6 @@ new WC_Admin_Reports(); -/* - - -/* -$customer_orders = $this->get_order_report_data( array( - 'data' => array( - '_order_total' => array( - 'type' => 'meta', - 'function' => 'SUM', - 'name' => 'total_sales' - ), - 'ID' => array( - 'type' => 'post_data', - 'function' => 'COUNT', - 'name' => 'total_orders' - ), - ), - 'where_meta' => array( - array( - 'meta_key' => '_customer_user', - 'meta_value' => '0', - 'operator' => '>' - ) - ) - ) ); - - $guest_orders = $this->get_order_report_data( array( - 'data' => array( - '_order_total' => array( - 'type' => 'meta', - 'function' => 'SUM', - 'name' => 'total_sales' - ), - 'ID' => array( - 'type' => 'post_data', - 'function' => 'COUNT', - 'name' => 'total_orders' - ), - ), - 'where_meta' => array( - array( - 'meta_key' => '_customer_user', - 'meta_value' => '0', - 'operator' => '=' - ) - ) - ) ); - -// Get order ids and dates in range - $orders = apply_filters('woocommerce_reports_sales_overview_orders', $wpdb->get_results( " - SELECT posts.ID, posts.post_date, COUNT( order_items.order_item_id ) as order_item_count FROM {$wpdb->posts} AS posts - - 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 ) - LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id - - WHERE posts.post_type = 'shop_order' - AND posts.post_status = 'publish' - AND tax.taxonomy = 'shop_order_status' - AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "') - AND post_date > '" . date('Y-m-d', $this->start_date ) . "' - AND post_date < '" . date('Y-m-d', $this->end_date ) . "' - GROUP BY posts.ID - ORDER BY post_date ASC - " ) ); - - if ( $orders ) { - foreach ( $orders as $order ) { - - $order_total = get_post_meta( $order->ID, '_order_total', true ); - $time = strtotime( date( 'Y-m-d', strtotime( $order->post_date ) ) ) . '000'; - - if ( isset( $order_counts[ $time ] ) ) - $order_counts[ $time ]++; - else - $order_counts[ $time ] = 1; - - if ( isset( $order_item_counts[ $time ] ) ) - $order_item_counts[ $time ] += $order->order_item_count; - else - $order_item_counts[ $time ] = $order->order_item_count; - - if ( isset( $order_amounts[ $time ] ) ) - $order_amounts[ $time ] = $order_amounts[ $time ] + $order_total; - else - $order_amounts[ $time ] = floatval( $order_total ); - } - } - - $order_counts_array = $order_amounts_array = $order_item_counts_array = array(); - - foreach ( $order_counts as $key => $count ) - $order_counts_array[] = array( esc_js( $key ), esc_js( $count ) ); - - foreach ( $order_item_counts as $key => $count ) - $order_item_counts_array[] = array( esc_js( $key ), esc_js( $count ) ); - - foreach ( $order_amounts as $key => $amount ) - $order_amounts_array[] = array( esc_js( $key ), esc_js( $amount ) ); - - $chart_data = json_encode( array( 'order_counts' => $order_counts_array, 'order_item_counts' => $order_item_counts_array, 'order_amounts' => $order_amounts_array, 'guest_total_orders' => $guest_orders->total_orders, 'customer_total_orders' => $customer_orders->total_orders ) ); - - jQuery.plot( - jQuery('.chart-placeholder.customers_vs_guests'), - [ - { - label: "Customer", - data: order_data.customer_total_orders, - color: '#3498db', - }, - { - label: "Guest", - data: order_data.guest_total_orders, - color: '#2ecc71', - } - ], - { - series: { - pie: { - show: true, - radius: 1, - innerRadius: 0.6, - label: { - show: true - } - } - }, - legend: { - show: false - } - } - ); - */ - - - -/** - * Output the customer overview stats. - * - * @access public - * @return void - */ -function woocommerce_customer_overview() { - - global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale; - - $total_customers = 0; - $total_customer_sales = 0; - $total_guest_sales = 0; - $total_customer_orders = 0; - $total_guest_orders = 0; - - $users_query = new WP_User_Query( array( - 'fields' => array('user_registered'), - 'role' => 'customer' - ) ); - $customers = $users_query->get_results(); - $total_customers = (int) sizeof($customers); - - $customer_orders = apply_filters( 'woocommerce_reports_customer_overview_customer_orders', $wpdb->get_row( " - SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders 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 = '_order_total' - AND posts.post_type = 'shop_order' - AND posts.post_status = 'publish' - AND tax.taxonomy = 'shop_order_status' - AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "') - AND posts.ID IN ( - SELECT post_id FROM {$wpdb->postmeta} - WHERE meta_key = '_customer_user' - AND meta_value > 0 - ) - " ) ); - - $total_customer_sales = $customer_orders->total_sales; - $total_customer_orders = absint( $customer_orders->total_orders ); - - $guest_orders = apply_filters( 'woocommerce_reports_customer_overview_guest_orders', $wpdb->get_row( " - SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders 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 = '_order_total' - AND posts.post_type = 'shop_order' - AND posts.post_status = 'publish' - AND tax.taxonomy = 'shop_order_status' - AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "') - AND posts.ID IN ( - SELECT post_id FROM {$wpdb->postmeta} - WHERE meta_key = '_customer_user' - AND meta_value = 0 - ) - " ) ); - - $total_guest_sales = $guest_orders->total_sales; - $total_guest_orders = absint( $guest_orders->total_orders ); - ?> -
-
-
-

-
-

0) echo $total_customers; else _e( 'n/a', 'woocommerce' ); ?>

-
-
-
-

-
-

0) echo woocommerce_price($total_customer_sales); else _e( 'n/a', 'woocommerce' ); ?>

-
-
-
-

-
-

0) echo woocommerce_price($total_guest_sales); else _e( 'n/a', 'woocommerce' ); ?>

-
-
-
-

-
-

0) echo $total_customer_orders; else _e( 'n/a', 'woocommerce' ); ?>

-
-
-
-

-
-

0) echo $total_guest_orders; else _e( 'n/a', 'woocommerce' ); ?>

-
-
-
-

-
-

0 && $total_customers>0) echo number_format($total_customer_orders/$total_customers, 2); else _e( 'n/a', 'woocommerce' ); ?>

-
-
-
-
-
-

-
-
-
-
-
-
-
- user_registered) > $start_date) : - $time = strtotime(date('Ymd', strtotime($customer->user_registered))).'000'; - - if (isset($signups[ $time ])) : - $signups[ $time ]++; - else : - $signups[ $time ] = 1; - endif; - endif; - endforeach; - - $signups_array = array(); - foreach ($signups as $key => $count) : - $signups_array[] = array( esc_js( $key ), esc_js( $count ) ); - endforeach; - - $chart_data = json_encode($signups_array); - ?> - - prepare_items(); + + echo '
'; + + if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) { + $linked = woocommerce_update_new_customer_past_orders( absint( $_GET['link_orders'] ) ); + + echo '

' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '

'; + } + + echo '
'; + + $this->search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' ); + $this->display(); + + echo '
'; + echo '
'; + } + /** * column_default function. * @@ -66,18 +89,13 @@ class WC_Admin_Customers extends WP_List_Table { $value .= $country; - return $value; + if ( $value ) + return $value; + else + return '-'; break; case 'email' : return '' . $user->user_email . ''; - case 'paying' : - $paying_customer = get_user_meta( $user->ID, 'paying_customer', true ); - - if ( $paying_customer ) - return 'yes'; - else - return ' - '; - break; case 'spent' : if ( ! $spent = get_user_meta( $user->ID, '_money_spent', true ) ) { @@ -148,7 +166,7 @@ class WC_Admin_Customers extends WP_List_Table { $order = new WC_Order( $order_ids[0] ); echo '' . $order->get_order_number() . ' – ' . date_i18n( get_option( 'date_format', strtotime( $order->order_date ) ) ); - } + } else echo '-'; break; case 'user_actions' : @@ -218,11 +236,10 @@ class WC_Admin_Customers extends WP_List_Table { $columns = array( 'customer_name' => __( 'Name (Last, First)', 'woocommerce' ), 'username' => __( 'Username', 'woocommerce' ), - 'email' => __( 'Email address', 'woocommerce' ), + 'email' => __( 'Email', 'woocommerce' ), 'location' => __( 'Location', 'woocommerce' ), - 'paying' => __( 'Paying customer?', 'woocommerce' ), - 'orders' => __( 'Complete orders', 'woocommerce' ), - 'spent' => __( 'Money spent', 'woocommerce' ), + 'orders' => __( 'Orders', 'woocommerce' ), + 'spent' => __( 'Spent', 'woocommerce' ), 'last_order' => __( 'Last order', 'woocommerce' ), 'user_actions' => __( 'Actions', 'woocommerce' ) ); @@ -272,8 +289,22 @@ class WC_Admin_Customers extends WP_List_Table { /** * Get users */ + $admin_users = new WP_User_Query( + array( + 'role' => 'administrator', + 'fields' => 'ID' + ) + ); + + $manager_users = new WP_User_Query( + array( + 'role' => 'shop_manager', + 'fields' => 'ID' + ) + ); + $query = new WP_User_Query( array( - 'role' => 'customer', + //'exclude' => array_merge( $admin_users->get_results(), $manager_users->get_results() ), 'number' => $per_page, 'offset' => ( $current_page - 1 ) * $per_page ) ); diff --git a/admin/woocommerce-admin-init.php b/admin/woocommerce-admin-init.php index d68032419b9..da457c1b695 100644 --- a/admin/woocommerce-admin-init.php +++ b/admin/woocommerce-admin-init.php @@ -88,7 +88,6 @@ add_action( 'admin_menu', 'woocommerce_admin_menu', 9 ); * @return void */ function woocommerce_admin_menu_after() { - $customers_page = add_submenu_page( 'woocommerce', __( 'Customers', 'woocommerce' ), __( 'Customers', 'woocommerce' ) , 'manage_woocommerce', 'woocommerce_customers', 'woocommerce_customers_page' ); $settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce Settings', 'woocommerce' ), __( 'Settings', 'woocommerce' ) , 'manage_woocommerce', 'woocommerce_settings', 'woocommerce_settings_page'); $status_page = add_submenu_page( 'woocommerce', __( 'WooCommerce Status', 'woocommerce' ), __( 'System Status', 'woocommerce' ) , 'manage_woocommerce', 'woocommerce_status', 'woocommerce_status_page'); @@ -331,38 +330,6 @@ function woocommerce_settings_page() { woocommerce_settings(); } -/** - * Include and display the customers page. - * - * @access public - * @return void - */ -function woocommerce_customers_page() { - include_once( 'woocommerce-admin-customers.php' ); - - $WC_Admin_Customers = new WC_Admin_Customers(); - $WC_Admin_Customers->prepare_items(); - ?> -
-

-

- -

' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '

'; - } - ?> - -
- search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' ); ?> - display() ?> -
- -