woocommerce/plugins/woocommerce-admin/client/analytics/report/variations/table.js

318 lines
7.7 KiB
JavaScript
Raw Normal View History

2018-10-31 00:04:00 +00:00
/**
* External dependencies
*/
import { __, _n, _x } from '@wordpress/i18n';
import { applyFilters } from '@wordpress/hooks';
2018-10-31 00:04:00 +00:00
import { Component } from '@wordpress/element';
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
import { map } from 'lodash';
import { Link } from '@woocommerce/components';
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
import { formatValue } from '@woocommerce/number';
import { getAdminLink } from '@woocommerce/settings';
2018-10-31 00:04:00 +00:00
/**
* Internal dependencies
*/
import ReportTable from '../../components/report-table';
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
import { isLowStock } from '../products/utils';
import { CurrencyContext } from '../../../lib/currency-context';
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
import { getVariationName } from '../../../lib/async-requests';
import { getAdminSetting } from '~/utils/admin-settings';
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';
const manageStock = getAdminSetting( 'manageStock', 'no' );
const stockStatuses = getAdminSetting( 'stockStatuses', {} );
const getFullVariationName = ( rowData ) =>
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
getVariationName( rowData.extended_info || {} );
class VariationsReportTable extends Component {
constructor() {
super();
this.getHeadersContent = this.getHeadersContent.bind( this );
this.getRowsContent = this.getRowsContent.bind( this );
this.getSummary = this.getSummary.bind( this );
}
2018-10-31 00:04:00 +00:00
getHeadersContent() {
return [
{
label: __( 'Product / Variation title', 'woocommerce' ),
2018-10-31 00:04:00 +00:00
key: 'name',
required: true,
isLeftAligned: true,
},
{
label: __( 'SKU', 'woocommerce' ),
key: 'sku',
hiddenByDefault: true,
isSortable: true,
},
2018-10-31 00:04:00 +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,
},
{
label: __( 'Net sales', 'woocommerce' ),
screenReaderLabel: __( 'Net sales', 'woocommerce' ),
key: 'net_revenue',
2018-10-31 00:04:00 +00:00
required: true,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Orders', 'woocommerce' ),
2018-10-31 00:04:00 +00:00
key: 'orders_count',
isSortable: true,
isNumeric: true,
},
manageStock === 'yes'
? {
label: __( 'Status', 'woocommerce' ),
key: 'stock_status',
}
: null,
manageStock === 'yes'
? {
label: __( 'Stock', 'woocommerce' ),
key: 'stock',
isNumeric: true,
}
: null,
].filter( Boolean );
2018-10-31 00:04:00 +00:00
}
getRowsContent( data = [] ) {
const { query } = this.props;
const persistedQuery = getPersistedQuery( query );
const {
formatAmount,
formatDecimal: getCurrencyFormatDecimal,
getCurrencyConfig,
} = this.context;
2018-10-31 00:04:00 +00:00
return map( data, ( row ) => {
const {
items_sold: itemsSold,
net_revenue: netRevenue,
orders_count: ordersCount,
product_id: productId,
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
variation_id: variationId,
} = row;
const extendedInfo = row.extended_info || {};
const {
stock_status: stockStatus,
stock_quantity: stockQuantity,
low_stock_amount: lowStockAmount,
deleted,
sku,
} = extendedInfo;
const name = getFullVariationName( row );
const ordersLink = getNewPath(
persistedQuery,
'/analytics/orders',
{
filter: 'advanced',
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
variation_includes: variationId,
}
);
const editPostLink = getAdminLink(
`post.php?post=${ productId }&action=edit`
);
2018-10-31 00:04:00 +00:00
return [
{
display: deleted ? (
name + ' ' + __( '(Deleted)', 'woocommerce' )
) : (
<Link href={ editPostLink } type="wp-admin">
{ name }
</Link>
),
2018-10-31 00:04:00 +00:00
value: name,
},
{
display: sku,
value: sku,
},
2018-10-31 00:04:00 +00:00
{
display: formatValue(
getCurrencyConfig(),
'number',
itemsSold
),
value: itemsSold,
2018-10-31 00:04:00 +00:00
},
{
display: formatAmount( netRevenue ),
value: getCurrencyFormatDecimal( netRevenue ),
2018-10-31 00:04:00 +00:00
},
{
display: (
<Link href={ ordersLink } type="wc-admin">
{ ordersCount }
2018-10-31 00:04:00 +00:00
</Link>
),
value: ordersCount,
2018-10-31 00:04:00 +00:00
},
manageStock === 'yes'
? {
display: isLowStock(
stockStatus,
stockQuantity,
lowStockAmount
) ? (
<Link href={ editPostLink } type="wp-admin">
{ _x(
'Low',
'Indication of a low quantity',
'woocommerce'
) }
</Link>
) : (
stockStatuses[ stockStatus ]
),
value: stockStatuses[ stockStatus ],
}
: null,
manageStock === 'yes'
? {
display: stockQuantity,
value: stockQuantity,
}
: null,
].filter( Boolean );
2018-10-31 00:04:00 +00:00
} );
}
getSummary( totals ) {
const { query } = this.props;
const {
variations_count: variationsCount = 0,
items_sold: itemsSold = 0,
net_revenue: netRevenue = 0,
orders_count: ordersCount = 0,
} = totals;
const { formatAmount, getCurrencyConfig } = this.context;
const currency = getCurrencyConfig();
return [
{
/**
* 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
*
* @param {string} label Label used for the count.
* @param {string} variationsCount Number of variations.
* @param {Array} query Query parameters.
*/
label: applyFilters(
EXPERIMENTAL_VARIATIONS_REPORT_TABLE_SUMMARY_VARIATIONS_COUNT_LABEL_FILTER,
_n(
'variation sold',
'variations sold',
variationsCount,
'woocommerce'
),
variationsCount,
query
),
value: formatValue( currency, 'number', variationsCount ),
},
{
label: _n(
'item sold',
'items sold',
itemsSold,
'woocommerce'
),
value: formatValue( currency, 'number', itemsSold ),
},
{
label: __( 'net sales', 'woocommerce' ),
value: formatAmount( netRevenue ),
},
{
label: _n( 'orders', 'orders', ordersCount, 'woocommerce' ),
value: formatValue( currency, 'number', ordersCount ),
},
];
}
2018-10-31 00:04:00 +00:00
render() {
const {
advancedFilters,
baseSearchQuery,
filters,
isRequesting,
query,
} = this.props;
2018-10-31 00:04:00 +00:00
const labels = {
helpText: __(
'Check at least two variations below to compare',
'woocommerce'
),
placeholder: __( 'Search by variation name or SKU', 'woocommerce' ),
2018-10-31 00:04:00 +00:00
};
return (
<ReportTable
baseSearchQuery={ baseSearchQuery }
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
compareBy="variations"
compareParam="filter-variations"
endpoint="variations"
getHeadersContent={ this.getHeadersContent }
getRowsContent={ this.getRowsContent }
isRequesting={ isRequesting }
itemIdField="variation_id"
labels={ labels }
query={ query }
getSummary={ this.getSummary }
summaryFields={ [
'variations_count',
'items_sold',
'net_revenue',
'orders_count',
] }
tableQuery={ {
orderby: query.orderby || 'items_sold',
order: query.order || 'desc',
extended_info: true,
product_includes: query.product_includes,
variations: query.variations,
} }
/**
* 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.
* @param {Array} query Query parameters.
*/
title={ applyFilters(
EXPERIMENTAL_VARIATIONS_REPORT_TABLE_TITLE_FILTER,
__( 'Variations', 'woocommerce' ),
query
) }
columnPrefsKey="variations_report_columns"
2019-03-21 03:25:05 +00:00
filters={ filters }
advancedFilters={ advancedFilters }
2018-10-31 00:04:00 +00:00
/>
);
}
}
VariationsReportTable.contextType = CurrencyContext;
export default VariationsReportTable;