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

66 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' ) {
return reportTableData;
}
2019-06-17 04:38:42 +00:00
reportTableData.headers = [
...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
},
];
if (
! reportTableData.items ||
! reportTableData.items.data ||
! reportTableData.items.data.length
) {
return reportTableData;
}
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.rows = newRows;
2019-06-17 04:38:42 +00:00
return reportTableData;
}
);