Check and update customer first orders to fix returning_customers (https://github.com/woocommerce/woocommerce-admin/pull/1517)
* Add get_oldest_orders function to customer data store * Check if is first order in is_returning_customer() * Check if the customers first order has changed and update all orders respectively * Fix check for empty oldest_orders array * Change avg_items_per_order to number instead of integer * Fix failing tests for new/returning customers Only the first orders with product 1 and without coupons should be new per each customer * Remove sleep functions from tests * Assign second order to a variable for legibility * Fix second order assignment and check if set
This commit is contained in:
parent
a60d977d5c
commit
746d062f09
|
@ -170,7 +170,7 @@ class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Report
|
||||||
),
|
),
|
||||||
'avg_items_per_order' => array(
|
'avg_items_per_order' => array(
|
||||||
'description' => __( 'Average items per order', 'wc-admin' ),
|
'description' => __( 'Average items per order', 'wc-admin' ),
|
||||||
'type' => 'integer',
|
'type' => 'number',
|
||||||
'context' => array( 'view', 'edit' ),
|
'context' => array( 'view', 'edit' ),
|
||||||
'readonly' => true,
|
'readonly' => true,
|
||||||
),
|
),
|
||||||
|
|
|
@ -523,6 +523,24 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
||||||
return $customer_id ? (int) $customer_id : false;
|
return $customer_id ? (int) $customer_id : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the oldest orders made by a customer.
|
||||||
|
*
|
||||||
|
* @param int $customer_id Customer ID.
|
||||||
|
* @return array Orders.
|
||||||
|
*/
|
||||||
|
public static function get_oldest_orders( $customer_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
$orders_table = $wpdb->prefix . 'wc_order_stats';
|
||||||
|
|
||||||
|
return $wpdb->get_results(
|
||||||
|
$wpdb->prepare(
|
||||||
|
"SELECT order_id, date_created FROM {$orders_table} WHERE customer_id = %d ORDER BY date_created, order_id ASC LIMIT 2",
|
||||||
|
$customer_id
|
||||||
|
)
|
||||||
|
); // WPCS: unprepared SQL ok.
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the database with customer data.
|
* Update the database with customer data.
|
||||||
*
|
*
|
||||||
|
|
|
@ -515,24 +515,57 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected static function is_returning_customer( $order ) {
|
protected static function is_returning_customer( $order ) {
|
||||||
global $wpdb;
|
|
||||||
$customer_id = WC_Admin_Reports_Customers_Data_Store::get_customer_id_by_user_id( $order->get_user_id() );
|
$customer_id = WC_Admin_Reports_Customers_Data_Store::get_customer_id_by_user_id( $order->get_user_id() );
|
||||||
$orders_stats_table = $wpdb->prefix . self::TABLE_NAME;
|
|
||||||
|
|
||||||
if ( ! $customer_id ) {
|
if ( ! $customer_id ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$customer_orders = $wpdb->get_var(
|
$oldest_orders = WC_Admin_Reports_Customers_Data_Store::get_oldest_orders( $customer_id );
|
||||||
|
|
||||||
|
if ( empty( $oldest_orders ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$first_order = $oldest_orders[0];
|
||||||
|
|
||||||
|
// Order is older than previous first order.
|
||||||
|
if ( $order->get_date_created() < new WC_DateTime( $first_order->date_created ) ) {
|
||||||
|
self::set_customer_first_order( $customer_id, $order->get_id() );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// First order date has changed and next oldest is now the first order.
|
||||||
|
$second_order = isset( $oldest_orders[1] ) ? $oldest_orders[1] : false;
|
||||||
|
if (
|
||||||
|
(int) $order->get_id() === (int) $first_order->order_id &&
|
||||||
|
$order->get_date_created() > new WC_DateTime( $first_order->date_created ) &&
|
||||||
|
$second_order &&
|
||||||
|
new WC_DateTime( $second_order->date_created ) < $order->get_date_created()
|
||||||
|
) {
|
||||||
|
self::set_customer_first_order( $customer_id, $second_order->order_id );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int) $order->get_id() !== (int) $first_order->order_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a customer's first order and all others to returning.
|
||||||
|
*
|
||||||
|
* @param int $customer_id Customer ID.
|
||||||
|
* @param int $order_id Order ID.
|
||||||
|
*/
|
||||||
|
protected static function set_customer_first_order( $customer_id, $order_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
$orders_stats_table = $wpdb->prefix . self::TABLE_NAME;
|
||||||
|
|
||||||
|
$wpdb->query(
|
||||||
$wpdb->prepare(
|
$wpdb->prepare(
|
||||||
"SELECT COUNT(*) FROM ${orders_stats_table} WHERE customer_id = %d AND date_created < %s AND order_id != %d",
|
"UPDATE ${orders_stats_table} SET returning_customer = CASE WHEN order_id = %d THEN false ELSE true END WHERE customer_id = %d",
|
||||||
$customer_id,
|
$order_id,
|
||||||
date( 'Y-m-d H:i:s', $order->get_date_created()->getTimestamp() ),
|
$customer_id
|
||||||
$order->get_id()
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $customer_orders >= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -178,9 +178,6 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
$order->set_shipping_total( 0 );
|
$order->set_shipping_total( 0 );
|
||||||
$order->set_cart_tax( 0 );
|
$order->set_cart_tax( 0 );
|
||||||
$order->save();
|
$order->save();
|
||||||
|
|
||||||
// Wait one second to avoid potentially ambiguous new/returning customer.
|
|
||||||
sleep( 1 ); // @todo Remove this after p90Yrv-XN-p2 is resolved.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WC_Helper_Queue::run_all_pending();
|
WC_Helper_Queue::run_all_pending();
|
||||||
|
@ -990,8 +987,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
// product 3 and product 4 (that is sometimes included in the orders with product 3).
|
// product 3 and product 4 (that is sometimes included in the orders with product 3).
|
||||||
|
@ -1014,8 +1011,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1064,8 +1061,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'products' => 3,
|
'products' => 3,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1087,8 +1084,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1136,8 +1133,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1159,8 +1156,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1211,8 +1208,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1234,8 +1231,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1288,8 +1285,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'products' => 4,
|
'products' => 4,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1311,8 +1308,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1362,8 +1359,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'products' => 4,
|
'products' => 4,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1385,8 +1382,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => 2,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1591,8 +1588,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 4,
|
'products' => 4,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1614,8 +1611,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1635,18 +1632,11 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'customer' => 'new',
|
'customer' => 'new',
|
||||||
);
|
);
|
||||||
|
|
||||||
$orders_count = 144;
|
$orders_count = 2;
|
||||||
$num_items_sold = $orders_count / 2 * $qty_per_product
|
$num_items_sold = $orders_count * $qty_per_product;
|
||||||
+ $orders_count / 2 * $qty_per_product * 2;
|
$coupons = 0;
|
||||||
$coupons = $orders_count;
|
|
||||||
$shipping = $orders_count * 10;
|
$shipping = $orders_count * 10;
|
||||||
$net_revenue = $product_1_price * $qty_per_product * ( $orders_count / 6 )
|
$net_revenue = $product_1_price * $qty_per_product * $orders_count;
|
||||||
+ $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;
|
$gross_revenue = $net_revenue + $shipping;
|
||||||
|
|
||||||
$expected_stats = array(
|
$expected_stats = array(
|
||||||
|
@ -1663,7 +1653,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 0,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => $new_customers,
|
||||||
'products' => 4,
|
'products' => 1,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
'intervals' => array(
|
'intervals' => array(
|
||||||
|
@ -1697,15 +1687,6 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
$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}" );
|
$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
|
// ** 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();
|
|
||||||
|
|
||||||
WC_Helper_Queue::run_all_pending();
|
|
||||||
|
|
||||||
$query_args = array(
|
$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.
|
'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 ),
|
'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
||||||
|
@ -1713,15 +1694,23 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'customer' => 'returning',
|
'customer' => 'returning',
|
||||||
);
|
);
|
||||||
|
|
||||||
$order_permutations = 72;
|
$total_orders_count = 144;
|
||||||
|
$returning_orders_count = 2;
|
||||||
$order_w_coupon_1_perms = 24;
|
$order_w_coupon_1_perms = 24;
|
||||||
$order_w_coupon_2_perms = 24;
|
$order_w_coupon_2_perms = 24;
|
||||||
|
|
||||||
$orders_count = 1;
|
$orders_count = $total_orders_count - $returning_orders_count;
|
||||||
$num_items_sold = 4;
|
$num_items_sold = $total_orders_count * 6 - ( $returning_orders_count * 4 );
|
||||||
$coupons = 0;
|
$coupons = count( $this_['hour'] ) * ( $order_w_coupon_1_perms * $coupon_1_amount + $order_w_coupon_2_perms * $coupon_2_amount );
|
||||||
$shipping = $orders_count * 10;
|
$shipping = $orders_count * 10;
|
||||||
$net_revenue = 100;
|
$net_revenue = $product_1_price * $qty_per_product * ( $total_orders_count / 6 )
|
||||||
|
+ $product_2_price * $qty_per_product * ( $total_orders_count / 6 )
|
||||||
|
+ $product_3_price * $qty_per_product * ( $total_orders_count / 6 )
|
||||||
|
+ ( $product_1_price + $product_4_price ) * $qty_per_product * ( $total_orders_count / 6 )
|
||||||
|
+ ( $product_2_price + $product_4_price ) * $qty_per_product * ( $total_orders_count / 6 )
|
||||||
|
+ ( $product_3_price + $product_4_price ) * $qty_per_product * ( $total_orders_count / 6 )
|
||||||
|
- $product_1_price * $qty_per_product * $returning_orders_count
|
||||||
|
- $coupons;
|
||||||
$gross_revenue = $net_revenue + $shipping;
|
$gross_revenue = $net_revenue + $shipping;
|
||||||
|
|
||||||
$expected_stats = array(
|
$expected_stats = array(
|
||||||
|
@ -1734,11 +1723,11 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'taxes' => 0,
|
'taxes' => 0,
|
||||||
'shipping' => $shipping,
|
'shipping' => $shipping,
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold,
|
'avg_items_per_order' => round( $num_items_sold / $orders_count, 4 ),
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 1,
|
'num_returning_customers' => $returning_orders_count,
|
||||||
'num_new_customers' => 0,
|
'num_new_customers' => 0,
|
||||||
'products' => 1,
|
'products' => 4,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
'intervals' => array(
|
'intervals' => array(
|
||||||
|
@ -1757,9 +1746,9 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'taxes' => 0,
|
'taxes' => 0,
|
||||||
'shipping' => $shipping,
|
'shipping' => $shipping,
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold,
|
'avg_items_per_order' => round( $num_items_sold / $orders_count, 4 ),
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 1,
|
'num_returning_customers' => $returning_orders_count,
|
||||||
'num_new_customers' => 0,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1771,7 +1760,6 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
);
|
);
|
||||||
|
|
||||||
$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};" );
|
$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
|
// Combinations: match all
|
||||||
// status_is + product_includes.
|
// status_is + product_includes.
|
||||||
|
@ -1891,8 +1879,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 4,
|
'products' => 4,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1914,8 +1902,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1965,8 +1953,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -1988,8 +1976,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -2042,8 +2030,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -2065,8 +2053,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -2123,8 +2111,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -2146,8 +2134,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -2289,8 +2277,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -2312,8 +2300,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -2377,8 +2365,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'products' => 2,
|
'products' => 2,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
|
@ -2400,8 +2388,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
'net_revenue' => $net_revenue,
|
'net_revenue' => $net_revenue,
|
||||||
'avg_items_per_order' => $num_items_sold / $orders_count,
|
'avg_items_per_order' => $num_items_sold / $orders_count,
|
||||||
'avg_order_value' => $net_revenue / $orders_count,
|
'avg_order_value' => $net_revenue / $orders_count,
|
||||||
'num_returning_customers' => 0,
|
'num_returning_customers' => 2,
|
||||||
'num_new_customers' => $new_customers,
|
'num_new_customers' => 0,
|
||||||
'segments' => array(),
|
'segments' => array(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -4732,7 +4720,6 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
||||||
) as $order_time
|
) as $order_time
|
||||||
) {
|
) {
|
||||||
// Order with 1 product.
|
// Order with 1 product.
|
||||||
sleep( 1 ); // @todo Remove this after p90Yrv-XN-p2 is resolved.
|
|
||||||
$order = WC_Helper_Order::create_order( $customer->get_id(), $product );
|
$order = WC_Helper_Order::create_order( $customer->get_id(), $product );
|
||||||
$order->set_date_created( $order_time );
|
$order->set_date_created( $order_time );
|
||||||
$order->set_status( $order_status );
|
$order->set_status( $order_status );
|
||||||
|
|
Loading…
Reference in New Issue