Hook up Categories table to endpoint
This commit is contained in:
parent
3c2cdc4322
commit
15bbbe739d
|
@ -19,6 +19,12 @@ import ReportTable from 'analytics/components/report-table';
|
|||
import { numberFormat } from 'lib/number';
|
||||
|
||||
export default class CategoriesReportTable extends Component {
|
||||
constructor( props ) {
|
||||
super( props );
|
||||
|
||||
this.getRowsContent = this.getRowsContent.bind( this );
|
||||
}
|
||||
|
||||
getHeadersContent() {
|
||||
return [
|
||||
{
|
||||
|
@ -60,23 +66,27 @@ export default class CategoriesReportTable extends Component {
|
|||
|
||||
getRowsContent( categories ) {
|
||||
return map( categories, category => {
|
||||
const { category_id, items_sold, gross_revenue, products_count, orders_count } = category;
|
||||
|
||||
// @TODO it should link to the Products report filtered by category
|
||||
const productsLink = (
|
||||
<Link
|
||||
href={ '/analytics/orders?filter=advanced&code_includes=' + category_id }
|
||||
type="wc-admin"
|
||||
>
|
||||
{ numberFormat( products_count ) }
|
||||
</Link>
|
||||
);
|
||||
const {
|
||||
category_id,
|
||||
items_sold,
|
||||
gross_revenue,
|
||||
products_count,
|
||||
orders_count,
|
||||
extended_info,
|
||||
} = category;
|
||||
const { name } = extended_info;
|
||||
|
||||
return [
|
||||
// @TODO it should be the category name, not the category ID
|
||||
{
|
||||
display: category_id,
|
||||
value: category_id,
|
||||
display: (
|
||||
<Link
|
||||
href={ 'term.php?taxonomy=product_cat&post_type=product&tag_ID=' + category_id }
|
||||
type="wp-admin"
|
||||
>
|
||||
{ name }
|
||||
</Link>
|
||||
),
|
||||
value: name,
|
||||
},
|
||||
{
|
||||
display: numberFormat( items_sold ),
|
||||
|
@ -87,7 +97,7 @@ export default class CategoriesReportTable extends Component {
|
|||
value: getCurrencyFormatDecimal( gross_revenue ),
|
||||
},
|
||||
{
|
||||
display: productsLink,
|
||||
display: numberFormat( products_count ),
|
||||
value: products_count,
|
||||
},
|
||||
{
|
||||
|
@ -135,6 +145,11 @@ export default class CategoriesReportTable extends Component {
|
|||
getSummary={ this.getSummary }
|
||||
itemIdField="category_id"
|
||||
query={ query }
|
||||
tableQuery={ {
|
||||
orderby: query.orderby || 'items_sold',
|
||||
order: query.order || 'desc',
|
||||
extended_info: true,
|
||||
} }
|
||||
title={ __( 'Categories', 'wc-admin' ) }
|
||||
columnPrefsKey="categories_report_columns"
|
||||
/>
|
||||
|
|
|
@ -20,7 +20,7 @@ export default {
|
|||
async getReportItems( ...args ) {
|
||||
const [ endpoint, query ] = args.slice( -2 );
|
||||
|
||||
const swaggerEndpoints = [ 'categories', 'coupons' ];
|
||||
const swaggerEndpoints = [ 'coupons' ];
|
||||
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
|
||||
try {
|
||||
const response = await fetch(
|
||||
|
|
|
@ -17,7 +17,7 @@ import { NAMESPACE } from '../../constants';
|
|||
import { SWAGGERNAMESPACE } from 'store/constants';
|
||||
|
||||
// TODO: Remove once swagger endpoints are phased out.
|
||||
const swaggerEndpoints = [ 'categories', 'coupons', 'customers' ];
|
||||
const swaggerEndpoints = [ 'coupons', 'customers' ];
|
||||
|
||||
const typeEndpointMap = {
|
||||
'report-items-query-orders': 'orders',
|
||||
|
|
|
@ -46,6 +46,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_REST_Reports_Contro
|
|||
$args['per_page'] = $request['per_page'];
|
||||
$args['orderby'] = $request['orderby'];
|
||||
$args['order'] = $request['order'];
|
||||
$args['extended_info'] = $request['extended_info'];
|
||||
$args['categories'] = (array) $request['categories'];
|
||||
$args['status_is'] = (array) $request['status_is'];
|
||||
$args['status_is_not'] = (array) $request['status_is_not'];
|
||||
|
@ -190,6 +191,14 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_REST_Reports_Contro
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'extended_info' => array(
|
||||
'name' => array(
|
||||
'type' => 'string',
|
||||
'readonly' => true,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'description' => __( 'Category name.', 'wc-admin' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -250,6 +259,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_REST_Reports_Contro
|
|||
'gross_revenue',
|
||||
'orders_count',
|
||||
'products_count',
|
||||
'category',
|
||||
),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
@ -296,6 +306,13 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_REST_Reports_Contro
|
|||
'type' => 'integer',
|
||||
),
|
||||
);
|
||||
$params['extended_info'] = array(
|
||||
'description' => __( 'Add additional piece of info about each category to the report.', 'wc-admin' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'sanitize_callback' => 'wc_string_to_bool',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
|
|
@ -148,6 +148,22 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
return array_slice( $data, $offset, $items_per_page );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enriches the category data.
|
||||
*
|
||||
* @param array $categories_data Categories data.
|
||||
* @param array $query_args Query parameters.
|
||||
*/
|
||||
protected function include_extended_info( &$categories_data, $query_args ) {
|
||||
foreach ( $categories_data as $key => $category_data ) {
|
||||
$extended_info = new ArrayObject();
|
||||
if ( $query_args['extended_info'] ) {
|
||||
$extended_info['name'] = get_the_category_by_ID( $category_data['category_id'] );
|
||||
}
|
||||
$categories_data[ $key ]['extended_info'] = $extended_info;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the report data based on parameters supplied by the user.
|
||||
*
|
||||
|
@ -171,6 +187,7 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
|
||||
'fields' => '*',
|
||||
'categories' => array(),
|
||||
'extended_info' => false,
|
||||
// This is not a parameter for products reports per se, but maybe we should restricts order statuses here, too?
|
||||
'order_status' => parent::get_report_order_statuses(),
|
||||
|
||||
|
@ -246,6 +263,8 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
$this->sort_records( $categories_data, $query_args['orderby'], $query_args['order'] );
|
||||
$categories_data = $this->page_records( $categories_data, $query_args['page'], $query_args['per_page'] );
|
||||
|
||||
$this->include_extended_info( $categories_data, $query_args );
|
||||
|
||||
$categories_data = array_map( array( $this, 'cast_numbers' ), $categories_data );
|
||||
$data = (object) array(
|
||||
'data' => $categories_data,
|
||||
|
|
|
@ -5,6 +5,13 @@
|
|||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reports Categories REST API Test Class
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Reports_Categories extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
|
@ -102,11 +109,12 @@ class WC_Tests_API_Reports_Categories extends WC_REST_Unit_Test_Case {
|
|||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 5, count( $properties ) );
|
||||
$this->assertEquals( 6, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'category_id', $properties );
|
||||
$this->assertArrayHasKey( 'items_sold', $properties );
|
||||
$this->assertArrayHasKey( 'gross_revenue', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'products_count', $properties );
|
||||
$this->assertArrayHasKey( 'extended_info', $properties );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue