2018-10-31 00:04:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-12-03 22:40:21 +00:00
|
|
|
import { __, _n, _x } from '@wordpress/i18n';
|
2022-03-14 21:06:05 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2018-10-31 00:04:00 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2020-09-25 13:57:48 +00:00
|
|
|
import { map } from 'lodash';
|
2018-11-26 14:01:20 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
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';
|
2023-02-28 16:55:49 +00:00
|
|
|
import { CurrencyContext } from '@woocommerce/currency';
|
2018-10-31 00:04:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import ReportTable from '../../components/report-table';
|
2020-09-25 13:57:48 +00:00
|
|
|
import { isLowStock } from '../products/utils';
|
|
|
|
import { getVariationName } from '../../../lib/async-requests';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2018-11-26 14:01:20 +00:00
|
|
|
|
2022-03-14 21:06:05 +00:00
|
|
|
const EXPERIMENTAL_VARIATIONS_REPORT_TABLE_TITLE_FILTER =
|
|
|
|
'experimental_woocommerce_admin_variations_report_table_title';
|
|
|
|
const EXPERIMENTAL_VARIATIONS_REPORT_TABLE_SUMMARY_VARIATIONS_COUNT_LABEL_FILTER =
|
|
|
|
'experimental_woocommerce_admin_variations_report_table_summary_variations_count_label';
|
|
|
|
|
2022-01-06 12:53:30 +00:00
|
|
|
const manageStock = getAdminSetting( 'manageStock', 'no' );
|
|
|
|
const stockStatuses = getAdminSetting( 'stockStatuses', {} );
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2020-09-09 13:29:10 +00:00
|
|
|
const getFullVariationName = ( rowData ) =>
|
2020-09-25 13:57:48 +00:00
|
|
|
getVariationName( rowData.extended_info || {} );
|
2020-09-09 13:29:10 +00:00
|
|
|
|
2020-04-02 21:54:38 +00:00
|
|
|
class VariationsReportTable extends Component {
|
2018-11-26 14:01:20 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
2020-04-02 21:54:38 +00:00
|
|
|
this.getSummary = this.getSummary.bind( this );
|
2018-11-26 14:01:20 +00:00
|
|
|
}
|
2018-10-31 00:04:00 +00:00
|
|
|
|
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Product / Variation title', 'woocommerce' ),
|
2018-10-31 00:04:00 +00:00
|
|
|
key: 'name',
|
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
},
|
2019-01-31 01:27:15 +00:00
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'SKU', 'woocommerce' ),
|
2019-01-31 01:27:15 +00:00
|
|
|
key: 'sku',
|
|
|
|
hiddenByDefault: true,
|
2019-04-01 08:09:45 +00:00
|
|
|
isSortable: true,
|
2019-01-31 01:27:15 +00:00
|
|
|
},
|
2018-10-31 00:04:00 +00:00
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Items sold', 'woocommerce' ),
|
2018-10-31 00:04:00 +00:00
|
|
|
key: 'items_sold',
|
|
|
|
required: true,
|
|
|
|
defaultSort: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Net sales', 'woocommerce' ),
|
|
|
|
screenReaderLabel: __( 'Net sales', 'woocommerce' ),
|
2018-12-19 00:56:27 +00:00
|
|
|
key: 'net_revenue',
|
2018-10-31 00:04:00 +00:00
|
|
|
required: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Orders', 'woocommerce' ),
|
2018-10-31 00:04:00 +00:00
|
|
|
key: 'orders_count',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
2020-02-14 02:23:21 +00:00
|
|
|
manageStock === 'yes'
|
2019-03-21 05:42:07 +00:00
|
|
|
? {
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Status', 'woocommerce' ),
|
2019-03-21 05:42:07 +00:00
|
|
|
key: 'stock_status',
|
2020-02-14 02:23:21 +00:00
|
|
|
}
|
2019-03-21 05:42:07 +00:00
|
|
|
: null,
|
2020-02-14 02:23:21 +00:00
|
|
|
manageStock === 'yes'
|
2019-03-21 05:42:07 +00:00
|
|
|
? {
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Stock', 'woocommerce' ),
|
2019-03-21 05:42:07 +00:00
|
|
|
key: 'stock',
|
|
|
|
isNumeric: true,
|
2020-02-14 02:23:21 +00:00
|
|
|
}
|
2019-03-21 05:42:07 +00:00
|
|
|
: null,
|
|
|
|
].filter( Boolean );
|
2018-10-31 00:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( data = [] ) {
|
|
|
|
const { query } = this.props;
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
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,
|
2020-04-02 21:54:38 +00:00
|
|
|
} = this.context;
|
2018-10-31 00:04:00 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return map( data, ( row ) => {
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
|
|
|
items_sold: itemsSold,
|
|
|
|
net_revenue: netRevenue,
|
|
|
|
orders_count: ordersCount,
|
|
|
|
product_id: productId,
|
2020-09-25 13:57:48 +00:00
|
|
|
variation_id: variationId,
|
2020-04-02 21:54:38 +00:00
|
|
|
} = row;
|
2020-02-14 02:23:21 +00:00
|
|
|
const extendedInfo = row.extended_info || {};
|
|
|
|
const {
|
|
|
|
stock_status: stockStatus,
|
|
|
|
stock_quantity: stockQuantity,
|
|
|
|
low_stock_amount: lowStockAmount,
|
2022-02-07 18:13:27 +00:00
|
|
|
deleted,
|
2020-02-14 02:23:21 +00:00
|
|
|
sku,
|
|
|
|
} = extendedInfo;
|
2020-09-09 13:29:10 +00:00
|
|
|
const name = getFullVariationName( row );
|
2020-02-14 02:23:21 +00:00
|
|
|
const ordersLink = getNewPath(
|
|
|
|
persistedQuery,
|
|
|
|
'/analytics/orders',
|
|
|
|
{
|
|
|
|
filter: 'advanced',
|
2020-09-25 13:57:48 +00:00
|
|
|
variation_includes: variationId,
|
2020-02-14 02:23:21 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
const editPostLink = getAdminLink(
|
|
|
|
`post.php?post=${ productId }&action=edit`
|
|
|
|
);
|
2018-10-31 00:04:00 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
2022-02-07 18:13:27 +00:00
|
|
|
display: deleted ? (
|
2022-07-08 10:37:30 +00:00
|
|
|
name + ' ' + __( '(Deleted)', 'woocommerce' )
|
2022-02-07 18:13:27 +00:00
|
|
|
) : (
|
2018-11-26 02:58:19 +00:00
|
|
|
<Link href={ editPostLink } type="wp-admin">
|
|
|
|
{ name }
|
|
|
|
</Link>
|
|
|
|
),
|
2018-10-31 00:04:00 +00:00
|
|
|
value: name,
|
|
|
|
},
|
2019-01-31 01:27:15 +00:00
|
|
|
{
|
|
|
|
display: sku,
|
|
|
|
value: sku,
|
|
|
|
},
|
2018-10-31 00:04:00 +00:00
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatValue(
|
|
|
|
getCurrencyConfig(),
|
|
|
|
'number',
|
|
|
|
itemsSold
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: itemsSold,
|
2018-10-31 00:04:00 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( netRevenue ),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: getCurrencyFormatDecimal( netRevenue ),
|
2018-10-31 00:04:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: (
|
|
|
|
<Link href={ ordersLink } type="wc-admin">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ ordersCount }
|
2018-10-31 00:04:00 +00:00
|
|
|
</Link>
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: ordersCount,
|
2018-10-31 00:04:00 +00:00
|
|
|
},
|
2020-02-14 02:23:21 +00:00
|
|
|
manageStock === 'yes'
|
2019-03-21 05:42:07 +00:00
|
|
|
? {
|
2020-02-14 02:23:21 +00:00
|
|
|
display: isLowStock(
|
|
|
|
stockStatus,
|
|
|
|
stockQuantity,
|
|
|
|
lowStockAmount
|
|
|
|
) ? (
|
2019-03-21 05:42:07 +00:00
|
|
|
<Link href={ editPostLink } type="wp-admin">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ _x(
|
|
|
|
'Low',
|
|
|
|
'Indication of a low quantity',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
) }
|
2019-03-21 05:42:07 +00:00
|
|
|
</Link>
|
|
|
|
) : (
|
2020-02-14 02:23:21 +00:00
|
|
|
stockStatuses[ stockStatus ]
|
2019-03-21 05:42:07 +00:00
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: stockStatuses[ stockStatus ],
|
|
|
|
}
|
2019-03-21 05:42:07 +00:00
|
|
|
: null,
|
2020-02-14 02:23:21 +00:00
|
|
|
manageStock === 'yes'
|
2019-03-21 05:42:07 +00:00
|
|
|
? {
|
2020-02-14 02:23:21 +00:00
|
|
|
display: stockQuantity,
|
|
|
|
value: stockQuantity,
|
|
|
|
}
|
2019-03-21 05:42:07 +00:00
|
|
|
: null,
|
|
|
|
].filter( Boolean );
|
2018-10-31 00:04:00 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-11-26 02:58:19 +00:00
|
|
|
getSummary( totals ) {
|
2022-03-14 21:06:05 +00:00
|
|
|
const { query } = this.props;
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
variations_count: variationsCount = 0,
|
|
|
|
items_sold: itemsSold = 0,
|
|
|
|
net_revenue: netRevenue = 0,
|
|
|
|
orders_count: ordersCount = 0,
|
|
|
|
} = totals;
|
2020-06-17 23:33:40 +00:00
|
|
|
const { formatAmount, getCurrencyConfig } = this.context;
|
|
|
|
const currency = getCurrencyConfig();
|
2018-11-26 02:58:19 +00:00
|
|
|
return [
|
|
|
|
{
|
2022-03-14 21:06:05 +00:00
|
|
|
/**
|
|
|
|
* Experimental: Filter the label used for the number of variations in the report table summary.
|
|
|
|
*
|
|
|
|
* @filter experimental_woocommerce_admin_variations_report_table_summary_variations_count_label
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {string} label Label used for the count.
|
2022-03-14 21:06:05 +00:00
|
|
|
* @param {string} variationsCount Number of variations.
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Array} query Query parameters.
|
2022-03-14 21:06:05 +00:00
|
|
|
*/
|
|
|
|
label: applyFilters(
|
|
|
|
EXPERIMENTAL_VARIATIONS_REPORT_TABLE_SUMMARY_VARIATIONS_COUNT_LABEL_FILTER,
|
|
|
|
_n(
|
|
|
|
'variation sold',
|
|
|
|
'variations sold',
|
|
|
|
variationsCount,
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2022-03-14 21:06:05 +00:00
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
variationsCount,
|
2022-03-14 21:06:05 +00:00
|
|
|
query
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', variationsCount ),
|
2018-11-26 02:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
|
|
|
'item sold',
|
|
|
|
'items sold',
|
|
|
|
itemsSold,
|
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', itemsSold ),
|
2018-11-26 02:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'net sales', 'woocommerce' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( netRevenue ),
|
2018-11-26 02:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: _n( 'orders', 'orders', ordersCount, 'woocommerce' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', ordersCount ),
|
2018-11-26 02:58:19 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-31 00:04:00 +00:00
|
|
|
render() {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
advancedFilters,
|
|
|
|
baseSearchQuery,
|
|
|
|
filters,
|
|
|
|
isRequesting,
|
|
|
|
query,
|
|
|
|
} = this.props;
|
2018-10-31 00:04:00 +00:00
|
|
|
|
|
|
|
const labels = {
|
2020-02-14 02:23:21 +00:00
|
|
|
helpText: __(
|
|
|
|
'Check at least two variations below to compare',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
2022-03-30 09:00:04 +00:00
|
|
|
placeholder: __( 'Search by variation name or SKU', 'woocommerce' ),
|
2018-10-31 00:04:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2018-11-26 14:01:20 +00:00
|
|
|
<ReportTable
|
2019-03-18 04:23:08 +00:00
|
|
|
baseSearchQuery={ baseSearchQuery }
|
2020-09-25 13:57:48 +00:00
|
|
|
compareBy="variations"
|
|
|
|
compareParam="filter-variations"
|
2018-11-26 14:01:20 +00:00
|
|
|
endpoint="variations"
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
getRowsContent={ this.getRowsContent }
|
2019-02-27 14:43:34 +00:00
|
|
|
isRequesting={ isRequesting }
|
2019-02-05 18:12:58 +00:00
|
|
|
itemIdField="variation_id"
|
2018-11-26 14:01:20 +00:00
|
|
|
labels={ labels }
|
2018-11-22 23:12:12 +00:00
|
|
|
query={ query }
|
2018-11-26 02:58:19 +00:00
|
|
|
getSummary={ this.getSummary }
|
2020-03-31 15:08:40 +00:00
|
|
|
summaryFields={ [
|
|
|
|
'variations_count',
|
|
|
|
'items_sold',
|
|
|
|
'net_revenue',
|
|
|
|
'orders_count',
|
|
|
|
] }
|
2018-11-26 14:01:20 +00:00
|
|
|
tableQuery={ {
|
|
|
|
orderby: query.orderby || 'items_sold',
|
|
|
|
order: query.order || 'desc',
|
2018-11-26 02:58:19 +00:00
|
|
|
extended_info: true,
|
2022-01-07 04:25:44 +00:00
|
|
|
product_includes: query.product_includes,
|
2018-12-19 19:43:35 +00:00
|
|
|
variations: query.variations,
|
2018-11-26 14:01:20 +00:00
|
|
|
} }
|
2022-03-14 21:06:05 +00:00
|
|
|
/**
|
|
|
|
* Experimental: Filter the title used for the report table.
|
|
|
|
*
|
|
|
|
* @filter experimental_woocommerce_admin_variations_report_table_title
|
|
|
|
*
|
|
|
|
* @param {string} title Title used for the report table.
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Array} query Query parameters.
|
2022-03-14 21:06:05 +00:00
|
|
|
*/
|
|
|
|
title={ applyFilters(
|
|
|
|
EXPERIMENTAL_VARIATIONS_REPORT_TABLE_TITLE_FILTER,
|
2022-03-30 09:00:04 +00:00
|
|
|
__( 'Variations', 'woocommerce' ),
|
2022-03-14 21:06:05 +00:00
|
|
|
query
|
|
|
|
) }
|
2018-12-13 20:34:23 +00:00
|
|
|
columnPrefsKey="variations_report_columns"
|
2019-03-21 03:25:05 +00:00
|
|
|
filters={ filters }
|
2019-08-27 19:33:31 +00:00
|
|
|
advancedFilters={ advancedFilters }
|
2018-10-31 00:04:00 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 21:54:38 +00:00
|
|
|
|
|
|
|
VariationsReportTable.contextType = CurrencyContext;
|
|
|
|
|
|
|
|
export default VariationsReportTable;
|