2018-12-06 22:21:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-02-05 12:00:37 +00:00
|
|
|
import { __, _n } from '@wordpress/i18n';
|
2022-03-01 12:33:41 +00:00
|
|
|
import { Fragment, useContext } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2019-01-18 03:13:07 +00:00
|
|
|
import { Tooltip } from '@wordpress/components';
|
2024-07-17 17:12:11 +00:00
|
|
|
import { Date, Link, Pill } from '@woocommerce/components';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { formatValue } from '@woocommerce/number';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2020-08-17 21:53:13 +00:00
|
|
|
import { defaultTableDateFormat } from '@woocommerce/date';
|
2022-03-01 12:33:41 +00:00
|
|
|
import { COUNTRIES_STORE_NAME } from '@woocommerce/data';
|
2023-02-28 16:55:49 +00:00
|
|
|
import { CurrencyContext } from '@woocommerce/currency';
|
2018-12-06 22:21:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import ReportTable from '../../components/report-table';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2018-12-06 22:21:46 +00:00
|
|
|
|
2022-03-01 12:33:41 +00:00
|
|
|
function CustomersReportTable( {
|
|
|
|
isRequesting,
|
|
|
|
query,
|
|
|
|
filters,
|
|
|
|
advancedFilters,
|
|
|
|
} ) {
|
|
|
|
const context = useContext( CurrencyContext );
|
|
|
|
const { countries, loadingCountries } = useSelect( ( select ) => {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { getCountries, hasFinishedResolution } =
|
|
|
|
select( COUNTRIES_STORE_NAME );
|
2022-03-01 12:33:41 +00:00
|
|
|
return {
|
|
|
|
countries: getCountries(),
|
|
|
|
loadingCountries: ! hasFinishedResolution( 'getCountries' ),
|
|
|
|
};
|
|
|
|
} );
|
2018-12-06 22:21:46 +00:00
|
|
|
|
2022-03-01 12:33:41 +00:00
|
|
|
const getHeadersContent = () => {
|
2018-12-06 22:21:46 +00:00
|
|
|
return [
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Name', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'name',
|
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Username', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'username',
|
|
|
|
hiddenByDefault: true,
|
|
|
|
},
|
2019-01-23 17:51:12 +00:00
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Last active', 'woocommerce' ),
|
2019-01-23 17:51:12 +00:00
|
|
|
key: 'date_last_active',
|
|
|
|
defaultSort: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
2018-12-06 22:21:46 +00:00
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Date registered', 'woocommerce' ),
|
2019-01-10 00:43:39 +00:00
|
|
|
key: 'date_registered',
|
2018-12-06 22:21:46 +00:00
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Email', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'email',
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Orders', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'orders_count',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Total spend', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'total_spend',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'AOV', 'woocommerce' ),
|
|
|
|
screenReaderLabel: __( 'Average order value', 'woocommerce' ),
|
2018-12-12 14:46:40 +00:00
|
|
|
key: 'avg_order_value',
|
2018-12-06 22:21:46 +00:00
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Country / Region', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'country',
|
2019-06-19 22:15:58 +00:00
|
|
|
isSortable: true,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'City', 'woocommerce' ),
|
2018-12-06 22:21:46 +00:00
|
|
|
key: 'city',
|
|
|
|
hiddenByDefault: true,
|
2019-06-19 22:15:58 +00:00
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Region', 'woocommerce' ),
|
2019-06-19 22:15:58 +00:00
|
|
|
key: 'state',
|
|
|
|
hiddenByDefault: true,
|
|
|
|
isSortable: true,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Postal code', 'woocommerce' ),
|
2019-06-28 18:15:50 +00:00
|
|
|
key: 'postcode',
|
2018-12-06 22:21:46 +00:00
|
|
|
hiddenByDefault: true,
|
2019-06-19 22:15:58 +00:00
|
|
|
isSortable: true,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
];
|
2022-03-01 12:33:41 +00:00
|
|
|
};
|
2018-12-06 22:21:46 +00:00
|
|
|
|
2022-03-01 12:33:41 +00:00
|
|
|
const getCountryName = ( code ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
return typeof countries[ code ] !== 'undefined'
|
|
|
|
? countries[ code ]
|
|
|
|
: null;
|
2022-03-01 12:33:41 +00:00
|
|
|
};
|
2019-01-18 03:13:07 +00:00
|
|
|
|
2022-03-01 12:33:41 +00:00
|
|
|
const getRowsContent = ( customers ) => {
|
2022-01-06 12:53:30 +00:00
|
|
|
const dateFormat = getAdminSetting(
|
|
|
|
'dateFormat',
|
|
|
|
defaultTableDateFormat
|
|
|
|
);
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
2020-06-17 23:33:40 +00:00
|
|
|
formatAmount,
|
2020-04-02 21:54:38 +00:00
|
|
|
formatDecimal: getCurrencyFormatDecimal,
|
2020-06-17 23:33:40 +00:00
|
|
|
getCurrencyConfig,
|
2022-03-01 12:33:41 +00:00
|
|
|
} = context;
|
2020-02-17 01:12:35 +00:00
|
|
|
|
2020-10-16 18:47:29 +00:00
|
|
|
return customers?.map( ( customer ) => {
|
2018-12-06 22:21:46 +00:00
|
|
|
const {
|
2020-02-14 02:23:21 +00:00
|
|
|
avg_order_value: avgOrderValue,
|
|
|
|
date_last_active: dateLastActive,
|
|
|
|
date_registered: dateRegistered,
|
2018-12-06 22:21:46 +00:00
|
|
|
email,
|
2019-01-10 00:43:39 +00:00
|
|
|
name,
|
2020-02-14 02:23:21 +00:00
|
|
|
user_id: userId,
|
|
|
|
orders_count: ordersCount,
|
2018-12-06 22:21:46 +00:00
|
|
|
username,
|
2020-02-14 02:23:21 +00:00
|
|
|
total_spend: totalSpend,
|
2019-01-10 00:43:39 +00:00
|
|
|
postcode,
|
|
|
|
city,
|
2019-06-19 22:15:58 +00:00
|
|
|
state,
|
2019-01-10 00:43:39 +00:00
|
|
|
country,
|
2018-12-06 22:21:46 +00:00
|
|
|
} = customer;
|
2022-03-01 12:33:41 +00:00
|
|
|
const countryName = getCountryName( country );
|
2024-07-17 17:12:11 +00:00
|
|
|
const customerName =
|
|
|
|
name?.trim() !== '' ? (
|
|
|
|
name
|
|
|
|
) : (
|
|
|
|
<Pill>{ __( 'Guest', 'woocommerce' ) }</Pill>
|
|
|
|
);
|
2018-12-06 22:21:46 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const customerNameLink = userId ? (
|
|
|
|
<Link
|
|
|
|
href={ getAdminLink( 'user-edit.php?user_id=' + userId ) }
|
|
|
|
type="wp-admin"
|
|
|
|
>
|
2018-12-06 22:21:46 +00:00
|
|
|
{ name }
|
|
|
|
</Link>
|
2019-01-10 00:43:39 +00:00
|
|
|
) : (
|
2024-07-17 17:12:11 +00:00
|
|
|
customerName
|
2018-12-06 22:21:46 +00:00
|
|
|
);
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const dateLastActiveDisplay = dateLastActive ? (
|
2020-04-02 21:54:38 +00:00
|
|
|
<Date date={ dateLastActive } visibleFormat={ dateFormat } />
|
2019-08-07 19:12:47 +00:00
|
|
|
) : (
|
|
|
|
'—'
|
|
|
|
);
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const dateRegisteredDisplay = dateRegistered ? (
|
2020-04-02 21:54:38 +00:00
|
|
|
<Date date={ dateRegistered } visibleFormat={ dateFormat } />
|
2019-01-16 21:10:23 +00:00
|
|
|
) : (
|
|
|
|
'—'
|
|
|
|
);
|
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
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: dateLastActiveDisplay,
|
|
|
|
value: dateLastActive,
|
2019-01-23 17:51:12 +00:00
|
|
|
},
|
2018-12-06 22:21:46 +00:00
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: dateRegisteredDisplay,
|
|
|
|
value: dateRegistered,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: <a href={ 'mailto:' + email }>{ email }</a>,
|
|
|
|
value: email,
|
|
|
|
},
|
|
|
|
{
|
2020-04-02 21:54:38 +00:00
|
|
|
display: formatValue(
|
2020-06-17 23:33:40 +00:00
|
|
|
getCurrencyConfig(),
|
2020-04-02 21:54:38 +00:00
|
|
|
'number',
|
|
|
|
ordersCount
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: ordersCount,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( totalSpend ),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: getCurrencyFormatDecimal( totalSpend ),
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( avgOrderValue ),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: getCurrencyFormatDecimal( avgOrderValue ),
|
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,
|
|
|
|
},
|
2019-06-19 22:15:58 +00:00
|
|
|
{
|
|
|
|
display: state,
|
|
|
|
value: state,
|
|
|
|
},
|
2018-12-06 22:21:46 +00:00
|
|
|
{
|
2018-12-12 12:55:10 +00:00
|
|
|
display: postcode,
|
|
|
|
value: postcode,
|
2018-12-06 22:21:46 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
2022-03-01 12:33:41 +00:00
|
|
|
};
|
2018-12-06 22:21:46 +00:00
|
|
|
|
2022-03-01 12:33:41 +00:00
|
|
|
const getSummary = ( totals ) => {
|
2019-02-05 12:00:37 +00:00
|
|
|
const {
|
2020-02-14 02:23:21 +00:00
|
|
|
customers_count: customersCount = 0,
|
|
|
|
avg_orders_count: avgOrdersCount = 0,
|
|
|
|
avg_total_spend: avgTotalSpend = 0,
|
|
|
|
avg_avg_order_value: avgAvgOrderValue = 0,
|
2019-02-05 12:00:37 +00:00
|
|
|
} = totals;
|
2022-03-01 12:33:41 +00:00
|
|
|
const { formatAmount, getCurrencyConfig } = context;
|
2020-06-17 23:33:40 +00:00
|
|
|
const currency = getCurrencyConfig();
|
2019-01-21 15:46:21 +00:00
|
|
|
return [
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
|
|
|
'customer',
|
|
|
|
'customers',
|
|
|
|
customersCount,
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', customersCount ),
|
2019-01-21 15:46:21 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
'Average order',
|
|
|
|
'Average orders',
|
2020-02-14 02:23:21 +00:00
|
|
|
avgOrdersCount,
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', avgOrdersCount ),
|
2019-01-21 15:46:21 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Average lifetime spend', 'woocommerce' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( avgTotalSpend ),
|
2019-01-21 15:46:21 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Average order value', 'woocommerce' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( avgAvgOrderValue ),
|
2019-01-21 15:46:21 +00:00
|
|
|
},
|
|
|
|
];
|
2022-03-01 12:33:41 +00:00
|
|
|
};
|
2019-01-21 15:46:21 +00:00
|
|
|
|
2022-03-01 12:33:41 +00:00
|
|
|
return (
|
|
|
|
<ReportTable
|
|
|
|
endpoint="customers"
|
|
|
|
getHeadersContent={ getHeadersContent }
|
|
|
|
getRowsContent={ getRowsContent }
|
|
|
|
getSummary={ getSummary }
|
|
|
|
summaryFields={ [
|
|
|
|
'customers_count',
|
|
|
|
'avg_orders_count',
|
|
|
|
'avg_total_spend',
|
|
|
|
'avg_avg_order_value',
|
|
|
|
] }
|
|
|
|
isRequesting={ isRequesting || loadingCountries }
|
|
|
|
itemIdField="id"
|
|
|
|
query={ query }
|
|
|
|
labels={ {
|
2022-03-30 09:00:04 +00:00
|
|
|
placeholder: __( 'Search by customer name', 'woocommerce' ),
|
2022-03-01 12:33:41 +00:00
|
|
|
} }
|
|
|
|
searchBy="customers"
|
2022-03-30 09:00:04 +00:00
|
|
|
title={ __( 'Customers', 'woocommerce' ) }
|
2022-03-01 12:33:41 +00:00
|
|
|
columnPrefsKey="customers_report_columns"
|
|
|
|
filters={ filters }
|
|
|
|
advancedFilters={ advancedFilters }
|
|
|
|
/>
|
|
|
|
);
|
2019-01-29 16:48:46 +00:00
|
|
|
}
|
2020-04-02 21:54:38 +00:00
|
|
|
|
|
|
|
export default CustomersReportTable;
|