Fix no email report on product filter (#35971)

* Make sure order total count uses distinct for order ids

* Add unit test for order count bug

* Add changelog

* Fix lint errors

* Fix lint errors
This commit is contained in:
louwie17 2022-12-22 10:13:44 -04:00 committed by GitHub
parent a9986fe75e
commit 60f2ced82b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix total count query of orders within Analytics reports data store.

View File

@ -256,7 +256,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$this->add_sql_query_params( $query_args );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$db_records_count = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM (
"SELECT COUNT( DISTINCT tt.order_id ) FROM (
{$this->subquery->get_query_statement()}
) AS tt"
);

View File

@ -175,6 +175,109 @@ class WC_Admin_Tests_Reports_Orders extends WC_Unit_Test_Case {
$this->assertEquals( $expected, $data_2->data[0]['extended_info']['products'] );
}
/**
* Test that product includes count returns correctly when multiple variations of the same product is added.
*/
public function test_product_and_variation_includes_count() {
global $wpdb;
WC_Helper_Reports::reset_stats_dbs();
// Populate all of the data.
$parent_product = new WC_Product_Variable();
$parent_product->set_name( 'Variable Product' );
$parent_product->set_regular_price( 25 );
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 );
$attribute->set_name( 'pa_color' );
$attribute->set_options( explode( WC_DELIMITER, 'green | red' ) );
$attribute->set_visible( false );
$attribute->set_variation( true );
$parent_product->set_attributes( array( $attribute ) );
$parent_product->save();
$variation = new WC_Product_Variation();
$variation->set_name( 'Test Variation - Green' );
$variation->set_parent_id( $parent_product->get_id() );
$variation->set_regular_price( 10 );
$variation->set_attributes( array( 'pa_color' => 'green' ) );
$variation->set_manage_stock( true );
$variation->set_stock_quantity( 25 );
$variation->save();
$variation2 = new WC_Product_Variation();
$variation2->set_name( 'Test Variation - Red' );
$variation2->set_parent_id( $parent_product->get_id() );
$variation2->set_regular_price( 10 );
$variation2->set_attributes( array( 'pa_color' => 'red' ) );
$variation2->set_manage_stock( true );
$variation2->set_stock_quantity( 25 );
$variation2->save();
$simple_product = new WC_Product_Simple();
$simple_product->set_name( 'Simple Product' );
$simple_product->set_regular_price( 25 );
$simple_product->save();
$order = WC_Helper_Order::create_order( 1, $variation );
$order2 = WC_Helper_Order::create_order( 1, $simple_product );
// Add simple product.
$item = new WC_Order_Item_Product();
$item->set_props(
array(
'product' => $simple_product,
'quantity' => 1,
'subtotal' => wc_get_price_excluding_tax( $simple_product, array( 'qty' => 1 ) ),
'total' => wc_get_price_excluding_tax( $simple_product, array( 'qty' => 1 ) ),
)
);
$item->save();
$order->add_item( $item );
$item2 = new WC_Order_Item_Product();
$item2->set_props(
array(
'product' => $variation2,
'quantity' => 2,
'subtotal' => wc_get_price_excluding_tax( $variation2, array( 'qty' => 1 ) ),
'total' => wc_get_price_excluding_tax( $variation2, array( 'qty' => 1 ) ),
)
);
$item2->save();
$order->add_item( $item2 );
// Fix totals.
$order->set_total( 95 ); // ( 6 * 10 ) + 25 + 10 shipping (in helper).
$order->set_status( 'completed' );
$order->save();
$order2->set_total( 45 ); // ( 1 * 10 ) + 25 + 10 shipping (in helper).
$order2->set_status( 'completed' );
$order2->save();
WC_Helper_Queue::run_all_pending();
$data_store = new OrdersDataStore();
$start_time = gmdate( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = gmdate( 'Y-m-d H:59:59', $order2->get_date_created()->getOffsetTimestamp() );
$args = array(
'after' => $start_time,
'before' => $end_time,
'extended_info' => 1,
'product_includes' => array( $parent_product->get_id() ),
);
// Test retrieving the stats through the data store.
$data = $data_store->get_data( $args );
$this->assertEquals( 1, $data->total );
$args_variation = array(
'after' => $start_time,
'before' => $end_time,
'variation_includes' => array( $variation->get_id() ),
);
// Test retrieving the stats through the data store.
$data_variation = $data_store->get_data( $args_variation );
$this->assertEquals( 1, $data_variation->total );
}
/**
* Test that excluding specific coupons doesn't exclude orders without coupons.
* See: https://github.com/woocommerce/woocommerce-admin/issues/6824.