Added extended info.

This commit is contained in:
Peter Fabian 2018-12-10 16:39:20 +01:00 committed by Joshua Flowers
parent 5eee869a74
commit a346f13beb
3 changed files with 129 additions and 28 deletions

View File

@ -46,7 +46,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['code'] = (array) $request['code'];
$args['extended_info'] = $request['extended_info'];
return $args;
}
@ -167,6 +167,45 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'extended_info' => array(
'code' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon code.', 'wc-admin' ),
),
'date_created' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon creation date.', 'wc-admin' ),
),
'date_created_gmt' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon creation date in GMT.', 'wc-admin' ),
),
'date_expires' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon expiration date.', 'wc-admin' ),
),
'date_expires_gmt' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon expiration date in GMT.', 'wc-admin' ),
),
'discount_type' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'enum' => array_keys( wc_get_coupon_types() ),
'description' => __( 'Coupon discount type.', 'wc-admin' ),
),
),
),
);
@ -251,6 +290,13 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
'type' => 'string',
),
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each coupon to the report.', 'wc-admin' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}

View File

@ -86,6 +86,54 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
return $sql_query_params;
}
/**
* Enriches the coupon data with extra attributes.
*
* @param array $coupon_data Coupon data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$coupon_data, $query_args ) {
if ( $query_args['extended_info'] ) {
foreach ( $coupon_data as $idx => $coupon_datum ) {
$coupon_id = $coupon_datum['coupon_id'];
$coupon = new WC_Coupon( $coupon_id );
$gmt_timzone = new DateTimeZone( 'UTC' );
$date_expires = $coupon->get_date_expires();
if ( null === $date_expires ) {
$date_expires = '';
$date_expires_gmt = '';
} else {
$date_expires = $date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_expires_gmt = new DateTime( $date_expires );
$date_expires_gmt->setTimezone( $gmt_timzone );
$date_expires_gmt = $date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$date_created = $coupon->get_date_created();
if ( null === $date_created ) {
$date_created = '';
$date_created_gmt = '';
} else {
$date_created = $date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_created_gmt = new DateTime( $date_created );
$date_created_gmt->setTimezone( $gmt_timzone );
$date_created_gmt = $date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$coupon_data[ $idx ]['extended_info'] = array(
'code' => $coupon->get_code(),
'date_created' => $date_created,
'date_created_gmt' => $date_created_gmt,
'date_expires' => $date_expires,
'date_expires_gmt' => $date_expires_gmt,
'discount_type' => $coupon->get_discount_type(),
);
}
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
@ -109,6 +157,7 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'fields' => '*',
'code' => array(),
'extended_info' => false,
// This is not a parameter for coupons reports per se, but we want to only take into account selected order types.
'order_status' => parent::get_report_order_statuses(),
@ -173,6 +222,8 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
return $data;
}
$this->include_extended_info( $coupon_data, $query_args );
$coupon_data = array_map( array( $this, 'cast_numbers' ), $coupon_data );
$data = (object) array(
'data' => $coupon_data,

View File

@ -294,6 +294,10 @@ class WC_Admin_Reports_Data_Store {
$retyped_array = array();
$column_types = apply_filters( 'woocommerce_rest_reports_column_types', $this->column_types, $array );
foreach ( $array as $column_name => $value ) {
if ( is_array( $value ) ) {
$value = $this->cast_numbers( $value );
}
if ( isset( $column_types[ $column_name ] ) ) {
$retyped_array[ $column_name ] = $column_types[ $column_name ]( $value );
} else {