Make variations and coupons endpoints return zero-value items (https://github.com/woocommerce/woocommerce-admin/pull/1765)
* Make categories endpoint return zero-value items * Rename PHP variable to make it more specific * Remove uneeded lines * Make variations and coupons endpoints return zero-value items * Remove second parameter from get_ids_table
This commit is contained in:
parent
d3230d8dae
commit
6c9b96f49a
|
@ -40,7 +40,6 @@ export default class VariationsReportTable extends Component {
|
|||
label: __( 'SKU', 'wc-admin' ),
|
||||
key: 'sku',
|
||||
hiddenByDefault: true,
|
||||
isSortable: true,
|
||||
},
|
||||
{
|
||||
label: __( 'Items Sold', 'wc-admin' ),
|
||||
|
@ -106,7 +105,7 @@ export default class VariationsReportTable extends Component {
|
|||
value: sku,
|
||||
},
|
||||
{
|
||||
display: items_sold,
|
||||
display: numberFormat( items_sold ),
|
||||
value: items_sold,
|
||||
},
|
||||
{
|
||||
|
|
|
@ -92,7 +92,7 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
|
||||
// Limit is left out here so that the grouping in code by PHP can be applied correctly.
|
||||
// This also needs to be put after the term_taxonomy JOIN so that we can match the correct term name.
|
||||
$sql_query_params = $this->get_order_by_params( $query_args, $sql_query_params, 'outer_from_clause', 'default_results.id' );
|
||||
$sql_query_params = $this->get_order_by_params( $query_args, $sql_query_params, 'outer_from_clause', 'default_results.category_id' );
|
||||
} else {
|
||||
$sql_query_params = $this->get_order_by_params( $query_args, $sql_query_params, 'from_clause', "{$wpdb->prefix}term_taxonomy.term_id" );
|
||||
}
|
||||
|
@ -258,13 +258,13 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
|
||||
if ( count( $included_categories ) > 0 ) {
|
||||
$fields = $this->get_fields( $query_args );
|
||||
$join_selections = $this->format_join_selections( array_merge( array( 'category_id' ), $fields ), 'category_id' );
|
||||
$ids_table = $this->get_ids_table( $included_categories );
|
||||
$join_selections = $this->format_join_selections( array_merge( array( 'category_id' ), $fields ), array( 'category_id' ) );
|
||||
$ids_table = $this->get_ids_table( $included_categories, 'category_id' );
|
||||
|
||||
$prefix = "SELECT {$join_selections} FROM (";
|
||||
$suffix = ") AS {$table_name}";
|
||||
$right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results
|
||||
ON default_results.id = {$table_name}.category_id";
|
||||
ON default_results.category_id = {$table_name}.category_id";
|
||||
} else {
|
||||
$prefix = '';
|
||||
$suffix = '';
|
||||
|
|
|
@ -58,6 +58,19 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
add_action( 'woocommerce_reports_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 5 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of ids of included coupons, based on query arguments from the user.
|
||||
*
|
||||
* @param array $query_args Parameters supplied by the user.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_included_coupons_array( $query_args ) {
|
||||
if ( isset( $query_args['coupons'] ) && is_array( $query_args['coupons'] ) && count( $query_args['coupons'] ) > 0 ) {
|
||||
return $query_args['coupons'];
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns comma separated ids of included coupons, based on query arguments from the user.
|
||||
*
|
||||
|
@ -65,12 +78,8 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
* @return string
|
||||
*/
|
||||
protected function get_included_coupons( $query_args ) {
|
||||
$included_coupons_str = '';
|
||||
|
||||
if ( isset( $query_args['coupons'] ) && is_array( $query_args['coupons'] ) && count( $query_args['coupons'] ) > 0 ) {
|
||||
$included_coupons_str = implode( ',', $query_args['coupons'] );
|
||||
}
|
||||
return $included_coupons_str;
|
||||
$included_coupons = $this->get_included_coupons_array( $query_args );
|
||||
return implode( ',', $included_coupons );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,11 +94,14 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
|
||||
$sql_query_params = $this->get_time_period_sql_params( $query_args, $order_coupon_lookup_table );
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_limit_sql_params( $query_args ) );
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_order_by_sql_params( $query_args ) );
|
||||
|
||||
$included_coupons = $this->get_included_coupons( $query_args );
|
||||
if ( $included_coupons ) {
|
||||
$sql_query_params['where_clause'] .= " AND {$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})";
|
||||
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_order_by_params( $query_args, 'outer_from_clause', 'default_results.coupon_id' ) );
|
||||
} else {
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_order_by_params( $query_args, 'from_clause', "{$order_coupon_lookup_table}.coupon_id" ) );
|
||||
}
|
||||
|
||||
$order_status_filter = $this->get_status_subquery( $query_args );
|
||||
|
@ -101,25 +113,27 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
return $sql_query_params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fills ORDER BY clause of SQL request based on user supplied parameters.
|
||||
*
|
||||
* @param array $query_args Parameters supplied by the user.
|
||||
* @param array $query_args Parameters supplied by the user.
|
||||
* @param string $from_arg Name of the FROM sql param.
|
||||
* @param string $id_cell ID cell identifier, like `table_name.id_column_name`.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_order_by_sql_params( $query_args ) {
|
||||
protected function get_order_by_params( $query_args, $from_arg, $id_cell ) {
|
||||
global $wpdb;
|
||||
$lookup_table = $wpdb->prefix . self::TABLE_NAME;
|
||||
$sql_query = array();
|
||||
$sql_query['from_clause'] = '';
|
||||
$sql_query['order_by_clause'] = '';
|
||||
$lookup_table = $wpdb->prefix . self::TABLE_NAME;
|
||||
$sql_query = array();
|
||||
$sql_query['from_clause'] = '';
|
||||
$sql_query['outer_from_clause'] = '';
|
||||
$sql_query['order_by_clause'] = '';
|
||||
if ( isset( $query_args['orderby'] ) ) {
|
||||
$sql_query['order_by_clause'] = $this->normalize_order_by( $query_args['orderby'] );
|
||||
}
|
||||
|
||||
if ( false !== strpos( $sql_query['order_by_clause'], '_coupons' ) ) {
|
||||
$sql_query['from_clause'] .= " JOIN {$wpdb->prefix}posts AS _coupons ON {$lookup_table}.coupon_id = _coupons.ID";
|
||||
$sql_query[ $from_arg ] .= " JOIN {$wpdb->prefix}posts AS _coupons ON {$id_cell} = _coupons.ID";
|
||||
}
|
||||
|
||||
if ( isset( $query_args['order'] ) ) {
|
||||
|
@ -236,30 +250,51 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
|
||||
$selections = $this->selected_columns( $query_args );
|
||||
$sql_query_params = $this->get_sql_query_params( $query_args );
|
||||
$included_coupons = $this->get_included_coupons_array( $query_args );
|
||||
|
||||
$db_records_count = (int) $wpdb->get_var(
|
||||
"SELECT COUNT(*) FROM (
|
||||
SELECT
|
||||
coupon_id
|
||||
FROM
|
||||
{$table_name}
|
||||
{$sql_query_params['from_clause']}
|
||||
WHERE
|
||||
1=1
|
||||
{$sql_query_params['where_time_clause']}
|
||||
{$sql_query_params['where_clause']}
|
||||
GROUP BY
|
||||
coupon_id
|
||||
) AS tt"
|
||||
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
||||
if ( count( $included_coupons ) > 0 ) {
|
||||
$total_results = count( $included_coupons );
|
||||
$total_pages = (int) ceil( $total_results / $sql_query_params['per_page'] );
|
||||
|
||||
$total_pages = (int) ceil( $db_records_count / $sql_query_params['per_page'] );
|
||||
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
|
||||
return $data;
|
||||
$fields = $this->get_fields( $query_args );
|
||||
$join_selections = $this->format_join_selections( $fields, array( 'coupon_id' ) );
|
||||
$ids_table = $this->get_ids_table( $included_coupons, 'coupon_id' );
|
||||
|
||||
$prefix = "SELECT {$join_selections} FROM (";
|
||||
$suffix = ") AS {$table_name}";
|
||||
$right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results
|
||||
ON default_results.coupon_id = {$table_name}.coupon_id";
|
||||
} else {
|
||||
$db_records_count = (int) $wpdb->get_var(
|
||||
"SELECT COUNT(*) FROM (
|
||||
SELECT
|
||||
coupon_id
|
||||
FROM
|
||||
{$table_name}
|
||||
{$sql_query_params['from_clause']}
|
||||
WHERE
|
||||
1=1
|
||||
{$sql_query_params['where_time_clause']}
|
||||
{$sql_query_params['where_clause']}
|
||||
GROUP BY
|
||||
coupon_id
|
||||
) AS tt"
|
||||
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
||||
|
||||
$total_results = $db_records_count;
|
||||
$total_pages = (int) ceil( $db_records_count / $sql_query_params['per_page'] );
|
||||
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$prefix = '';
|
||||
$suffix = '';
|
||||
$right_join = '';
|
||||
}
|
||||
|
||||
$coupon_data = $wpdb->get_results(
|
||||
"SELECT
|
||||
"${prefix}
|
||||
SELECT
|
||||
{$selections}
|
||||
FROM
|
||||
{$table_name}
|
||||
|
@ -270,6 +305,9 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
{$sql_query_params['where_clause']}
|
||||
GROUP BY
|
||||
coupon_id
|
||||
{$suffix}
|
||||
{$right_join}
|
||||
{$sql_query_params['outer_from_clause']}
|
||||
ORDER BY
|
||||
{$sql_query_params['order_by_clause']}
|
||||
{$sql_query_params['limit']}
|
||||
|
@ -286,7 +324,7 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
|
|||
$coupon_data = array_map( array( $this, 'cast_numbers' ), $coupon_data );
|
||||
$data = (object) array(
|
||||
'data' => $coupon_data,
|
||||
'total' => $db_records_count,
|
||||
'total' => $total_results,
|
||||
'pages' => $total_pages,
|
||||
'page_no' => (int) $query_args['page'],
|
||||
);
|
||||
|
|
|
@ -609,13 +609,19 @@ class WC_Admin_Reports_Data_Store {
|
|||
/**
|
||||
* Generates a virtual table given a list of IDs.
|
||||
*
|
||||
* @param array $ids Array of IDs.
|
||||
* @param array $ids Array of IDs.
|
||||
* @param array $id_field Name of the ID field.
|
||||
* @param array $other_values Other values that must be contained in the virtual table.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_ids_table( $ids ) {
|
||||
protected function get_ids_table( $ids, $id_field, $other_values = array() ) {
|
||||
$selects = array();
|
||||
foreach ( $ids as $id ) {
|
||||
array_push( $selects, "SELECT {$id} AS id" );
|
||||
$new_select = "SELECT {$id} AS {$id_field}";
|
||||
foreach ( $other_values as $key => $value ) {
|
||||
$new_select .= ", {$value} AS {$key}";
|
||||
}
|
||||
array_push( $selects, $new_select );
|
||||
}
|
||||
return join( ' UNION ', $selects );
|
||||
}
|
||||
|
@ -636,15 +642,17 @@ class WC_Admin_Reports_Data_Store {
|
|||
/**
|
||||
* Returns a comma separated list of the field names prepared to be used for a selection after a join with `default_results`.
|
||||
*
|
||||
* @param array $fields Array of fields name.
|
||||
* @param string $id_field Name of the column used as an identifier.
|
||||
* @param array $outer_selections Array of fields that are not selected in the inner query.
|
||||
* @param array $fields Array of fields name.
|
||||
* @param array $default_results_fields Fields to load from `default_results` table.
|
||||
* @param array $outer_selections Array of fields that are not selected in the inner query.
|
||||
* @return string
|
||||
*/
|
||||
protected function format_join_selections( $fields, $id_field, $outer_selections = array() ) {
|
||||
protected function format_join_selections( $fields, $default_results_fields, $outer_selections = array() ) {
|
||||
foreach ( $fields as $i => $field ) {
|
||||
if ( $field === $id_field ) {
|
||||
$fields[ $i ] = "default_results.id AS {$field}";
|
||||
foreach ( $default_results_fields as $default_results_field ) {
|
||||
if ( $field === $default_results_field ) {
|
||||
$fields[ $i ] = "default_results.{$field} AS {$field}";
|
||||
}
|
||||
}
|
||||
if ( in_array( $field, $outer_selections, true ) && array_key_exists( $field, $this->report_columns ) ) {
|
||||
$fields[ $i ] = $this->report_columns[ $field ];
|
||||
|
|
|
@ -158,7 +158,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
|
|||
|
||||
$included_products = $this->get_included_products( $query_args );
|
||||
if ( $included_products ) {
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_from_sql_params( $query_args, 'outer_from_clause', 'default_results.id' ) );
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_from_sql_params( $query_args, 'outer_from_clause', 'default_results.product_id' ) );
|
||||
$sql_query_params['where_clause'] .= " AND {$order_product_lookup_table}.product_id IN ({$included_products})";
|
||||
} else {
|
||||
$sql_query_params = array_merge( $sql_query_params, $this->get_from_sql_params( $query_args, 'from_clause', "{$order_product_lookup_table}.product_id" ) );
|
||||
|
@ -287,12 +287,12 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
|
|||
}
|
||||
|
||||
$fields = $this->get_fields( $query_args );
|
||||
$join_selections = $this->format_join_selections( $fields, 'product_id' );
|
||||
$join_selections = $this->format_join_selections( $fields, array( 'product_id' ) );
|
||||
$ids_table = $this->get_ids_table( $included_products, 'product_id' );
|
||||
$prefix = "SELECT {$join_selections} FROM (";
|
||||
$suffix = ") AS {$table_name}";
|
||||
$right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results
|
||||
ON default_results.id = {$table_name}.product_id";
|
||||
ON default_results.product_id = {$table_name}.product_id";
|
||||
} else {
|
||||
$db_records_count = (int) $wpdb->get_var(
|
||||
"SELECT COUNT(*) FROM (
|
||||
|
|
|
@ -93,7 +93,7 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
|
|||
}
|
||||
|
||||
if ( isset( $query_args['taxes'] ) && ! empty( $query_args['taxes'] ) ) {
|
||||
$sql_query['outer_from_clause'] .= " JOIN {$wpdb->prefix}woocommerce_tax_rates ON default_results.id = {$wpdb->prefix}woocommerce_tax_rates.tax_rate_id";
|
||||
$sql_query['outer_from_clause'] .= " JOIN {$wpdb->prefix}woocommerce_tax_rates ON default_results.tax_rate_id = {$wpdb->prefix}woocommerce_tax_rates.tax_rate_id";
|
||||
} else {
|
||||
$sql_query['from_clause'] .= " JOIN {$wpdb->prefix}woocommerce_tax_rates ON {$table_name}.tax_rate_id = {$wpdb->prefix}woocommerce_tax_rates.tax_rate_id";
|
||||
}
|
||||
|
@ -198,12 +198,12 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
|
|||
|
||||
$selections = $this->selected_columns( array( 'fields' => $inner_selections ) );
|
||||
$fields = $this->get_fields( $query_args );
|
||||
$join_selections = $this->format_join_selections( $fields, 'tax_rate_id', $outer_selections );
|
||||
$join_selections = $this->format_join_selections( $fields, array( 'tax_rate_id' ), $outer_selections );
|
||||
$ids_table = $this->get_ids_table( $query_args['taxes'], 'tax_rate_id' );
|
||||
$prefix = "SELECT {$join_selections} FROM (";
|
||||
$suffix = ") AS {$table_name}";
|
||||
$right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results
|
||||
ON default_results.id = {$table_name}.tax_rate_id";
|
||||
ON default_results.tax_rate_id = {$table_name}.tax_rate_id";
|
||||
} else {
|
||||
$db_records_count = (int) $wpdb->get_var(
|
||||
"SELECT COUNT(*) FROM (
|
||||
|
|
|
@ -81,10 +81,11 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
/**
|
||||
* Updates the database query with parameters used for Products report: categories and order status.
|
||||
*
|
||||
* @param array $query_args Query arguments supplied by the user.
|
||||
* @return array Array of parameters used for SQL query.
|
||||
* @param array $query_args Query arguments supplied by the user.
|
||||
* @param string $from_arg Name of the FROM sql param.
|
||||
* @return array Array of parameters used for SQL query.
|
||||
*/
|
||||
protected function get_sql_query_params( $query_args ) {
|
||||
protected function get_sql_query_params( $query_args, $from_arg ) {
|
||||
global $wpdb;
|
||||
$order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME;
|
||||
|
||||
|
@ -103,8 +104,9 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
}
|
||||
|
||||
$order_status_filter = $this->get_status_subquery( $query_args );
|
||||
$sql_query_params['outer_from_clause'] = '';
|
||||
if ( $order_status_filter ) {
|
||||
$sql_query_params['from_clause'] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
|
||||
$sql_query_params[ $from_arg ] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
|
||||
$sql_query_params['where_clause'] .= " AND ( {$order_status_filter} )";
|
||||
$sql_query_params['where_clause'] .= ' AND variation_id > 0';
|
||||
}
|
||||
|
@ -217,31 +219,60 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
);
|
||||
|
||||
$selections = $this->selected_columns( $query_args );
|
||||
$sql_query_params = $this->get_sql_query_params( $query_args );
|
||||
$included_products = $this->get_included_products_array( $query_args );
|
||||
|
||||
$db_records_count = (int) $wpdb->get_var(
|
||||
"SELECT COUNT(*) FROM (
|
||||
SELECT
|
||||
product_id
|
||||
FROM
|
||||
{$table_name}
|
||||
{$sql_query_params['from_clause']}
|
||||
WHERE
|
||||
1=1
|
||||
{$sql_query_params['where_time_clause']}
|
||||
{$sql_query_params['where_clause']}
|
||||
GROUP BY
|
||||
variation_id
|
||||
) AS tt"
|
||||
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
||||
if ( count( $included_products ) > 0 && count( $query_args['variations'] ) > 0 ) {
|
||||
$sql_query_params = $this->get_sql_query_params( $query_args, 'from_clause' );
|
||||
|
||||
$total_pages = (int) ceil( $db_records_count / $sql_query_params['per_page'] );
|
||||
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
|
||||
return $data;
|
||||
if ( 'date' === $query_args['orderby'] ) {
|
||||
$selections .= ", {$table_name}.date_created";
|
||||
}
|
||||
|
||||
$total_results = count( $query_args['variations'] );
|
||||
$total_pages = (int) ceil( $total_results / $sql_query_params['per_page'] );
|
||||
|
||||
$fields = $this->get_fields( $query_args );
|
||||
$join_selections = $this->format_join_selections( $fields, array( 'product_id', 'variation_id' ) );
|
||||
$ids_table = $this->get_ids_table( $query_args['variations'], 'variation_id', array( 'product_id' => $included_products[0] ) );
|
||||
|
||||
$prefix = "SELECT {$join_selections} FROM (";
|
||||
$suffix = ") AS {$table_name}";
|
||||
$right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results
|
||||
ON default_results.variation_id = {$table_name}.variation_id";
|
||||
} else {
|
||||
$sql_query_params = $this->get_sql_query_params( $query_args, 'from_clause' );
|
||||
|
||||
$db_records_count = (int) $wpdb->get_var(
|
||||
"SELECT COUNT(*) FROM (
|
||||
SELECT
|
||||
product_id
|
||||
FROM
|
||||
{$table_name}
|
||||
{$sql_query_params['from_clause']}
|
||||
WHERE
|
||||
1=1
|
||||
{$sql_query_params['where_time_clause']}
|
||||
{$sql_query_params['where_clause']}
|
||||
GROUP BY
|
||||
variation_id
|
||||
) AS tt"
|
||||
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
||||
|
||||
$total_results = $db_records_count;
|
||||
$total_pages = (int) ceil( $db_records_count / $sql_query_params['per_page'] );
|
||||
|
||||
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$prefix = '';
|
||||
$suffix = '';
|
||||
$right_join = '';
|
||||
}
|
||||
|
||||
$product_data = $wpdb->get_results(
|
||||
"SELECT
|
||||
"{$prefix}
|
||||
SELECT
|
||||
{$selections}
|
||||
FROM
|
||||
{$table_name}
|
||||
|
@ -252,6 +283,9 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
{$sql_query_params['where_clause']}
|
||||
GROUP BY
|
||||
variation_id
|
||||
{$suffix}
|
||||
{$right_join}
|
||||
{$sql_query_params['outer_from_clause']}
|
||||
ORDER BY
|
||||
{$sql_query_params['order_by_clause']}
|
||||
{$sql_query_params['limit']}
|
||||
|
@ -268,7 +302,7 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
$product_data = array_map( array( $this, 'cast_numbers' ), $product_data );
|
||||
$data = (object) array(
|
||||
'data' => $product_data,
|
||||
'total' => $db_records_count,
|
||||
'total' => $total_results,
|
||||
'pages' => $total_pages,
|
||||
'page_no' => (int) $query_args['page'],
|
||||
);
|
||||
|
|
|
@ -103,6 +103,62 @@ class WC_Tests_API_Reports_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'coupon', $coupon_reports[1]['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting basic reports with the `coupons` param.
|
||||
*/
|
||||
public function test_get_reports_coupons_param() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Simple product.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Coupons.
|
||||
$coupon_1_amount = 1; // by default in create_coupon.
|
||||
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
|
||||
|
||||
$coupon_2_amount = 2;
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
|
||||
$coupon_2->set_amount( $coupon_2_amount );
|
||||
$coupon_2->save();
|
||||
|
||||
// Order with 1 coupon.
|
||||
$order_1c = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_1c->set_status( 'completed' );
|
||||
$order_1c->apply_coupon( $coupon_1 );
|
||||
$order_1c->calculate_totals();
|
||||
$order_1c->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'coupons' => $coupon_1->get_id() . ',' . $coupon_2->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$coupon_reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $coupon_reports ) );
|
||||
|
||||
$this->assertEquals( $coupon_2->get_id(), $coupon_reports[0]['coupon_id'] );
|
||||
$this->assertEquals( 0, $coupon_reports[0]['amount'] );
|
||||
$this->assertEquals( 0, $coupon_reports[0]['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $coupon_reports[0] );
|
||||
$this->assertArrayHasKey( 'coupon', $coupon_reports[0]['_links'] );
|
||||
|
||||
$this->assertEquals( $coupon_1->get_id(), $coupon_reports[1]['coupon_id'] );
|
||||
$this->assertEquals( $coupon_1_amount, $coupon_reports[1]['amount'] );
|
||||
$this->assertEquals( 1, $coupon_reports[1]['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $coupon_reports[1] );
|
||||
$this->assertArrayHasKey( 'coupon', $coupon_reports[1]['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
|
|
|
@ -84,6 +84,70 @@ class WC_Tests_API_Reports_Variations extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'variation', $variation_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with the `variations` param.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_variations_param() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$variation = new WC_Product_Variation();
|
||||
$variation->set_name( 'Test Variation' );
|
||||
$variation->set_regular_price( 25 );
|
||||
$variation->set_attributes( array( 'color' => 'green' ) );
|
||||
$variation->save();
|
||||
|
||||
$variation_2 = new WC_Product_Variation();
|
||||
$variation_2->set_name( 'Test Variation 2' );
|
||||
$variation_2->set_regular_price( 100 );
|
||||
$variation_2->set_attributes( array( 'color' => 'red' ) );
|
||||
$variation_2->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $variation );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'product_includes' => $variation->get_parent_id(),
|
||||
'products' => $variation->get_parent_id(),
|
||||
'variations' => $variation->get_id() . ',' . $variation_2->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$variation_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $variation->get_id(), $variation_report['variation_id'] );
|
||||
$this->assertEquals( 4, $variation_report['items_sold'] );
|
||||
$this->assertEquals( 1, $variation_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $variation_report );
|
||||
$this->assertArrayHasKey( 'extended_info', $variation_report );
|
||||
$this->assertArrayHasKey( 'product', $variation_report['_links'] );
|
||||
$this->assertArrayHasKey( 'variation', $variation_report['_links'] );
|
||||
|
||||
$variation_report = next( $reports );
|
||||
|
||||
$this->assertEquals( $variation_2->get_id(), $variation_report['variation_id'] );
|
||||
$this->assertEquals( 0, $variation_report['items_sold'] );
|
||||
$this->assertEquals( 0, $variation_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $variation_report );
|
||||
$this->assertArrayHasKey( 'extended_info', $variation_report );
|
||||
$this->assertArrayHasKey( 'product', $variation_report['_links'] );
|
||||
$this->assertArrayHasKey( 'variation', $variation_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue