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

289 lines
6.5 KiB
JavaScript
Raw Normal View History

2018-10-31 00:04:00 +00:00
/**
* External dependencies
*/
import { __, _n, _x } from '@wordpress/i18n';
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, getSetting } from '@woocommerce/wc-admin-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';
const manageStock = getSetting( 'manageStock', 'no' );
const stockStatuses = getSetting( '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-admin' ),
2018-10-31 00:04:00 +00:00
key: 'name',
required: true,
isLeftAligned: true,
},
{
label: __( 'SKU', 'woocommerce-admin' ),
key: 'sku',
hiddenByDefault: true,
isSortable: true,
},
2018-10-31 00:04:00 +00:00
{
label: __( 'Items sold', 'woocommerce-admin' ),
2018-10-31 00:04:00 +00:00
key: 'items_sold',
required: true,
defaultSort: true,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Net sales', 'woocommerce-admin' ),
screenReaderLabel: __( 'Net sales', 'woocommerce-admin' ),
key: 'net_revenue',
2018-10-31 00:04:00 +00:00
required: true,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Orders', 'woocommerce-admin' ),
2018-10-31 00:04:00 +00:00
key: 'orders_count',
isSortable: true,
isNumeric: true,
},
manageStock === 'yes'
? {
label: __( 'Status', 'woocommerce-admin' ),
key: 'stock_status',
}
: null,
manageStock === 'yes'
? {
label: __( 'Stock', 'woocommerce-admin' ),
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,
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: (
<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-admin'
) }
</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 {
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 [
{
label: _n(
'variation sold',
'variations sold',
variationsCount,
'woocommerce-admin'
),
value: formatValue( currency, 'number', variationsCount ),
},
{
label: _n(
'item sold',
'items sold',
itemsSold,
'woocommerce-admin'
),
value: formatValue( currency, 'number', itemsSold ),
},
{
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
label: __( 'net sales', 'woocommerce-admin' ),
value: formatAmount( netRevenue ),
},
{
label: _n(
'orders',
'orders',
ordersCount,
'woocommerce-admin'
),
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-admin'
),
placeholder: __(
'Search by variation name or SKU',
'woocommerce-admin'
),
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.products,
variations: query.variations,
} }
title={ __( 'Variations', 'woocommerce-admin' ) }
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;