3663 lines
152 KiB
PHP
3663 lines
152 KiB
PHP
<?php
|
|
/**
|
|
* Reports order stats tests.
|
|
*
|
|
* @package WooCommerce\Tests\Orders
|
|
*/
|
|
|
|
/**
|
|
* Class WC_Tests_Reports_Orders_Stats
|
|
*/
|
|
class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
|
|
|
/**
|
|
* Test the calculations and querying works correctly for the base case of 1 order.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_populate_and_query() {
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
// Populate all of the data.
|
|
$product = new WC_Product_Simple();
|
|
$product->set_name( 'Test Product' );
|
|
$product->set_regular_price( 25 );
|
|
$product->save();
|
|
|
|
$order = WC_Helper_Order::create_order( 1, $product );
|
|
$order->set_status( 'completed' );
|
|
$order->set_shipping_total( 10 );
|
|
$order->set_discount_total( 20 );
|
|
$order->set_discount_tax( 0 );
|
|
$order->set_cart_tax( 5 );
|
|
$order->set_shipping_tax( 2 );
|
|
$order->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax.
|
|
$order->save();
|
|
|
|
$refund = wc_create_refund(
|
|
array(
|
|
'amount' => 12,
|
|
'order_id' => $order->get_id(),
|
|
)
|
|
);
|
|
|
|
$data_store = new WC_Admin_Reports_Orders_Stats_Data_Store();
|
|
|
|
$start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() );
|
|
$end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() );
|
|
|
|
$args = array(
|
|
'interval' => 'hour',
|
|
'after' => $start_time,
|
|
'before' => $end_time,
|
|
);
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => 1,
|
|
'num_items_sold' => 4,
|
|
'avg_items_per_order' => 4,
|
|
'avg_order_value' => 68,
|
|
'gross_revenue' => 97,
|
|
'coupons' => 20,
|
|
'refunds' => 12,
|
|
'taxes' => 7,
|
|
'shipping' => 10,
|
|
'net_revenue' => 68,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 1,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => date( 'Y-m-d H', $order->get_date_created()->getOffsetTimestamp() ),
|
|
'date_start' => $start_time,
|
|
'date_start_gmt' => $start_time,
|
|
'date_end' => $end_time,
|
|
'date_end_gmt' => $end_time,
|
|
'subtotals' => array(
|
|
'gross_revenue' => 97,
|
|
'net_revenue' => 68,
|
|
'coupons' => 20,
|
|
'shipping' => 10,
|
|
'taxes' => 7,
|
|
'refunds' => 12,
|
|
'orders_count' => 1,
|
|
'num_items_sold' => 4,
|
|
'avg_items_per_order' => 4,
|
|
'avg_order_value' => 68,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 1,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
|
|
// Test retrieving the stats from the data store.
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $args ) ), true ) );
|
|
|
|
// Test retrieving the stats through the query class.
|
|
$query = new WC_Admin_Reports_Orders_Stats_Query( $args );
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'net_revenue' => 68,
|
|
'avg_order_value' => 68,
|
|
'orders_count' => 1,
|
|
'avg_items_per_order' => 4,
|
|
'num_items_sold' => 4,
|
|
'coupons' => 20,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 1,
|
|
'products' => '1',
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => date( 'Y-m-d H', $order->get_date_created()->getOffsetTimestamp() ),
|
|
'date_start' => $start_time,
|
|
'date_start_gmt' => $start_time,
|
|
'date_end' => $end_time,
|
|
'date_end_gmt' => $end_time,
|
|
'subtotals' => array(
|
|
'net_revenue' => 68,
|
|
'avg_order_value' => 68,
|
|
'orders_count' => 1,
|
|
'avg_items_per_order' => 4,
|
|
'num_items_sold' => 4,
|
|
'coupons' => 20,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 1,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $query->get_data() ), true ) );
|
|
}
|
|
|
|
/**
|
|
* Test the calculations and querying works correctly for the case of multiple orders.
|
|
*/
|
|
public function test_populate_and_query_multiple_intervals() {
|
|
global $wpdb;
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
// 2 different products.
|
|
$product_1_price = 25;
|
|
$product_1 = new WC_Product_Simple();
|
|
$product_1->set_name( 'Test Product' );
|
|
$product_1->set_regular_price( $product_1_price );
|
|
$product_1->save();
|
|
|
|
$product_2_price = 10;
|
|
$product_2 = new WC_Product_Simple();
|
|
$product_2->set_name( 'Test Product 2' );
|
|
$product_2->set_regular_price( $product_2_price );
|
|
$product_2->save();
|
|
|
|
$product_3_price = 13;
|
|
$product_3 = new WC_Product_Simple();
|
|
$product_3->set_name( 'Test Product 3' );
|
|
$product_3->set_regular_price( $product_3_price );
|
|
$product_3->save();
|
|
|
|
$product_4_price = 1;
|
|
$product_4 = new WC_Product_Simple();
|
|
$product_4->set_name( 'Test Product 4' );
|
|
$product_4->set_regular_price( $product_4_price );
|
|
$product_4->save();
|
|
|
|
// 2 different 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_status_1 = 'completed';
|
|
$order_status_2 = 'processing';
|
|
|
|
$customer_1 = WC_Helper_Customer::create_customer( 'cust_1', 'pwd_1', 'user_1@mail.com' );
|
|
$customer_2 = WC_Helper_Customer::create_customer( 'cust_2', 'pwd_2', 'user_2@mail.com' );
|
|
|
|
$order_1_time = time();
|
|
// One more order needs to fit into the same hour, but also be one second later than this one, so in case it's very end of the hour, shift order 1 one second towards past.
|
|
if ( 59 === $order_1_time % MINUTE_IN_SECONDS ) {
|
|
$order_1_time--;
|
|
}
|
|
$order_2_time = $order_1_time;
|
|
|
|
$this_['hour'] = array( 1, 2 );
|
|
$this_['day'] = array( 1, 2 );
|
|
$this_['week'] = array( 1, 2 );
|
|
$this_['month'] = array( 1, 2 );
|
|
$this_['year'] = array( 1, 2 );
|
|
|
|
$order_1_datetime = new DateTime();
|
|
$order_1_datetime = $order_1_datetime->setTimestamp( $order_1_time );
|
|
|
|
$order[1]['year'] = (int) $order_1_datetime->format( 'Y' );
|
|
$order[1]['month'] = (int) $order_1_datetime->format( 'm' );
|
|
$order[1]['week'] = (int) $order_1_datetime->format( 'W' );
|
|
$order[1]['day'] = (int) $order_1_datetime->format( 'd' );
|
|
$order[1]['hour'] = (int) $order_1_datetime->format( 'H' );
|
|
|
|
// same day, different hour.
|
|
$order_3_datetime = new DateTime();
|
|
$order_3_datetime = $order_3_datetime->setTimestamp( $order_1_time - HOUR_IN_SECONDS );
|
|
$order_3_time = $order_3_datetime->format( 'U' );
|
|
$order[3]['year'] = (int) $order_3_datetime->format( 'Y' );
|
|
$order[3]['month'] = (int) $order_3_datetime->format( 'm' );
|
|
$order[3]['week'] = (int) $order_3_datetime->format( 'W' );
|
|
$order[3]['day'] = (int) $order_3_datetime->format( 'd' );
|
|
|
|
// Previous day.
|
|
$order_4_datetime = new DateTime();
|
|
$order_4_datetime = $order_4_datetime->setTimestamp( $order_1_time - DAY_IN_SECONDS );
|
|
$order_4_time = $order_4_datetime->format( 'U' );
|
|
$order[4]['year'] = (int) $order_4_datetime->format( 'Y' );
|
|
$order[4]['month'] = (int) $order_4_datetime->format( 'm' );
|
|
$order[4]['week'] = (int) $order_4_datetime->format( 'W' );
|
|
$order[4]['day'] = (int) $order_4_datetime->format( 'd' );
|
|
|
|
// Previous week.
|
|
$order_5_datetime = new DateTime();
|
|
$order_5_datetime = $order_5_datetime->setTimestamp( $order_1_time - WEEK_IN_SECONDS );
|
|
$order_5_time = $order_5_datetime->format( 'U' );
|
|
$order[5]['year'] = (int) $order_5_datetime->format( 'Y' );
|
|
$order[5]['month'] = (int) $order_5_datetime->format( 'm' );
|
|
$order[5]['week'] = (int) $order_5_datetime->format( 'W' );
|
|
$order[5]['day'] = (int) $order_5_datetime->format( 'd' );
|
|
|
|
// Previous month.
|
|
$order_6_datetime = new DateTime();
|
|
$order_6_datetime = $order_6_datetime->setTimestamp( $order_1_time - MONTH_IN_SECONDS );
|
|
$order_6_time = $order_6_datetime->format( 'U' );
|
|
$order[6]['year'] = (int) $order_6_datetime->format( 'Y' );
|
|
$order[6]['month'] = (int) $order_6_datetime->format( 'm' );
|
|
$order[6]['week'] = (int) $order_6_datetime->format( 'W' );
|
|
$order[6]['day'] = (int) $order_6_datetime->format( 'd' );
|
|
|
|
// Previous year.
|
|
$order_7_datetime = new DateTime();
|
|
$order_7_datetime = $order_7_datetime->setTimestamp( $order_1_time - YEAR_IN_SECONDS );
|
|
$order_7_time = $order_7_datetime->format( 'U' );
|
|
$order[7]['year'] = (int) $order_7_datetime->format( 'Y' );
|
|
$order[7]['month'] = (int) $order_7_datetime->format( 'm' );
|
|
$order[7]['week'] = (int) $order_7_datetime->format( 'W' );
|
|
$order[7]['day'] = (int) $order_7_datetime->format( 'd' );
|
|
|
|
foreach ( array( 3, 4, 5, 6, 7 ) as $order_no ) {
|
|
if ( $order[ $order_no ]['day'] === $order[1]['day'] && $order[ $order_no ]['month'] === $order[1]['month'] && $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$this_['day'][] = $order_no;
|
|
}
|
|
if ( $order[ $order_no ]['week'] === $order[1]['week'] && $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$this_['week'][] = $order_no;
|
|
}
|
|
if ( $order[ $order_no ]['month'] === $order[1]['month'] && $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$this_['month'][] = $order_no;
|
|
}
|
|
if ( $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$this_['year'][] = $order_no;
|
|
}
|
|
}
|
|
|
|
$orders = array();
|
|
// 2 different order statuses, plus new vs returning customer
|
|
$qty_per_product = 4; // Hardcoded in WC_Helper_Order::create_order.
|
|
|
|
foreach ( array( $product_1, $product_2, $product_3 ) as $product ) {
|
|
foreach ( array( null, $coupon_1, $coupon_2 ) as $coupon ) {
|
|
foreach ( array( $order_status_1, $order_status_2 ) as $order_status ) {
|
|
foreach ( array( $customer_1, $customer_2 ) as $customer ) {
|
|
foreach (
|
|
array(
|
|
$order_1_time,
|
|
$order_2_time,
|
|
) as $order_time
|
|
) { // As there are no tests for different timeframes, ignore these for now: $order_3_time, $order_4_time, $order_5_time, $order_6_time, $order_7_time
|
|
// One order with only 1 product.
|
|
$order = WC_Helper_Order::create_order( $customer->get_id(), $product );
|
|
$order->set_date_created( $order_time );
|
|
$order->set_status( $order_status );
|
|
|
|
if ( $coupon ) {
|
|
$order->apply_coupon( $coupon );
|
|
}
|
|
|
|
$order->calculate_totals();
|
|
$order->save();
|
|
|
|
$orders[] = $order;
|
|
|
|
// One order with 2 products: product_4 and selected product.
|
|
$order_2 = WC_Helper_Order::create_order( $customer->get_id(), $product_4 );
|
|
|
|
$item = new WC_Order_Item_Product();
|
|
$item->set_props(
|
|
array(
|
|
'product' => $product,
|
|
'quantity' => 4,
|
|
'subtotal' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ),
|
|
'total' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ),
|
|
)
|
|
);
|
|
$item->save();
|
|
$order_2->add_item( $item );
|
|
$order_2->set_date_created( $order_time );
|
|
$order_2->set_status( $order_status );
|
|
|
|
if ( $coupon ) {
|
|
$order_2->apply_coupon( $coupon );
|
|
}
|
|
|
|
$order_2->calculate_totals();
|
|
$order_2->save();
|
|
|
|
$orders[] = $order_2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$data_store = new WC_Admin_Reports_Orders_Stats_Data_Store();
|
|
|
|
// Tests for before & after set to current hour.
|
|
$current_hour_start = new DateTime();
|
|
$current_hour_start->setTimestamp( $order_1_time );
|
|
$current_hour_minutes = (int) $current_hour_start->format( 'i' );
|
|
$current_hour_start->setTimestamp( $order_1_time - $current_hour_minutes * MINUTE_IN_SECONDS );
|
|
|
|
$current_hour_end = new DateTime();
|
|
$current_hour_end->setTimestamp( $order_1_time );
|
|
$order_1_seconds = (int) $current_hour_end->format( 'U' ) % HOUR_IN_SECONDS;
|
|
$current_hour_end->setTimestamp( $order_1_time + ( HOUR_IN_SECONDS - $order_1_seconds ) - 1 );
|
|
|
|
// All orders, no filters.
|
|
// 72 orders in one batch (3 products * 3 coupon options * 2 order statuses * 2 customers * 2 orders), 4 items of each product per order
|
|
// 24 orders without coupons, 48 with coupons: 24 with $1 coupon and 24 with $2 coupon.
|
|
// shipping is $10 per order.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
);
|
|
|
|
$order_permutations = 72;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 24;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product + $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 2;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// * Order status filter
|
|
// ** Status is, positive filter for 2 statuses, i.e. all orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
);
|
|
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Status is, positive filter for 1 status -> half orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
);
|
|
|
|
$order_permutations = 36;
|
|
$order_w_coupon_1_perms = 12;
|
|
$order_w_coupon_2_perms = 12;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Status is not, negative filter for 1 status -> half orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
);
|
|
|
|
$order_permutations = 36;
|
|
$order_w_coupon_1_perms = 12;
|
|
$order_w_coupon_2_perms = 12;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Status is not, negative filter for 2 statuses -> no orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is_not' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
);
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => 0,
|
|
'num_items_sold' => 0,
|
|
'gross_revenue' => 0,
|
|
'coupons' => 0,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => 0,
|
|
'net_revenue' => 0,
|
|
'avg_items_per_order' => 0,
|
|
'avg_order_value' => 0,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 0,
|
|
'products' => 0,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => 0,
|
|
'num_items_sold' => 0,
|
|
'gross_revenue' => 0,
|
|
'coupons' => 0,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => 0,
|
|
'net_revenue' => 0,
|
|
'avg_items_per_order' => 0,
|
|
'avg_order_value' => 0,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 0,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Status is + Status is not, positive filter for 2 statuses, negative for 1 -> half of orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
);
|
|
|
|
$order_permutations = 36;
|
|
$order_w_coupon_1_perms = 12;
|
|
$order_w_coupon_2_perms = 12;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// * Product filter
|
|
// ** Product includes, positive filter for 2 products, i.e. 2 orders out of 3.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
$product_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 48;
|
|
$order_w_coupon_1_perms = 16;
|
|
$order_w_coupon_2_perms = 16;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 4 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 4 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 4 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 4 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 3,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Product includes, positive filter for 1 product, 1/3 of orders
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'product_includes' => array(
|
|
$product_3->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 24;
|
|
$order_w_coupon_1_perms = 8;
|
|
$order_w_coupon_2_perms = 8;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_3_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
'products' => 2,
|
|
// product 3 and product 4 (that is sometimes included in the orders with product 3).
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Product excludes, negative filter for 1 product, 2/3 of orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'product_excludes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 48;
|
|
$order_w_coupon_1_perms = 16;
|
|
$order_w_coupon_2_perms = 16;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_3_price * $qty_per_product * ( $orders_count / 4 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 4 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 4 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 4 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
'products' => 3,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Product excludes, negative filter for 2 products, 1/3 of orders
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'product_excludes' => array(
|
|
$product_1->get_id(),
|
|
$product_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 24;
|
|
$order_w_coupon_1_perms = 8;
|
|
$order_w_coupon_2_perms = 8;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_3_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Product includes + product excludes, positive filter for 2 products, negative for 1 -> 1/3 of orders, only orders with product 2 and product 2 + product 4
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
$product_2->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 24;
|
|
$order_w_coupon_1_perms = 8;
|
|
$order_w_coupon_2_perms = 8;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_2_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// * Coupon filters
|
|
// ** Coupon includes, positive filter for 2 coupons, i.e. 2/3 of orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
$coupon_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 48;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 24;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Coupon includes, positive filter for 1 coupon, 1/3 of orders
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 24;
|
|
$order_w_coupon_1_perms = 24;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => 2,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Coupon excludes, negative filter for 1 coupon, 2/3 of orders
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'coupon_excludes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 48;
|
|
$order_w_coupon_2_perms = 24;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Coupon excludes, negative filter for 2 coupons, 1/3 of orders
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'coupon_excludes' => array(
|
|
$coupon_1->get_id(),
|
|
$coupon_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 24;
|
|
$order_w_coupon_1_perms = 0;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Coupon includes + coupon excludes, positive filter for 2 coupon, negative for 1, 1/3 orders
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
$coupon_2->get_id(),
|
|
),
|
|
'coupon_excludes' => array(
|
|
$coupon_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 24;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// * Customer filters
|
|
// ** Customer new
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'customer' => 'new',
|
|
);
|
|
|
|
$orders_count = 144;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = $orders_count;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// ** Customer returning
|
|
$returning_order = WC_Helper_Order::create_order( $customer_1->get_id(), $product );
|
|
$returning_order->set_status( 'completed' );
|
|
$returning_order->set_shipping_total( 10 );
|
|
$returning_order->set_total( 110 ); // $25x4 products + $10 shipping.
|
|
$returning_order->set_date_created( $order_1_time + 1 ); // This is guaranteed to belong to the same hour by the adjustment to $order_1_time.
|
|
$returning_order->save();
|
|
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), // I don't think this makes sense.... date( 'Y-m-d H:i:s', $orders[0]->get_date_created()->getOffsetTimestamp() + 1 ), // Date after initial order to get a returning customer.
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'customer' => 'returning',
|
|
);
|
|
|
|
$order_permutations = 72;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 24;
|
|
|
|
$orders_count = 1;
|
|
$num_items_sold = 4;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = 100;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 1,
|
|
'num_new_customers' => 0,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'date_start_gmt' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 1,
|
|
'num_new_customers' => 0,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query};" );
|
|
wp_delete_post( $returning_order->get_id(), true );
|
|
|
|
// Combinations: match all
|
|
// status_is + product_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 12;
|
|
$order_w_coupon_1_perms = 4;
|
|
$order_w_coupon_2_perms = 4;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is + coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 12;
|
|
$order_w_coupon_1_perms = 12;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// product_includes + coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 8;
|
|
$order_w_coupon_1_perms = 8;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is + product_includes + coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 4;
|
|
$order_w_coupon_1_perms = 4;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is + status_is_not + product_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 4;
|
|
$order_w_coupon_1_perms = 4;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is + status_is_not + product_includes + product_excludes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
$product_2->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_4->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 12;
|
|
$order_w_coupon_1_perms = 4;
|
|
$order_w_coupon_2_perms = 4;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count * $qty_per_product; // No 2-item-orders here.
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
// Prod_1, status_1, no coupon orders included here, so 2 new cust orders.
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is + status_is_not + product_includes + product_excludes + coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
$product_2->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_4->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 4;
|
|
$order_w_coupon_1_perms = 4;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is + status_is_not + product_includes + product_excludes + coupon_includes + coupon_excludes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
$order_status_2,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
$product_2->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_4->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
$coupon_2->get_id(),
|
|
),
|
|
'coupon_excludes' => array(
|
|
$coupon_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 4;
|
|
$order_w_coupon_1_perms = 4;
|
|
$order_w_coupon_2_perms = 0;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 2 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 2 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 2,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// Combinations: match any
|
|
// status_is + status_is_not, all orders.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_1,
|
|
),
|
|
);
|
|
|
|
$order_permutations = 72;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 24;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR product_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 48;
|
|
$order_w_coupon_1_perms = 16;
|
|
$order_w_coupon_2_perms = 16;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 4 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 8 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 8 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 4 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 8 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 8 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 48;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 12;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR coupon_excludes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'coupon_excludes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 60;
|
|
$order_w_coupon_1_perms = 12;
|
|
$order_w_coupon_2_perms = 24;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count / 6 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// product_includes OR coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 40;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 8;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count * 3 / 10 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count * 1 / 10 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count * 1 / 10 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 10 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 10 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 10 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR product_includes OR coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 56;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 16;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count * 3 / 14 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 14 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR status_is_not OR product_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 56;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 16;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count * 3 / 14 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 14 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 7 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR status_is_not OR product_includes OR product_excludes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 60;
|
|
$order_w_coupon_1_perms = 20;
|
|
$order_w_coupon_2_perms = 20;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count * 1 / 5 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count * 1 / 10 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count * 1 / 5 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 5 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 10 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 5 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_2->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 64;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 20;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count * 3 / 16 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count * 1 / 8 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count * 3 / 16 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 16 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count * 1 / 8 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 16 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes OR coupon_excludes.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'match' => 'any',
|
|
'status_is' => array(
|
|
$order_status_1,
|
|
),
|
|
'status_is_not' => array(
|
|
$order_status_2,
|
|
),
|
|
'product_includes' => array(
|
|
$product_1->get_id(),
|
|
),
|
|
'product_excludes' => array(
|
|
$product_2->get_id(),
|
|
),
|
|
'coupon_includes' => array(
|
|
$coupon_1->get_id(),
|
|
),
|
|
'coupon_excludes' => array(
|
|
$coupon_2->get_id(),
|
|
),
|
|
);
|
|
|
|
$order_permutations = 68;
|
|
$order_w_coupon_1_perms = 24;
|
|
$order_w_coupon_2_perms = 20;
|
|
|
|
$orders_count = count( $this_['hour'] ) * $order_permutations;
|
|
$num_items_sold = $orders_count / 2 * $qty_per_product
|
|
+ $orders_count / 2 * $qty_per_product * 2;
|
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count * 3 / 17 )
|
|
+ $product_2_price * $qty_per_product * ( $orders_count * 5 / 34 )
|
|
+ $product_3_price * $qty_per_product * ( $orders_count * 3 / 17 )
|
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 17 )
|
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $orders_count * 5 / 34 )
|
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $orders_count * 3 / 17 )
|
|
- $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 4,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
}
|
|
|
|
/**
|
|
* Test zero filling when ordering by date in descending order.
|
|
*/
|
|
public function test_zero_fill_order_date_desc() {
|
|
global $wpdb;
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
$product_price = 11;
|
|
$product = new WC_Product_Simple();
|
|
$product->set_name( 'Test Product' );
|
|
$product->set_regular_price( $product_price );
|
|
$product->save();
|
|
|
|
$customer = WC_Helper_Customer::create_customer( 'cust_1', 'pwd_1', 'user_1@mail.com' );
|
|
|
|
$order_1_time = time();
|
|
$order_2_time = $order_1_time;
|
|
|
|
$order_during_this_['hour'] = array( 1, 2 );
|
|
$order_during_this_['day'] = array( 1, 2 );
|
|
$order_during_this_['week'] = array( 1, 2 );
|
|
$order_during_this_['month'] = array( 1, 2 );
|
|
$order_during_this_['year'] = array( 1, 2 );
|
|
|
|
$order_1_datetime = new DateTime();
|
|
$order_1_datetime = $order_1_datetime->setTimestamp( $order_1_time );
|
|
|
|
// Previous year.
|
|
$order_7_datetime = new DateTime();
|
|
$order_7_datetime = $order_7_datetime->setTimestamp( $order_1_time - YEAR_IN_SECONDS );
|
|
$order_7_time = $order_7_datetime->format( 'U' );
|
|
$order[7]['year'] = (int) $order_7_datetime->format( 'Y' );
|
|
$order[7]['month'] = (int) $order_7_datetime->format( 'm' );
|
|
$order[7]['week'] = (int) $order_7_datetime->format( 'W' );
|
|
$order[7]['day'] = (int) $order_7_datetime->format( 'd' );
|
|
|
|
// Previous month.
|
|
$order_6_datetime = new DateTime();
|
|
$order_6_datetime = $order_6_datetime->setTimestamp( $order_1_time - MONTH_IN_SECONDS );
|
|
$order_6_time = $order_6_datetime->format( 'U' );
|
|
$order[6]['year'] = (int) $order_6_datetime->format( 'Y' );
|
|
$order[6]['month'] = (int) $order_6_datetime->format( 'm' );
|
|
$order[6]['week'] = (int) $order_6_datetime->format( 'W' );
|
|
$order[6]['day'] = (int) $order_6_datetime->format( 'd' );
|
|
|
|
// Previous week.
|
|
$order_5_datetime = new DateTime();
|
|
$order_5_datetime = $order_5_datetime->setTimestamp( $order_1_time - WEEK_IN_SECONDS );
|
|
$order_5_time = $order_5_datetime->format( 'U' );
|
|
$order[5]['year'] = (int) $order_5_datetime->format( 'Y' );
|
|
$order[5]['month'] = (int) $order_5_datetime->format( 'm' );
|
|
$order[5]['week'] = (int) $order_5_datetime->format( 'W' );
|
|
$order[5]['day'] = (int) $order_5_datetime->format( 'd' );
|
|
|
|
// Previous day.
|
|
$order_4_datetime = new DateTime();
|
|
$order_4_datetime = $order_4_datetime->setTimestamp( $order_1_time - DAY_IN_SECONDS );
|
|
$order_4_time = $order_4_datetime->format( 'U' );
|
|
$order[4]['year'] = (int) $order_4_datetime->format( 'Y' );
|
|
$order[4]['month'] = (int) $order_4_datetime->format( 'm' );
|
|
$order[4]['week'] = (int) $order_4_datetime->format( 'W' );
|
|
$order[4]['day'] = (int) $order_4_datetime->format( 'd' );
|
|
|
|
// same day, -1 hour.
|
|
$order_3_datetime = new DateTime();
|
|
$order_3_datetime = $order_3_datetime->setTimestamp( $order_1_time - HOUR_IN_SECONDS );
|
|
$order_3_time = $order_3_datetime->format( 'U' );
|
|
$order[3]['year'] = (int) $order_3_datetime->format( 'Y' );
|
|
$order[3]['month'] = (int) $order_3_datetime->format( 'm' );
|
|
$order[3]['week'] = (int) $order_3_datetime->format( 'W' );
|
|
$order[3]['day'] = (int) $order_3_datetime->format( 'd' );
|
|
|
|
// Current order.
|
|
$order[1]['year'] = (int) $order_1_datetime->format( 'Y' );
|
|
$order[1]['month'] = (int) $order_1_datetime->format( 'm' );
|
|
$order[1]['week'] = (int) $order_1_datetime->format( 'W' );
|
|
$order[1]['day'] = (int) $order_1_datetime->format( 'd' );
|
|
$order[1]['hour'] = (int) $order_1_datetime->format( 'H' );
|
|
|
|
foreach ( array( 3, 4, 5, 6, 7 ) as $order_no ) {
|
|
if ( $order[ $order_no ]['day'] === $order[1]['day'] && $order[ $order_no ]['month'] === $order[1]['month'] && $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$order_during_this_['day'][] = $order_no;
|
|
}
|
|
if ( $order[ $order_no ]['week'] === $order[1]['week'] && $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$order_during_this_['week'][] = $order_no;
|
|
}
|
|
if ( $order[ $order_no ]['month'] === $order[1]['month'] && $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$order_during_this_['month'][] = $order_no;
|
|
}
|
|
if ( $order[ $order_no ]['year'] === $order[1]['year'] ) {
|
|
$order_during_this_['year'][] = $order_no;
|
|
}
|
|
}
|
|
|
|
$order_status = 'completed';
|
|
$qty_per_product = 4; // Hardcoded in WC_Helper_Order::create_order.
|
|
|
|
$orders = array();
|
|
foreach (
|
|
array(
|
|
$order_1_time,
|
|
$order_2_time,
|
|
$order_3_time,
|
|
$order_4_time,
|
|
$order_5_time,
|
|
$order_6_time,
|
|
$order_7_time,
|
|
) as $order_time
|
|
) {
|
|
// Order with 1 product.
|
|
$order = WC_Helper_Order::create_order( $customer->get_id(), $product );
|
|
$order->set_date_created( $order_time );
|
|
$order->set_status( $order_status );
|
|
|
|
$order->calculate_totals();
|
|
$order->save();
|
|
|
|
$orders[] = $order;
|
|
}
|
|
|
|
$data_store = new WC_Admin_Reports_Orders_Stats_Data_Store();
|
|
|
|
// Tests for before & after set to current hour.
|
|
$current_hour_start = new DateTime();
|
|
$current_hour_start->setTimestamp( $order_1_time );
|
|
$current_hour_minutes = (int) $current_hour_start->format( 'i' );
|
|
$current_hour_start->setTimestamp( $order_1_time - $current_hour_minutes * MINUTE_IN_SECONDS );
|
|
|
|
$current_hour_end = new DateTime();
|
|
$current_hour_end->setTimestamp( $order_1_time );
|
|
$seconds_into_hour = (int) $current_hour_end->format( 'U' ) % HOUR_IN_SECONDS;
|
|
$current_hour_end->setTimestamp( $order_1_time + ( HOUR_IN_SECONDS - $seconds_into_hour ) - 1 );
|
|
|
|
// Test for current hour--only 1 hour visible.
|
|
$query_args = array(
|
|
'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'orderby' => 'date',
|
|
'order' => 'desc',
|
|
);
|
|
|
|
$orders_count = count( $order_during_this_['hour'] );
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => array(
|
|
array(
|
|
'interval' => $current_hour_start->format( 'Y-m-d H' ),
|
|
'date_start' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $current_hour_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
),
|
|
),
|
|
'total' => 1,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
/*
|
|
* Test for [-5 hours, now] -- partial 1 page.
|
|
* | --------------------|------|-----|-----|
|
|
* minus 5 hours | | order1 now
|
|
* | | order2
|
|
* | now-1 hour
|
|
* order 3
|
|
*/
|
|
$hour_offset = 5;
|
|
$minus_5_hours = new DateTime();
|
|
$now_timestamp = (int) $current_hour_end->format( 'U' );
|
|
$minus_5_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS );
|
|
|
|
$query_args = array(
|
|
'after' => $minus_5_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'orderby' => 'date',
|
|
'order' => 'desc',
|
|
);
|
|
|
|
// Expected Intervals section construction.
|
|
$expected_intervals = [];
|
|
// Even in case this runs exactly at the hour turn second, there should still be 6 intervals:
|
|
// e.g. 20:30:51 -(minus 5 hours)- 15:30:51 means intervals 15:30:51--15:59:59, 16:00-16:59, 17, 18, 19, 20:00-20:30, i.e. 6 intervals
|
|
// also if this run exactly at 20:00 -(minus 5 hours)- 15:00, then intervals should be 15:00-15:59, 16, 17, 18, 19, 20:00-20:00.
|
|
$interval_count = $hour_offset + 1;
|
|
for ( $i = 0; $i < $interval_count; $i ++ ) {
|
|
if ( 0 === $i ) {
|
|
$date_start = new DateTime( $current_hour_end->format( 'Y-m-d H:00:00' ) );
|
|
$date_end = $current_hour_end;
|
|
} elseif ( $hour_offset === $i ) {
|
|
$date_start = $minus_5_hours;
|
|
$date_end = new DateTime( $minus_5_hours->format( 'Y-m-d H:59:59' ) );
|
|
} else {
|
|
$hour_anchor = new DateTime();
|
|
$hour_anchor->setTimestamp( $now_timestamp - $i * HOUR_IN_SECONDS );
|
|
$date_start = new DateTime( $hour_anchor->format( 'Y-m-d H:00:00' ) );
|
|
$date_end = new DateTime( $hour_anchor->format( 'Y-m-d H:59:59' ) );
|
|
}
|
|
|
|
if ( 0 === $i ) {
|
|
$orders_count = count( $order_during_this_['hour'] );
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
} elseif ( 1 === $i ) {
|
|
$orders_count = 1;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
} else {
|
|
$orders_count = 0;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 0;
|
|
}
|
|
|
|
$expected_interval = array(
|
|
'interval' => $date_start->format( 'Y-m-d H' ),
|
|
'date_start' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => 0 === $orders_count ? 0 : $num_items_sold / $orders_count,
|
|
'avg_order_value' => 0 === $orders_count ? 0 : $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
);
|
|
|
|
$expected_intervals[] = $expected_interval;
|
|
}
|
|
|
|
// Totals section.
|
|
$orders_count = count( $order_during_this_['hour'] ) + 1; // order 3 is 1 hour before order 1 & 2, so include it.
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => $expected_intervals,
|
|
'total' => $interval_count,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
$actual = json_decode( json_encode( $data_store->get_data( $query_args ) ), true );
|
|
$this->assertEquals( $expected_stats, $actual, 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
/*
|
|
* Test for [-9 hours, now] -- full 1 page.
|
|
* | --------------------|------|-----|-----|
|
|
* minus 9 hours | | order1 now
|
|
* | | order2
|
|
* | now-1 hour
|
|
* order 3
|
|
*/
|
|
$hour_offset = 9;
|
|
$minus_9_hours = new DateTime();
|
|
$now_timestamp = (int) $current_hour_end->format( 'U' );
|
|
$minus_9_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS );
|
|
|
|
$query_args = array(
|
|
'after' => $minus_9_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'orderby' => 'date',
|
|
'order' => 'desc',
|
|
);
|
|
|
|
// Expected Intervals section construction.
|
|
$expected_intervals = [];
|
|
$interval_count = $hour_offset + 1;
|
|
for ( $i = 0; $i < $interval_count; $i ++ ) {
|
|
if ( 0 === $i ) {
|
|
$date_start = new DateTime( $current_hour_end->format( 'Y-m-d H:00:00' ) );
|
|
$date_end = $current_hour_end;
|
|
} elseif ( $hour_offset === $i ) {
|
|
$date_start = $minus_9_hours;
|
|
$date_end = new DateTime( $minus_9_hours->format( 'Y-m-d H:59:59' ) );
|
|
} else {
|
|
$hour_anchor = new DateTime();
|
|
$hour_anchor->setTimestamp( $now_timestamp - $i * HOUR_IN_SECONDS );
|
|
$date_start = new DateTime( $hour_anchor->format( 'Y-m-d H:00:00' ) );
|
|
$date_end = new DateTime( $hour_anchor->format( 'Y-m-d H:59:59' ) );
|
|
}
|
|
|
|
if ( 0 === $i ) {
|
|
$orders_count = count( $order_during_this_['hour'] );
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
} elseif ( 1 === $i ) {
|
|
$orders_count = 1;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
} else {
|
|
$orders_count = 0;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 0;
|
|
}
|
|
|
|
$expected_interval = array(
|
|
'interval' => $date_start->format( 'Y-m-d H' ),
|
|
'date_start' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => 0 === $orders_count ? 0 : $num_items_sold / $orders_count,
|
|
'avg_order_value' => 0 === $orders_count ? 0 : $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
);
|
|
|
|
$expected_intervals[] = $expected_interval;
|
|
}
|
|
|
|
// Totals section.
|
|
$orders_count = count( $order_during_this_['hour'] ) + 1; // order 3 is 1 hour before order 1 & 2, so include it.
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => $expected_intervals,
|
|
'total' => $interval_count,
|
|
'pages' => 1,
|
|
'page_no' => 1,
|
|
);
|
|
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
/*
|
|
* Test for [-10 hours, now] -- 1 page full, 1 interval on 2nd page.
|
|
* page 1
|
|
* | --------------------|------|-----|-----|
|
|
* minus 9 hours | | order1 now
|
|
* | | order2
|
|
* | now-1 hour
|
|
* order 3
|
|
*
|
|
* page 2
|
|
* |---|
|
|
* minus 10 hours
|
|
* minus 9 hours
|
|
*/
|
|
$hour_offset = 10;
|
|
$minus_10_hours = new DateTime();
|
|
$now_timestamp = (int) $current_hour_end->format( 'U' );
|
|
$minus_10_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS );
|
|
|
|
// Page 1.
|
|
$query_args = array(
|
|
'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'orderby' => 'date',
|
|
'order' => 'desc',
|
|
'page' => 1,
|
|
);
|
|
|
|
// Expected Intervals section construction.
|
|
$expected_intervals = [];
|
|
$interval_count = 10;
|
|
for ( $i = 0; $i < $interval_count; $i ++ ) {
|
|
if ( 0 === $i ) {
|
|
$date_start = new DateTime( $current_hour_end->format( 'Y-m-d H:00:00' ) );
|
|
$date_end = $current_hour_end;
|
|
} else {
|
|
$hour_anchor = new DateTime();
|
|
$hour_anchor->setTimestamp( $now_timestamp - $i * HOUR_IN_SECONDS );
|
|
$date_start = new DateTime( $hour_anchor->format( 'Y-m-d H:00:00' ) );
|
|
$date_end = new DateTime( $hour_anchor->format( 'Y-m-d H:59:59' ) );
|
|
}
|
|
|
|
if ( 0 === $i ) {
|
|
$orders_count = count( $order_during_this_['hour'] );
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
} elseif ( 1 === $i ) {
|
|
$orders_count = 1;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
} else {
|
|
$orders_count = 0;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 0;
|
|
}
|
|
|
|
$expected_interval = array(
|
|
'interval' => $date_start->format( 'Y-m-d H' ),
|
|
'date_start' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => 0 === $orders_count ? 0 : $num_items_sold / $orders_count,
|
|
'avg_order_value' => 0 === $orders_count ? 0 : $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
);
|
|
|
|
$expected_intervals[] = $expected_interval;
|
|
}
|
|
|
|
// Totals section.
|
|
$orders_count = count( $order_during_this_['hour'] ) + 1; // order 3 is 1 hour before order 1 & 2, so include it.
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => $expected_intervals,
|
|
'total' => 11,
|
|
'pages' => 2,
|
|
'page_no' => 1,
|
|
);
|
|
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// Page 2.
|
|
$query_args = array(
|
|
'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
|
'interval' => 'hour',
|
|
'orderby' => 'date',
|
|
'order' => 'desc',
|
|
'page' => 2,
|
|
);
|
|
|
|
// Expected Intervals section construction.
|
|
$expected_intervals = [];
|
|
$interval_count = 1;
|
|
|
|
// Interval subresponse.
|
|
$date_start = $minus_10_hours;
|
|
$date_end = new DateTime( $minus_10_hours->format( 'Y-m-d H:59:59' ) );
|
|
|
|
$orders_count = 0;
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 0;
|
|
|
|
$expected_interval = array(
|
|
'interval' => $date_start->format( 'Y-m-d H' ),
|
|
'date_start' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_start_gmt' => $date_start->format( 'Y-m-d H:i:s' ),
|
|
'date_end' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'date_end_gmt' => $date_end->format( 'Y-m-d H:i:s' ),
|
|
'subtotals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => 0 === $orders_count ? 0 : $num_items_sold / $orders_count,
|
|
'avg_order_value' => 0 === $orders_count ? 0 : $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
),
|
|
);
|
|
|
|
$expected_intervals[] = $expected_interval;
|
|
|
|
// Totals section.
|
|
$orders_count = count( $order_during_this_['hour'] ) + 1; // order 3 is 1 hour before order 1 & 2, so include it.
|
|
$num_items_sold = $orders_count * $qty_per_product;
|
|
$coupons = 0;
|
|
$shipping = $orders_count * 10;
|
|
$net_revenue = $product_price * $qty_per_product * $orders_count - $coupons;
|
|
$gross_revenue = $net_revenue + $shipping;
|
|
$new_customers = 1;
|
|
|
|
$expected_stats = array(
|
|
'totals' => array(
|
|
'orders_count' => $orders_count,
|
|
'num_items_sold' => $num_items_sold,
|
|
'gross_revenue' => $gross_revenue,
|
|
'coupons' => $coupons,
|
|
'refunds' => 0,
|
|
'taxes' => 0,
|
|
'shipping' => $shipping,
|
|
'net_revenue' => $net_revenue,
|
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
|
'avg_order_value' => $net_revenue / $orders_count,
|
|
'num_returning_customers' => 0,
|
|
'num_new_customers' => $new_customers,
|
|
'products' => 1,
|
|
),
|
|
'intervals' => $expected_intervals,
|
|
'total' => 11,
|
|
'pages' => 2,
|
|
'page_no' => 2,
|
|
);
|
|
|
|
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Query args: ' . print_r( $query_args, true ) . "; query: {$wpdb->last_query}" );
|
|
|
|
// Now the same should be done for days, weeks, months and years. But I'm too low on mana to do that.
|
|
}
|
|
}
|