Commit f86d92f6add for woocommerce
commit f86d92f6add6f5c2a8fdbd75c345b937b0a4cbf7
Author: Praveen Kumar <71814083+PraveenKum11@users.noreply.github.com>
Date: Tue Oct 20 20:10:33 2026 +0530
Accept username in the Analytics customers filter_empty parameter (#70026)
Lets wc-analytics/reports/customers?filter_empty=username exclude customers without a username, such as guests.
Fixes #39427.
diff --git a/plugins/woocommerce/changelog/39427-analytics-customers-filter-empty-username b/plugins/woocommerce/changelog/39427-analytics-customers-filter-empty-username
new file mode 100644
index 00000000000..da61e97c746
--- /dev/null
+++ b/plugins/woocommerce/changelog/39427-analytics-customers-filter-empty-username
@@ -0,0 +1,4 @@
+Significance: patch
+Type: enhancement
+
+Allow the Analytics customers report API to exclude customers without a username, such as guests.
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Customers/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Customers/Controller.php
index 97e34d35830..928e0cd045b 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Customers/Controller.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Customers/Controller.php
@@ -718,6 +718,7 @@ class Controller extends GenericController implements ExportableInterface {
'city',
'state',
'postcode',
+ 'username',
),
),
);
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php
index 1b6a47104e9..32bebfcbe33 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php
@@ -432,6 +432,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'city',
'state',
'postcode',
+ 'username',
);
if ( ! empty( $query_args['filter_empty'] ) ) {
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php
index 9b6630d94d2..a7570b171b6 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php
@@ -597,6 +597,40 @@ class WC_Admin_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case {
$this->assertCount( 2, $reports );
}
+ /**
+ * @testdox Should exclude guest customers when filter_empty includes username.
+ */
+ public function test_get_reports_with_filter_empty_username() {
+ wp_set_current_user( $this->user );
+ WC_Helper_Reports::reset_stats_dbs();
+
+ $customer = WC_Helper_Customer::create_customer( 'user_customer', 'password', 'user_customer@example.com' );
+
+ $guest_order = WC_Helper_Order::create_order( 0 );
+ $guest_order->set_billing_email( 'guest_customer@example.com' );
+ $guest_order->save();
+
+ $this->assertNotFalse( CustomersDataStore::update_registered_customer( $customer->get_id() ) );
+ $this->assertGreaterThan( 0, CustomersDataStore::get_or_create_customer_from_order( $guest_order ) );
+
+ $request = new WP_REST_Request( 'GET', $this->endpoint );
+ $request->set_query_params( array( 'filter_empty' => array( 'email' ) ) );
+ $response = $this->server->dispatch( $request );
+ $this->assertCount( 2, $response->get_data(), 'Both the registered customer and the guest have an email' );
+
+ $request->set_query_params( array( 'filter_empty' => array( 'username' ) ) );
+ $response = $this->server->dispatch( $request );
+ $reports = $response->get_data();
+ $this->assertEquals( 200, $response->get_status() );
+ $this->assertCount( 1, $reports );
+ $this->assertEquals( $customer->get_id(), $reports[0]['user_id'] );
+ $this->assertEquals( 'user_customer', $reports[0]['username'] );
+
+ $request->set_query_params( array( 'filter_empty' => array( 'username', 'email' ) ) );
+ $response = $this->server->dispatch( $request );
+ $this->assertCount( 1, $response->get_data() );
+ }
+
/**
* Test customer user profile name priority.
*/