Add coupons count to data store and hook up to summary numbers in orders report (https://github.com/woocommerce/woocommerce-admin/pull/1912)

* Change coupons sum to distinct coupon count in REST API

* Hook up unique coupon count to orders summary numbers

* Add coupons count to order segmenting

* Add coupons count to expected stats in order test and revenue tests
This commit is contained in:
Joshua T Flowers 2019-04-01 10:16:57 +08:00 committed by GitHub
parent 3cddc714c6
commit 42ddfc0108
11 changed files with 205 additions and 17 deletions

View File

@ -192,7 +192,7 @@ export default class OrdersReportTable extends Component {
num_returning_customers = 0, num_returning_customers = 0,
products = 0, products = 0,
num_items_sold = 0, num_items_sold = 0,
coupons = 0, coupons_count = 0,
net_revenue = 0, net_revenue = 0,
} = totals; } = totals;
return [ return [
@ -222,8 +222,8 @@ export default class OrdersReportTable extends Component {
value: numberFormat( num_items_sold ), value: numberFormat( num_items_sold ),
}, },
{ {
label: _n( 'coupon', 'coupons', coupons, 'woocommerce-admin' ), label: _n( 'coupon', 'coupons', coupons_count, 'woocommerce-admin' ),
value: numberFormat( coupons ), value: numberFormat( coupons_count ),
}, },
{ {
label: __( 'net revenue', 'woocommerce-admin' ), label: __( 'net revenue', 'woocommerce-admin' ),

View File

@ -180,8 +180,14 @@ class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Report
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,
), ),
'coupons' => array( 'coupons' => array(
'description' => __( 'Amount discounted by coupons', 'woocommerce-admin' ), 'description' => __( 'Amount discounted by coupons.', 'woocommerce-admin' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'coupons_count' => array(
'description' => __( 'Unique coupons count.', 'woocommerce-admin' ),
'type' => 'number', 'type' => 'number',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,

View File

@ -152,8 +152,14 @@ class WC_Admin_REST_Reports_Revenue_Stats_Controller extends WC_REST_Reports_Con
'indicator' => true, 'indicator' => true,
'format' => 'currency', 'format' => 'currency',
), ),
'coupons' => array( 'coupons' => array(
'description' => __( 'Total of coupons.', 'woocommerce-admin' ), 'description' => __( 'Amount discounted by coupons.', 'woocommerce-admin' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'coupons_count' => array(
'description' => __( 'Unique coupons count.', 'woocommerce-admin' ),
'type' => 'number', 'type' => 'number',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,

View File

@ -38,6 +38,7 @@ class WC_Admin_Reports_Orders_Stats_Query extends WC_Admin_Reports_Query {
'avg_items_per_order', 'avg_items_per_order',
'num_items_sold', 'num_items_sold',
'coupons', 'coupons',
'coupons_count',
'num_returning_customers', 'num_returning_customers',
'num_new_customers', 'num_new_customers',
), ),

View File

@ -24,6 +24,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
'num_items_sold' => "SUM($products_table.product_qty) as num_items_sold", 'num_items_sold' => "SUM($products_table.product_qty) as num_items_sold",
'gross_revenue' => "SUM($products_table.product_gross_revenue) AS gross_revenue", 'gross_revenue' => "SUM($products_table.product_gross_revenue) AS gross_revenue",
'coupons' => "SUM($products_table.coupon_amount) AS coupons", 'coupons' => "SUM($products_table.coupon_amount) AS coupons",
'coupons_count' => 'COUNT( DISTINCT( coupon_lookup_left_join.coupon_id ) ) AS coupons_count',
'refunds' => "SUM($products_table.refund_amount) AS refunds", 'refunds' => "SUM($products_table.refund_amount) AS refunds",
'taxes' => "SUM($products_table.tax_amount) AS taxes", 'taxes' => "SUM($products_table.tax_amount) AS taxes",
'shipping' => "SUM($products_table.shipping_amount) AS shipping", 'shipping' => "SUM($products_table.shipping_amount) AS shipping",
@ -66,6 +67,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
'num_items_sold' => "SUM($order_stats_table.num_items_sold) as num_items_sold", 'num_items_sold' => "SUM($order_stats_table.num_items_sold) as num_items_sold",
'gross_revenue' => "SUM($order_stats_table.gross_total) AS gross_revenue", 'gross_revenue' => "SUM($order_stats_table.gross_total) AS gross_revenue",
'coupons' => "SUM($order_stats_table.coupon_total) AS coupons", 'coupons' => "SUM($order_stats_table.coupon_total) AS coupons",
'coupons_count' => 'COUNT( DISTINCT(coupon_lookup_left_join.coupon_id) ) AS coupons_count',
'refunds' => "SUM($order_stats_table.refund_total) AS refunds", 'refunds' => "SUM($order_stats_table.refund_total) AS refunds",
'taxes' => "SUM($order_stats_table.tax_total) AS taxes", 'taxes' => "SUM($order_stats_table.tax_total) AS taxes",
'shipping' => "SUM($order_stats_table.shipping_total) AS shipping", 'shipping' => "SUM($order_stats_table.shipping_total) AS shipping",
@ -348,6 +350,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
$unique_orders_table = 'uniq_orders'; $unique_orders_table = 'uniq_orders';
$segmenting_from = "LEFT JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup_left_join ON ($table_name.order_id = coupon_lookup_left_join.order_id) ";
$segmenting_where = ''; $segmenting_where = '';
// Product, variation, and category are bound to product, so here product segmenting table is required, // Product, variation, and category are bound to product, so here product segmenting table is required,
@ -359,7 +362,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ), 'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ),
'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ), 'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ),
); );
$segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)"; $segmenting_from .= "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
$segmenting_groupby = $product_segmenting_table . '.product_id'; $segmenting_groupby = $product_segmenting_table . '.product_id';
$segmenting_dimension_name = 'product_id'; $segmenting_dimension_name = 'product_id';
@ -373,7 +376,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ), 'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ),
'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ), 'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ),
); );
$segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)"; $segmenting_from .= "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
$segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}"; $segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}";
$segmenting_groupby = $product_segmenting_table . '.variation_id'; $segmenting_groupby = $product_segmenting_table . '.variation_id';
$segmenting_dimension_name = 'variation_id'; $segmenting_dimension_name = 'variation_id';
@ -384,7 +387,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ), 'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ),
'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ), 'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ),
); );
$segmenting_from = " $segmenting_from .= "
INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id) INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)
LEFT JOIN {$wpdb->prefix}term_relationships ON {$product_segmenting_table}.product_id = {$wpdb->prefix}term_relationships.object_id LEFT JOIN {$wpdb->prefix}term_relationships ON {$product_segmenting_table}.product_id = {$wpdb->prefix}term_relationships.object_id
RIGHT JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id RIGHT JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id
@ -400,7 +403,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
'coupons' => 'SUM(coupon_lookup.discount_amount) AS coupons', 'coupons' => 'SUM(coupon_lookup.discount_amount) AS coupons',
); );
$segmenting_selections = $this->segment_selections_orders( $table_name, $coupon_override ); $segmenting_selections = $this->segment_selections_orders( $table_name, $coupon_override );
$segmenting_from = " $segmenting_from .= "
INNER JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup ON ($table_name.order_id = coupon_lookup.order_id) INNER JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup ON ($table_name.order_id = coupon_lookup.order_id)
"; ";
$segmenting_groupby = 'coupon_lookup.coupon_id'; $segmenting_groupby = 'coupon_lookup.coupon_id';
@ -408,7 +411,6 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
$segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
} elseif ( 'customer_type' === $this->query_args['segmentby'] ) { } elseif ( 'customer_type' === $this->query_args['segmentby'] ) {
$segmenting_selections = $this->segment_selections_orders( $table_name ); $segmenting_selections = $this->segment_selections_orders( $table_name );
$segmenting_from = '';
$segmenting_groupby = "$table_name.returning_customer"; $segmenting_groupby = "$table_name.returning_customer";
$segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );

View File

@ -40,6 +40,7 @@ class WC_Admin_Reports_Revenue_Query extends WC_Admin_Reports_Query {
'num_items_sold', 'num_items_sold',
'gross_revenue', 'gross_revenue',
'coupons', 'coupons',
'coupons_count',
'refunds', 'refunds',
'taxes', 'taxes',
'shipping', 'shipping',

View File

@ -36,6 +36,7 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'num_items_sold' => 'intval', 'num_items_sold' => 'intval',
'gross_revenue' => 'floatval', 'gross_revenue' => 'floatval',
'coupons' => 'floatval', 'coupons' => 'floatval',
'coupons_count' => 'intval',
'refunds' => 'floatval', 'refunds' => 'floatval',
'taxes' => 'floatval', 'taxes' => 'floatval',
'shipping' => 'floatval', 'shipping' => 'floatval',
@ -58,6 +59,7 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'num_items_sold' => 'SUM(num_items_sold) as num_items_sold', 'num_items_sold' => 'SUM(num_items_sold) as num_items_sold',
'gross_revenue' => 'SUM(gross_total) AS gross_revenue', 'gross_revenue' => 'SUM(gross_total) AS gross_revenue',
'coupons' => 'SUM(coupon_total) AS coupons', 'coupons' => 'SUM(coupon_total) AS coupons',
'coupons_count' => 'COUNT(DISTINCT coupon_id) as coupons_count',
'refunds' => 'SUM(refund_total) AS refunds', 'refunds' => 'SUM(refund_total) AS refunds',
'taxes' => 'SUM(tax_total) AS taxes', 'taxes' => 'SUM(tax_total) AS taxes',
'shipping' => 'SUM(shipping_total) AS shipping', 'shipping' => 'SUM(shipping_total) AS shipping',
@ -231,6 +233,9 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
FROM FROM
{$table_name} {$table_name}
{$totals_query['from_clause']} {$totals_query['from_clause']}
LEFT JOIN
{$wpdb->prefix}wc_order_coupon_lookup
ON {$wpdb->prefix}wc_order_coupon_lookup.order_id = {$wpdb->prefix}wc_order_stats.order_id
WHERE WHERE
1=1 1=1
{$totals_query['where_time_clause']} {$totals_query['where_time_clause']}
@ -253,6 +258,9 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
FROM FROM
{$table_name} {$table_name}
{$intervals_query['from_clause']} {$intervals_query['from_clause']}
LEFT JOIN
{$wpdb->prefix}wc_order_coupon_lookup
ON {$wpdb->prefix}wc_order_coupon_lookup.order_id = {$wpdb->prefix}wc_order_stats.order_id
WHERE WHERE
1=1 1=1
{$intervals_query['where_time_clause']} {$intervals_query['where_time_clause']}
@ -277,12 +285,15 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
$intervals = $wpdb->get_results( $intervals = $wpdb->get_results(
"SELECT "SELECT
MAX(date_created) AS datetime_anchor, MAX({$table_name}.date_created) AS datetime_anchor,
{$intervals_query['select_clause']} AS time_interval {$intervals_query['select_clause']} AS time_interval
{$selections} {$selections}
FROM FROM
{$table_name} {$table_name}
{$intervals_query['from_clause']} {$intervals_query['from_clause']}
LEFT JOIN
{$wpdb->prefix}wc_order_coupon_lookup
ON {$wpdb->prefix}wc_order_coupon_lookup.order_id = {$wpdb->prefix}wc_order_stats.order_id
WHERE WHERE
1=1 1=1
{$intervals_query['where_time_clause']} {$intervals_query['where_time_clause']}

View File

@ -94,13 +94,14 @@ class WC_Tests_API_Reports_Orders_Stats extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'intervals', $properties ); $this->assertArrayHasKey( 'intervals', $properties );
$totals = $properties['totals']['properties']; $totals = $properties['totals']['properties'];
$this->assertEquals( 10, count( $totals ) ); $this->assertEquals( 11, count( $totals ) );
$this->assertArrayHasKey( 'net_revenue', $totals ); $this->assertArrayHasKey( 'net_revenue', $totals );
$this->assertArrayHasKey( 'avg_order_value', $totals ); $this->assertArrayHasKey( 'avg_order_value', $totals );
$this->assertArrayHasKey( 'orders_count', $totals ); $this->assertArrayHasKey( 'orders_count', $totals );
$this->assertArrayHasKey( 'avg_items_per_order', $totals ); $this->assertArrayHasKey( 'avg_items_per_order', $totals );
$this->assertArrayHasKey( 'num_items_sold', $totals ); $this->assertArrayHasKey( 'num_items_sold', $totals );
$this->assertArrayHasKey( 'coupons', $totals ); $this->assertArrayHasKey( 'coupons', $totals );
$this->assertArrayHasKey( 'coupons_count', $totals );
$this->assertArrayHasKey( 'num_returning_customers', $totals ); $this->assertArrayHasKey( 'num_returning_customers', $totals );
$this->assertArrayHasKey( 'num_new_customers', $totals ); $this->assertArrayHasKey( 'num_new_customers', $totals );
$this->assertArrayHasKey( 'products', $totals ); $this->assertArrayHasKey( 'products', $totals );
@ -116,13 +117,14 @@ class WC_Tests_API_Reports_Orders_Stats extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'subtotals', $intervals ); $this->assertArrayHasKey( 'subtotals', $intervals );
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties']; $subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
$this->assertEquals( 9, count( $subtotals ) ); $this->assertEquals( 10, count( $subtotals ) );
$this->assertArrayHasKey( 'net_revenue', $subtotals ); $this->assertArrayHasKey( 'net_revenue', $subtotals );
$this->assertArrayHasKey( 'avg_order_value', $subtotals ); $this->assertArrayHasKey( 'avg_order_value', $subtotals );
$this->assertArrayHasKey( 'orders_count', $subtotals ); $this->assertArrayHasKey( 'orders_count', $subtotals );
$this->assertArrayHasKey( 'avg_items_per_order', $subtotals ); $this->assertArrayHasKey( 'avg_items_per_order', $subtotals );
$this->assertArrayHasKey( 'num_items_sold', $subtotals ); $this->assertArrayHasKey( 'num_items_sold', $subtotals );
$this->assertArrayHasKey( 'coupons', $subtotals ); $this->assertArrayHasKey( 'coupons', $subtotals );
$this->assertArrayHasKey( 'coupons_count', $subtotals );
$this->assertArrayHasKey( 'num_returning_customers', $subtotals ); $this->assertArrayHasKey( 'num_returning_customers', $subtotals );
$this->assertArrayHasKey( 'num_new_customers', $subtotals ); $this->assertArrayHasKey( 'num_new_customers', $subtotals );
$this->assertArrayHasKey( 'segments', $subtotals ); $this->assertArrayHasKey( 'segments', $subtotals );

View File

@ -97,10 +97,11 @@ class WC_Tests_API_Reports_Revenue_Stats extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'intervals', $properties ); $this->assertArrayHasKey( 'intervals', $properties );
$totals = $properties['totals']['properties']; $totals = $properties['totals']['properties'];
$this->assertEquals( 10, count( $totals ) ); $this->assertEquals( 11, count( $totals ) );
$this->assertArrayHasKey( 'gross_revenue', $totals ); $this->assertArrayHasKey( 'gross_revenue', $totals );
$this->assertArrayHasKey( 'net_revenue', $totals ); $this->assertArrayHasKey( 'net_revenue', $totals );
$this->assertArrayHasKey( 'coupons', $totals ); $this->assertArrayHasKey( 'coupons', $totals );
$this->assertArrayHasKey( 'coupons_count', $totals );
$this->assertArrayHasKey( 'shipping', $totals ); $this->assertArrayHasKey( 'shipping', $totals );
$this->assertArrayHasKey( 'taxes', $totals ); $this->assertArrayHasKey( 'taxes', $totals );
$this->assertArrayHasKey( 'refunds', $totals ); $this->assertArrayHasKey( 'refunds', $totals );
@ -119,10 +120,11 @@ class WC_Tests_API_Reports_Revenue_Stats extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'subtotals', $intervals ); $this->assertArrayHasKey( 'subtotals', $intervals );
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties']; $subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
$this->assertEquals( 9, count( $subtotals ) ); $this->assertEquals( 10, count( $subtotals ) );
$this->assertArrayHasKey( 'gross_revenue', $subtotals ); $this->assertArrayHasKey( 'gross_revenue', $subtotals );
$this->assertArrayHasKey( 'net_revenue', $subtotals ); $this->assertArrayHasKey( 'net_revenue', $subtotals );
$this->assertArrayHasKey( 'coupons', $subtotals ); $this->assertArrayHasKey( 'coupons', $subtotals );
$this->assertArrayHasKey( 'coupons_count', $subtotals );
$this->assertArrayHasKey( 'shipping', $subtotals ); $this->assertArrayHasKey( 'shipping', $subtotals );
$this->assertArrayHasKey( 'taxes', $subtotals ); $this->assertArrayHasKey( 'taxes', $subtotals );
$this->assertArrayHasKey( 'refunds', $subtotals ); $this->assertArrayHasKey( 'refunds', $subtotals );

View File

@ -56,6 +56,7 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case {
'num_items_sold' => 4, 'num_items_sold' => 4,
'gross_revenue' => 97, 'gross_revenue' => 97,
'coupons' => 20, 'coupons' => 20,
'coupons_count' => 0,
'refunds' => 0, 'refunds' => 0,
'taxes' => 7, 'taxes' => 7,
'shipping' => 10, 'shipping' => 10,
@ -79,6 +80,7 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case {
'num_items_sold' => 4, 'num_items_sold' => 4,
'gross_revenue' => 97, 'gross_revenue' => 97,
'coupons' => 20, 'coupons' => 20,
'coupons_count' => 0,
'refunds' => 0, 'refunds' => 0,
'taxes' => 7, 'taxes' => 7,
'shipping' => 10, 'shipping' => 10,
@ -106,6 +108,7 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case {
'num_items_sold' => 4, 'num_items_sold' => 4,
'gross_revenue' => 97, 'gross_revenue' => 97,
'coupons' => 20, 'coupons' => 20,
'coupons_count' => 0,
'refunds' => 0, 'refunds' => 0,
'taxes' => 7, 'taxes' => 7,
'shipping' => 10, 'shipping' => 10,
@ -125,6 +128,7 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case {
'num_items_sold' => 4, 'num_items_sold' => 4,
'gross_revenue' => 97, 'gross_revenue' => 97,
'coupons' => 20, 'coupons' => 20,
'coupons_count' => 0,
'refunds' => 0, 'refunds' => 0,
'taxes' => 7, 'taxes' => 7,
'shipping' => 10, 'shipping' => 10,