customer list moved to reports

This commit is contained in:
Mike Jolley 2013-07-09 15:45:42 +01:00
parent 1c1d60b375
commit b085df3302
3 changed files with 62 additions and 419 deletions

View File

@ -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 );
?>
<div id="poststuff" class="woocommerce-reports-wrap">
<div class="woocommerce-reports-sidebar">
<div class="postbox">
<h3><span><?php _e( 'Total customers', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_customers>0) echo $total_customers; else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Total customer sales', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_customer_sales>0) echo woocommerce_price($total_customer_sales); else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Total guest sales', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_guest_sales>0) echo woocommerce_price($total_guest_sales); else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Total customer orders', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_customer_orders>0) echo $total_customer_orders; else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Total guest orders', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_guest_orders>0) echo $total_guest_orders; else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Average orders per customer', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_customer_orders>0 && $total_customers>0) echo number_format($total_customer_orders/$total_customers, 2); else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
</div>
<div class="woocommerce-reports-main">
<div class="postbox">
<h3><span><?php _e( 'Signups per day', 'woocommerce' ); ?></span></h3>
<div class="inside chart">
<div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div>
<div id="chart-legend"></div>
</div>
</div>
</div>
</div>
<?php
$start_date = strtotime('-30 days', current_time('timestamp'));
$end_date = current_time('timestamp');
$signups = array();
// Blank date ranges to begin
$count = 0;
$days = ($end_date - $start_date) / (60 * 60 * 24);
if ($days==0) $days = 1;
while ($count < $days) :
$time = strtotime(date('Ymd', strtotime('+ '.$count.' DAY', $start_date))).'000';
$signups[ $time ] = 0;
$count++;
endwhile;
foreach ($customers as $customer) :
if (strtotime($customer->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);
?>
<script type="text/javascript">
jQuery(function(){
var d = jQuery.parseJSON( '<?php echo $chart_data; ?>' );
for (var i = 0; i < d.length; ++i) d[i][0] += 60 * 60 * 1000;
var placeholder = jQuery("#placeholder");
var plot = jQuery.plot(placeholder, [ { data: d } ], {
legend: {
container: jQuery('#chart-legend'),
noColumns: 2
},
series: {
bars: {
barWidth: 60 * 60 * 24 * 1000,
align: "center",
show: true
}
},
grid: {
show: true,
aboveData: false,
color: '#aaa',
backgroundColor: '#fff',
borderWidth: 2,
borderColor: '#aaa',
clickable: false,
hoverable: true,
markings: weekendAreas
},
xaxis: {
mode: "time",
timeformat: "%d %b",
monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ) ?>,
tickLength: 1,
minTickSize: [1, "day"]
},
yaxes: [ { position: "right", min: 0, tickSize: 1, tickDecimals: 0 } ],
colors: ["#8a4b75"]
});
placeholder.resize();
<?php woocommerce_weekend_area_js(); ?>
});
</script>
<?php
}

View File

@ -6,16 +6,9 @@ if ( ! class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
/**
* Admin customers table
*
* Lists customers.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin/Users
* @version 2.0.1
* WC_Report_Customer_List class
*/
class WC_Admin_Customers extends WP_List_Table {
class WC_Report_Customer_List extends WP_List_Table {
/**
* __construct function.
@ -32,6 +25,36 @@ class WC_Admin_Customers extends WP_List_Table {
) );
}
/**
* No items found text
*/
public function no_items() {
_e( 'No customers found.', 'woocommerce' );
}
/**
* Output the report
*/
public function output_report() {
$this->prepare_items();
echo '<div id="poststuff" class="woocommerce-reports-wide">';
if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) {
$linked = woocommerce_update_new_customer_past_orders( absint( $_GET['link_orders'] ) );
echo '<div class="updated"><p>' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '</p></div>';
}
echo '<form method="post" id="woocommerce_customers">';
$this->search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' );
$this->display();
echo '</form>';
echo '</div>';
}
/**
* 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 '<a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a>';
case 'paying' :
$paying_customer = get_user_meta( $user->ID, 'paying_customer', true );
if ( $paying_customer )
return '<img src="' . $woocommerce->plugin_url() . '/assets/images/success@2x.png" alt="yes" width="16px" />';
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 '<a href="' . admin_url( 'post.php?post=' . $order->id . '&action=edit' ) . '">' . $order->get_order_number() . '</a> &ndash; ' . 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
) );

View File

@ -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();
?>
<div class="wrap">
<div id="icon-woocommerce" class="icon32 icon32-woocommerce-users"><br/></div>
<h2><?php _e( 'Customers', 'wc_software' ); ?></h2>
<?php
if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) {
$linked = woocommerce_update_new_customer_past_orders( absint( $_GET['link_orders'] ) );
echo '<div class="updated"><p>' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '</p></div>';
}
?>
<form method="post" id="woocommerce_customers">
<?php $WC_Admin_Customers->search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' ); ?>
<?php $WC_Admin_Customers->display() ?>
</form>
</div>
<?php
}
/**
* Include and display the attibutes page.
*