2018-12-06 22:21:46 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-01-18 03:13:07 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { Tooltip } from '@wordpress/components';
|
2018-12-06 22:21:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-01-09 00:24:49 +00:00
|
|
|
import { defaultTableDateFormat } from '@woocommerce/date';
|
2018-12-06 22:21:46 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
2019-01-14 09:54:44 +00:00
|
|
|
import { Date, Link } from '@woocommerce/components';
|
2018-12-06 22:21:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ReportTable from 'analytics/components/report-table';
|
|
|
|
import { numberFormat } from 'lib/number';
|
|
|
|
|
|
|
|
export default class CustomersReportTable extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
2019-01-21 15:46:21 +00:00
|
|
|
this.getSummary = this.getSummary.bind( this );
|
2018-12-06 22:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: __( 'Name', 'wc-admin' ),
|
|
|
|
key: 'name',
|
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Username', 'wc-admin' ),
|
|
|
|
key: 'username',
|
|
|
|
hiddenByDefault: true,
|
|
|
|
},
|
2019-01-23 17:51:12 +00:00
|
|
|
{
|
|
|
|
label: __( 'Last Active', 'wc-admin' ),
|
|
|
|
key: 'date_last_active',
|
|
|
|
defaultSort: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
2018-12-06 22:21:46 +00:00
|
|
|
{
|
|
|
|
label: __( 'Sign Up', 'wc-admin' ),
|
2019-01-10 00:43:39 +00:00
|
|
|
key: 'date_registered',
|
2018-12-06 22:21:46 +00:00
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Email', 'wc-admin' ),
|
|
|
|
key: 'email',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Orders', 'wc-admin' ),
|
|
|
|
key: 'orders_count',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-01-23 17:51:12 +00:00
|
|
|
label: __( 'Total Spend', 'wc-admin' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'total_spend',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2018-12-10 15:23:02 +00:00
|
|
|
label: __( 'AOV', 'wc-admin' ),
|
|
|
|
screenReaderLabel: __( 'Average Order Value', 'wc-admin' ),
|
2018-12-12 14:46:40 +00:00
|
|
|
key: 'avg_order_value',
|
2018-12-06 22:21:46 +00:00
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Country', 'wc-admin' ),
|
|
|
|
key: 'country',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'City', 'wc-admin' ),
|
|
|
|
key: 'city',
|
|
|
|
hiddenByDefault: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Postal Code', 'wc-admin' ),
|
|
|
|
key: 'postal_code',
|
|
|
|
hiddenByDefault: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-01-18 03:13:07 +00:00
|
|
|
getCountryName( code ) {
|
|
|
|
const countries = ( wcSettings.dataEndpoints && wcSettings.dataEndpoints.countries ) || [];
|
|
|
|
const country = countries.find( c => c.code === code );
|
|
|
|
return country ? country.name : null;
|
|
|
|
}
|
|
|
|
|
2018-12-06 22:21:46 +00:00
|
|
|
getRowsContent( customers ) {
|
|
|
|
return customers.map( customer => {
|
|
|
|
const {
|
2018-12-12 14:46:40 +00:00
|
|
|
avg_order_value,
|
2018-12-06 22:21:46 +00:00
|
|
|
date_last_active,
|
2019-01-10 00:43:39 +00:00
|
|
|
date_registered,
|
2018-12-06 22:21:46 +00:00
|
|
|
email,
|
2019-01-10 00:43:39 +00:00
|
|
|
name,
|
|
|
|
user_id,
|
2018-12-06 22:21:46 +00:00
|
|
|
orders_count,
|
|
|
|
username,
|
|
|
|
total_spend,
|
2019-01-10 00:43:39 +00:00
|
|
|
postcode,
|
|
|
|
city,
|
|
|
|
country,
|
2018-12-06 22:21:46 +00:00
|
|
|
} = customer;
|
2019-01-18 03:13:07 +00:00
|
|
|
const countryName = this.getCountryName( country );
|
2018-12-06 22:21:46 +00:00
|
|
|
|
2019-01-10 00:43:39 +00:00
|
|
|
const customerNameLink = user_id ? (
|
|
|
|
<Link href={ 'user-edit.php?user_id=' + user_id } type="wp-admin">
|
2018-12-06 22:21:46 +00:00
|
|
|
{ name }
|
|
|
|
</Link>
|
2019-01-10 00:43:39 +00:00
|
|
|
) : (
|
|
|
|
name
|
2018-12-06 22:21:46 +00:00
|
|
|
);
|
|
|
|
|
2019-01-16 21:10:23 +00:00
|
|
|
const dateRegistered = date_registered ? (
|
|
|
|
<Date date={ date_registered } visibleFormat={ defaultTableDateFormat } />
|
|
|
|
) : (
|
|
|
|
'—'
|
|
|
|
);
|
2019-01-10 00:43:39 +00:00
|
|
|
|
2019-01-18 03:13:07 +00:00
|
|
|
const countryDisplay = (
|
|
|
|
<Fragment>
|
|
|
|
<Tooltip text={ countryName }>
|
|
|
|
<span aria-hidden="true">{ country }</span>
|
|
|
|
</Tooltip>
|
|
|
|
<span className="screen-reader-text">{ countryName }</span>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
2018-12-06 22:21:46 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: customerNameLink,
|
|
|
|
value: name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: username,
|
|
|
|
value: username,
|
|
|
|
},
|
2019-01-23 17:51:12 +00:00
|
|
|
{
|
2019-01-25 17:37:50 +00:00
|
|
|
display: date_last_active && (
|
|
|
|
<Date date={ date_last_active } visibleFormat={ defaultTableDateFormat } />
|
|
|
|
),
|
2019-01-23 17:51:12 +00:00
|
|
|
value: date_last_active,
|
|
|
|
},
|
2018-12-06 22:21:46 +00:00
|
|
|
{
|
2019-01-10 00:43:39 +00:00
|
|
|
display: dateRegistered,
|
|
|
|
value: date_registered,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: <a href={ 'mailto:' + email }>{ email }</a>,
|
|
|
|
value: email,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( orders_count ),
|
|
|
|
value: orders_count,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( total_spend ),
|
|
|
|
value: getCurrencyFormatDecimal( total_spend ),
|
|
|
|
},
|
|
|
|
{
|
2018-12-12 14:46:40 +00:00
|
|
|
display: formatCurrency( avg_order_value ),
|
|
|
|
value: getCurrencyFormatDecimal( avg_order_value ),
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-18 03:13:07 +00:00
|
|
|
display: countryDisplay,
|
2018-12-06 22:21:46 +00:00
|
|
|
value: country,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: city,
|
|
|
|
value: city,
|
|
|
|
},
|
|
|
|
{
|
2018-12-12 12:55:10 +00:00
|
|
|
display: postcode,
|
|
|
|
value: postcode,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:46:21 +00:00
|
|
|
getSummary( totals ) {
|
|
|
|
if ( ! totals ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: __( 'customers', 'wc-admin' ),
|
|
|
|
value: numberFormat( totals.customers_count ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'average orders', 'wc-admin' ),
|
|
|
|
value: numberFormat( totals.avg_orders_count ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'average lifetime spend', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.avg_total_spend ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'average order value', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.avg_avg_order_value ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-12-06 22:21:46 +00:00
|
|
|
render() {
|
|
|
|
const { query } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ReportTable
|
|
|
|
endpoint="customers"
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
getRowsContent={ this.getRowsContent }
|
2019-01-21 15:46:21 +00:00
|
|
|
getSummary={ this.getSummary }
|
2018-12-06 22:21:46 +00:00
|
|
|
itemIdField="id"
|
|
|
|
query={ query }
|
2018-12-18 12:35:58 +00:00
|
|
|
labels={ { placeholder: __( 'Search by customer name', 'wc-admin' ) } }
|
2018-12-14 12:29:17 +00:00
|
|
|
searchBy="customers"
|
|
|
|
searchParam="name_includes"
|
2019-01-10 00:43:39 +00:00
|
|
|
title={ __( 'Customers', 'wc-admin' ) }
|
2018-12-13 20:34:23 +00:00
|
|
|
columnPrefsKey="customers_report_columns"
|
2018-12-06 22:21:46 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2019-01-25 17:37:50 +00:00
|
|
|
}
|