2019-12-05 22:38:26 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { addFilter } from '@wordpress/hooks';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a dropdown to a report.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Array} filters - set of filters in a report.
|
|
|
|
* @return {Array} amended set of filters.
|
2019-12-05 22:38:26 +00:00
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
const addCurrencyFilters = ( filters ) => {
|
2019-12-05 22:38:26 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: __( 'Currency', 'plugin-domain' ),
|
|
|
|
staticParams: [],
|
|
|
|
param: 'currency',
|
|
|
|
showFilters: () => true,
|
|
|
|
defaultValue: 'USD',
|
|
|
|
filters: [ ...( wcSettings.multiCurrency || [] ) ],
|
|
|
|
},
|
|
|
|
...filters,
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_revenue_report_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_orders_report_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_products_report_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_categories_report_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_coupons_report_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_taxes_report_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_dashboard_filters',
|
|
|
|
'plugin-domain',
|
|
|
|
addCurrencyFilters
|
|
|
|
);
|
2019-12-05 22:38:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a column to a report table. Include a header and
|
|
|
|
* manipulate each row to handle the added parameter.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Object} reportTableData - table data.
|
|
|
|
* @return {Object} - table data.
|
2019-12-05 22:38:26 +00:00
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
const addTableColumn = ( reportTableData ) => {
|
2019-12-05 22:38:26 +00:00
|
|
|
const includedReports = [
|
|
|
|
'revenue',
|
|
|
|
'products',
|
|
|
|
'orders',
|
|
|
|
'categories',
|
|
|
|
'coupons',
|
|
|
|
'taxes',
|
|
|
|
];
|
|
|
|
if ( ! includedReports.includes( reportTableData.endpoint ) ) {
|
|
|
|
return reportTableData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newHeaders = [
|
|
|
|
{
|
|
|
|
label: 'Currency',
|
|
|
|
key: 'currency',
|
|
|
|
},
|
|
|
|
...reportTableData.headers,
|
|
|
|
];
|
|
|
|
const newRows = reportTableData.rows.map( ( row, index ) => {
|
|
|
|
const item = reportTableData.items.data[ index ];
|
2020-04-02 21:54:38 +00:00
|
|
|
const currency =
|
|
|
|
reportTableData.endpoint === 'revenue'
|
|
|
|
? item.subtotals.currency
|
|
|
|
: item.currency;
|
2019-12-05 22:38:26 +00:00
|
|
|
const newRow = [
|
|
|
|
{
|
2020-04-02 21:54:38 +00:00
|
|
|
display: currency,
|
|
|
|
value: currency,
|
2019-12-05 22:38:26 +00:00
|
|
|
},
|
|
|
|
...row,
|
|
|
|
];
|
|
|
|
return newRow;
|
|
|
|
} );
|
|
|
|
|
|
|
|
reportTableData.headers = newHeaders;
|
|
|
|
reportTableData.rows = newRows;
|
|
|
|
|
|
|
|
return reportTableData;
|
|
|
|
};
|
|
|
|
|
|
|
|
addFilter( 'woocommerce_admin_report_table', 'plugin-domain', addTableColumn );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add 'currency' to the list of persisted queries so that the parameter remains
|
|
|
|
* when navigating from report to report.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Array} params - array of report slugs.
|
|
|
|
* @return {Array} - array of report slugs including 'currency'.
|
2019-12-05 22:38:26 +00:00
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
const persistQueries = ( params ) => {
|
2019-12-05 22:38:26 +00:00
|
|
|
params.push( 'currency' );
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_persisted_queries',
|
|
|
|
'plugin-domain',
|
|
|
|
persistQueries
|
|
|
|
);
|
2020-04-02 21:54:38 +00:00
|
|
|
|
|
|
|
const currencies = {
|
|
|
|
ZAR: {
|
|
|
|
code: 'ZAR',
|
|
|
|
symbol: 'R',
|
|
|
|
symbolPosition: 'left',
|
|
|
|
thousandSeparator: ' ',
|
|
|
|
decimalSeparator: ',',
|
|
|
|
precision: 2,
|
|
|
|
},
|
|
|
|
NZD: {
|
|
|
|
code: 'NZD',
|
|
|
|
symbol: '$NZ',
|
|
|
|
symbolPosition: 'left',
|
|
|
|
thousandSeparator: ',',
|
|
|
|
decimalSeparator: '.',
|
|
|
|
precision: 2,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateReportCurrencies = ( config, { currency } ) => {
|
|
|
|
if ( currency && currencies[ currency ] ) {
|
|
|
|
return currencies[ currency ];
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
};
|
|
|
|
|
|
|
|
addFilter(
|
|
|
|
'woocommerce_admin_report_currency',
|
|
|
|
'plugin-domain',
|
|
|
|
updateReportCurrencies
|
|
|
|
);
|