woocommerce/plugins/woocommerce-admin/docs/examples/extensions/table-column/js/index.js

63 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-06-17 04:38:42 +00:00
/**
* External dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { Rating } from '@woocommerce/components';
addFilter(
'woocommerce_admin_report_table',
'plugin-domain',
( reportTableData ) => {
if (
reportTableData.endpoint !== 'products' ||
! reportTableData.items ||
! reportTableData.items.data ||
! reportTableData.items.data.length
) {
return reportTableData;
}
2019-06-17 04:38:42 +00:00
const newHeaders = [
...reportTableData.headers,
2019-06-17 04:38:42 +00:00
{
label: 'ID',
key: 'product_id',
2019-06-17 04:38:42 +00:00
},
{
label: 'Rating',
key: 'product_rating',
2019-06-17 04:38:42 +00:00
},
];
const newRows = reportTableData.rows.map( ( row, index ) => {
const product = reportTableData.items.data[ index ];
const newRow = [
...row,
// product_id is already returned in the response for productData.
{
display: product.product_id,
value: product.product_id,
},
// average_rating can be found on extended_info on productData.
{
display: (
<Rating
rating={ Number(
product.extended_info.average_rating
) }
totalStars={ 5 }
/>
),
value: product.extended_info.average_rating,
},
];
return newRow;
} );
2019-06-17 04:38:42 +00:00
reportTableData.headers = newHeaders;
reportTableData.rows = newRows;
2019-06-17 04:38:42 +00:00
return reportTableData;
}
);