2018-10-11 08:30:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2020-06-18 17:43:56 +00:00
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import { map } from 'lodash';
|
2019-01-14 09:54:44 +00:00
|
|
|
|
import { Date, Link, OrderStatus, ViewMoreList } from '@woocommerce/components';
|
2020-04-02 21:54:38 +00:00
|
|
|
|
import { formatValue } from '@woocommerce/number';
|
2019-10-11 15:45:45 +00:00
|
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-08-13 02:05:22 +00:00
|
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
2020-08-17 21:53:13 +00:00
|
|
|
|
import { defaultTableDateFormat } from '@woocommerce/date';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
|
import ReportTable from '../../components/report-table';
|
|
|
|
|
import { CurrencyContext } from '../../../lib/currency-context';
|
2018-10-18 10:43:45 +00:00
|
|
|
|
import './style.scss';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2020-12-04 04:19:04 +00:00
|
|
|
|
const capitalizeFirstLetter = ( expr ) =>
|
|
|
|
|
expr.charAt( 0 ).toUpperCase() + expr.slice( 1 );
|
|
|
|
|
|
2020-04-02 21:54:38 +00:00
|
|
|
|
class OrdersReportTable extends Component {
|
2018-12-03 03:40:57 +00:00
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
|
|
|
|
this.getSummary = this.getSummary.bind( this );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 08:30:51 +00:00
|
|
|
|
getHeadersContent() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: __( 'Date', 'woocommerce-admin' ),
|
2018-10-16 08:50:07 +00:00
|
|
|
|
key: 'date',
|
2018-10-11 08:30:51 +00:00
|
|
|
|
required: true,
|
|
|
|
|
defaultSort: true,
|
|
|
|
|
isLeftAligned: true,
|
|
|
|
|
isSortable: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: __( 'Order #', 'woocommerce-admin' ),
|
|
|
|
|
screenReaderLabel: __( 'Order Number', 'woocommerce-admin' ),
|
2019-02-28 14:14:21 +00:00
|
|
|
|
key: 'order_number',
|
2018-10-11 08:30:51 +00:00
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: __( 'Status', 'woocommerce-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'status',
|
|
|
|
|
required: false,
|
2018-10-16 08:50:07 +00:00
|
|
|
|
isSortable: false,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: __( 'Customer', 'woocommerce-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'customer_id',
|
|
|
|
|
required: false,
|
2018-10-16 08:50:07 +00:00
|
|
|
|
isSortable: false,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
2020-12-04 04:19:04 +00:00
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
|
label: __( 'Customer type', 'woocommerce-admin' ),
|
2020-12-04 04:19:04 +00:00
|
|
|
|
key: 'customer_type',
|
|
|
|
|
required: false,
|
|
|
|
|
isSortable: false,
|
|
|
|
|
},
|
2018-10-11 08:30:51 +00:00
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: __( 'Product(s)', 'woocommerce-admin' ),
|
|
|
|
|
screenReaderLabel: __( 'Products', 'woocommerce-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'products',
|
|
|
|
|
required: false,
|
|
|
|
|
isSortable: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
|
label: __( 'Items sold', 'woocommerce-admin' ),
|
2019-02-15 10:54:41 +00:00
|
|
|
|
key: 'num_items_sold',
|
2018-10-11 08:30:51 +00:00
|
|
|
|
required: false,
|
2019-02-15 10:54:41 +00:00
|
|
|
|
isSortable: true,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
isNumeric: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: __( 'Coupon(s)', 'woocommerce-admin' ),
|
|
|
|
|
screenReaderLabel: __( 'Coupons', 'woocommerce-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'coupons',
|
|
|
|
|
required: false,
|
|
|
|
|
isSortable: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
|
label: __( 'Net sales', 'woocommerce-admin' ),
|
|
|
|
|
screenReaderLabel: __( 'Net sales', 'woocommerce-admin' ),
|
2019-02-15 10:54:41 +00:00
|
|
|
|
key: 'net_total',
|
2018-10-11 08:30:51 +00:00
|
|
|
|
required: true,
|
2019-02-15 10:54:41 +00:00
|
|
|
|
isSortable: true,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
isNumeric: true,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 17:43:56 +00:00
|
|
|
|
getCustomerName( customer ) {
|
|
|
|
|
const { first_name: firstName, last_name: lastName } = customer || {};
|
|
|
|
|
|
|
|
|
|
if ( ! firstName && ! lastName ) {
|
|
|
|
|
return '';
|
2019-05-16 05:04:37 +00:00
|
|
|
|
}
|
2020-06-18 17:43:56 +00:00
|
|
|
|
|
|
|
|
|
return [ firstName, lastName ].join( ' ' );
|
2019-05-16 05:04:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 08:30:51 +00:00
|
|
|
|
getRowsContent( tableData ) {
|
2019-01-20 22:31:03 +00:00
|
|
|
|
const { query } = this.props;
|
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
2020-02-17 01:12:35 +00:00
|
|
|
|
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
2020-06-17 23:33:40 +00:00
|
|
|
|
const { render: renderCurrency, getCurrencyConfig } = this.context;
|
2020-02-17 01:12:35 +00:00
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
|
return map( tableData, ( row ) => {
|
2018-10-11 08:30:51 +00:00
|
|
|
|
const {
|
2019-01-18 02:52:58 +00:00
|
|
|
|
currency,
|
2020-02-14 02:23:21 +00:00
|
|
|
|
date_created: dateCreated,
|
|
|
|
|
net_total: netTotal,
|
|
|
|
|
num_items_sold: numItemsSold,
|
|
|
|
|
order_id: orderId,
|
|
|
|
|
order_number: orderNumber,
|
|
|
|
|
parent_id: parentId,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
status,
|
2020-12-04 04:19:04 +00:00
|
|
|
|
customer_type: customerType,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
} = row;
|
2020-02-14 02:23:21 +00:00
|
|
|
|
const extendedInfo = row.extended_info || {};
|
2020-06-18 17:43:56 +00:00
|
|
|
|
const { coupons, customer, products } = extendedInfo;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
|
const formattedProducts = products
|
2018-10-12 21:29:25 +00:00
|
|
|
|
.sort( ( itemA, itemB ) => itemB.quantity - itemA.quantity )
|
2020-02-14 02:23:21 +00:00
|
|
|
|
.map( ( item ) => ( {
|
2018-10-12 21:29:25 +00:00
|
|
|
|
label: item.name,
|
|
|
|
|
quantity: item.quantity,
|
2019-03-19 01:27:31 +00:00
|
|
|
|
href: getNewPath( persistedQuery, '/analytics/products', {
|
2019-01-20 22:31:03 +00:00
|
|
|
|
filter: 'single_product',
|
|
|
|
|
products: item.id,
|
|
|
|
|
} ),
|
2018-10-12 21:29:25 +00:00
|
|
|
|
} ) );
|
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
|
const formattedCoupons = coupons.map( ( coupon ) => ( {
|
2018-10-12 21:29:25 +00:00
|
|
|
|
label: coupon.code,
|
2019-03-19 01:27:31 +00:00
|
|
|
|
href: getNewPath( persistedQuery, '/analytics/coupons', {
|
2019-01-20 22:31:03 +00:00
|
|
|
|
filter: 'single_coupon',
|
|
|
|
|
coupons: coupon.id,
|
|
|
|
|
} ),
|
2018-10-12 21:29:25 +00:00
|
|
|
|
} ) );
|
|
|
|
|
|
2018-10-11 08:30:51 +00:00
|
|
|
|
return [
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
display: (
|
|
|
|
|
<Date
|
|
|
|
|
date={ dateCreated }
|
2020-02-17 01:12:35 +00:00
|
|
|
|
visibleFormat={ dateFormat }
|
2020-02-14 02:23:21 +00:00
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
value: dateCreated,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-12-18 09:44:43 +00:00
|
|
|
|
display: (
|
2019-05-16 05:04:37 +00:00
|
|
|
|
<Link
|
|
|
|
|
href={
|
|
|
|
|
'post.php?post=' +
|
2020-02-14 02:23:21 +00:00
|
|
|
|
( parentId ? parentId : orderId ) +
|
2019-05-16 05:04:37 +00:00
|
|
|
|
'&action=edit' +
|
2020-02-14 02:23:21 +00:00
|
|
|
|
( parentId ? '#order_refunds' : '' )
|
2019-05-16 05:04:37 +00:00
|
|
|
|
}
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
|
{ orderNumber }
|
2018-12-18 09:44:43 +00:00
|
|
|
|
</Link>
|
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
|
value: orderNumber,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-10-18 10:43:45 +00:00
|
|
|
|
display: (
|
2019-10-11 15:45:45 +00:00
|
|
|
|
<OrderStatus
|
|
|
|
|
className="woocommerce-orders-table__status"
|
|
|
|
|
order={ { status } }
|
|
|
|
|
orderStatusMap={ getSetting( 'orderStatuses', {} ) }
|
|
|
|
|
/>
|
2018-10-18 10:43:45 +00:00
|
|
|
|
),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
value: status,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-06-18 17:43:56 +00:00
|
|
|
|
display: this.getCustomerName( customer ),
|
|
|
|
|
value: this.getCustomerName( customer ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
2020-12-04 04:19:04 +00:00
|
|
|
|
{
|
|
|
|
|
display: capitalizeFirstLetter( customerType ),
|
|
|
|
|
value: customerType,
|
|
|
|
|
},
|
2018-10-11 08:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
display: this.renderList(
|
2020-02-14 02:23:21 +00:00
|
|
|
|
formattedProducts.length
|
|
|
|
|
? [ formattedProducts[ 0 ] ]
|
|
|
|
|
: [],
|
|
|
|
|
formattedProducts.map( ( product ) => ( {
|
2019-03-13 17:14:02 +00:00
|
|
|
|
label: sprintf(
|
|
|
|
|
__( '%s× %s', 'woocommerce-admin' ),
|
|
|
|
|
product.quantity,
|
|
|
|
|
product.label
|
|
|
|
|
),
|
2018-10-12 21:29:25 +00:00
|
|
|
|
href: product.href,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
} ) )
|
|
|
|
|
),
|
2019-10-24 16:41:16 +00:00
|
|
|
|
value: formattedProducts
|
|
|
|
|
.map( ( { quantity, label } ) =>
|
2020-02-14 02:23:21 +00:00
|
|
|
|
sprintf(
|
|
|
|
|
__( '%s× %s', 'woocommerce-admin' ),
|
|
|
|
|
quantity,
|
|
|
|
|
label
|
|
|
|
|
)
|
2019-10-24 16:41:16 +00:00
|
|
|
|
)
|
|
|
|
|
.join( ', ' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
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',
|
|
|
|
|
numItemsSold
|
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
|
value: numItemsSold,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-01-18 02:52:58 +00:00
|
|
|
|
display: this.renderList(
|
2020-02-14 02:23:21 +00:00
|
|
|
|
formattedCoupons.length
|
|
|
|
|
? [ formattedCoupons[ 0 ] ]
|
|
|
|
|
: [],
|
2019-01-18 02:52:58 +00:00
|
|
|
|
formattedCoupons
|
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
|
value: formattedCoupons
|
|
|
|
|
.map( ( coupon ) => coupon.label )
|
|
|
|
|
.join( ', ' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
display: renderCurrency( netTotal, currency ),
|
|
|
|
|
value: netTotal,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 19:09:38 +00:00
|
|
|
|
getSummary( totals ) {
|
2019-02-05 12:00:37 +00:00
|
|
|
|
const {
|
2020-02-14 02:23:21 +00:00
|
|
|
|
orders_count: ordersCount = 0,
|
2020-06-18 17:43:56 +00:00
|
|
|
|
total_customers: totalCustomers = 0,
|
2019-02-05 12:00:37 +00:00
|
|
|
|
products = 0,
|
2020-02-14 02:23:21 +00:00
|
|
|
|
num_items_sold: numItemsSold = 0,
|
|
|
|
|
coupons_count: couponsCount = 0,
|
|
|
|
|
net_revenue: netRevenue = 0,
|
2019-02-05 12:00:37 +00:00
|
|
|
|
} = totals;
|
2020-06-17 23:33:40 +00:00
|
|
|
|
const { formatAmount, getCurrencyConfig } = this.context;
|
|
|
|
|
const currency = getCurrencyConfig();
|
2018-10-31 19:09:38 +00:00
|
|
|
|
return [
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
|
'Order',
|
|
|
|
|
'Orders',
|
2020-02-14 02:23:21 +00:00
|
|
|
|
ordersCount,
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
|
value: formatValue( currency, 'number', ordersCount ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
|
' Customer',
|
|
|
|
|
' Customers',
|
2020-06-18 17:43:56 +00:00
|
|
|
|
totalCustomers,
|
2019-03-13 17:14:02 +00:00
|
|
|
|
'woocommerce-admin'
|
2018-10-31 19:09:38 +00:00
|
|
|
|
),
|
2020-06-18 17:43:56 +00:00
|
|
|
|
value: formatValue( currency, 'number', totalCustomers ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
|
'Product',
|
|
|
|
|
'Products',
|
2020-02-14 02:23:21 +00:00
|
|
|
|
products,
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
|
value: formatValue( currency, 'number', products ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
|
'Item sold',
|
|
|
|
|
'Items sold',
|
2020-02-14 02:23:21 +00:00
|
|
|
|
numItemsSold,
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
|
value: formatValue( currency, 'number', numItemsSold ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
|
'Coupon',
|
|
|
|
|
'Coupons',
|
2020-02-14 02:23:21 +00:00
|
|
|
|
couponsCount,
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
|
value: formatValue( currency, 'number', couponsCount ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
|
label: __( 'net sales', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
|
value: formatAmount( netRevenue ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 21:29:25 +00:00
|
|
|
|
renderLinks( items = [] ) {
|
2018-10-18 20:45:59 +00:00
|
|
|
|
return items.map( ( item, i ) => (
|
2019-01-20 22:31:03 +00:00
|
|
|
|
<Link href={ item.href } key={ i } type="wc-admin">
|
2018-10-12 21:29:25 +00:00
|
|
|
|
{ item.label }
|
|
|
|
|
</Link>
|
2018-10-11 08:30:51 +00:00
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 21:29:25 +00:00
|
|
|
|
renderList( visibleItems, popoverItems ) {
|
|
|
|
|
return (
|
|
|
|
|
<Fragment>
|
|
|
|
|
{ this.renderLinks( visibleItems ) }
|
2020-02-14 02:23:21 +00:00
|
|
|
|
{ popoverItems.length > 1 && (
|
|
|
|
|
<ViewMoreList items={ this.renderLinks( popoverItems ) } />
|
|
|
|
|
) }
|
2018-10-12 21:29:25 +00:00
|
|
|
|
</Fragment>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
render() {
|
2019-03-21 03:25:05 +00:00
|
|
|
|
const { query, filters, advancedFilters } = this.props;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
|
|
|
|
return (
|
2018-12-03 03:40:57 +00:00
|
|
|
|
<ReportTable
|
|
|
|
|
endpoint="orders"
|
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
|
getRowsContent={ this.getRowsContent }
|
|
|
|
|
getSummary={ this.getSummary }
|
2020-03-31 15:08:40 +00:00
|
|
|
|
summaryFields={ [
|
|
|
|
|
'orders_count',
|
2020-06-18 17:43:56 +00:00
|
|
|
|
'total_customers',
|
2020-03-31 15:08:40 +00:00
|
|
|
|
'products',
|
|
|
|
|
'num_items_sold',
|
|
|
|
|
'coupons_count',
|
|
|
|
|
'net_revenue',
|
|
|
|
|
] }
|
2018-12-03 03:40:57 +00:00
|
|
|
|
query={ query }
|
2019-01-18 02:52:58 +00:00
|
|
|
|
tableQuery={ {
|
|
|
|
|
extended_info: true,
|
|
|
|
|
} }
|
2019-03-13 17:14:02 +00:00
|
|
|
|
title={ __( 'Orders', 'woocommerce-admin' ) }
|
2018-12-13 20:34:23 +00:00
|
|
|
|
columnPrefsKey="orders_report_columns"
|
2019-03-21 03:25:05 +00:00
|
|
|
|
filters={ filters }
|
|
|
|
|
advancedFilters={ advancedFilters }
|
2018-10-11 08:30:51 +00:00
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-10-29 07:57:05 +00:00
|
|
|
|
}
|
2020-04-02 21:54:38 +00:00
|
|
|
|
|
|
|
|
|
OrdersReportTable.contextType = CurrencyContext;
|
|
|
|
|
|
|
|
|
|
export default OrdersReportTable;
|