Commit 752643a3d88 for woocommerce

commit 752643a3d8881c047e9a0401eb2c91f8afe5bee3
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 21:30:24 2026 +0100

    Prevent guest orders from overwriting registered Analytics customers (#71225)

    Forward-port the corresponding WooCommerce 11.3.1 fix to trunk.

diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 201aeba470b..229a205cdbc 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -39667,12 +39667,6 @@ parameters:
 			count: 1
 			path: src/Admin/API/Reports/Customers/DataStore.php

-		-
-			message: '#^Cannot call method get_id\(\) on Automattic\\WooCommerce\\Admin\\API\\Reports\\Customers\\WC_Order\|WC_Order\|WC_Order_Refund\|false\.$#'
-			identifier: method.nonObject
-			count: 1
-			path: src/Admin/API/Reports/Customers/DataStore.php
-
 		-
 			message: '#^Method Automattic\\WooCommerce\\Admin\\API\\Reports\\Customers\\DataStore\:\:add_sql_query_params\(\) has no return type specified\.$#'
 			identifier: missingType.return
@@ -39739,12 +39733,6 @@ parameters:
 			count: 1
 			path: src/Admin/API/Reports/Customers/DataStore.php

-		-
-			message: '#^Method Automattic\\WooCommerce\\Admin\\API\\Reports\\Customers\\DataStore\:\:sync_order_customer\(\) should return \-1\|true but returns bool\.$#'
-			identifier: return.type
-			count: 1
-			path: src/Admin/API/Reports/Customers/DataStore.php
-
 		-
 			message: '#^Method Automattic\\WooCommerce\\Admin\\API\\Reports\\Customers\\DataStore\:\:update_registered_customer_via_last_active\(\) has no return type specified\.$#'
 			identifier: missingType.return
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php
index 7aa2f05ba4f..e2862c7dadc 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php
@@ -139,6 +139,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 		add_action( 'delete_user', array( __CLASS__, 'delete_customer_by_user_id' ) );
 		add_action( 'remove_user_from_blog', array( __CLASS__, 'delete_customer_by_user_id' ) );

+		add_action( 'woocommerce_privacy_before_remove_order_personal_data', array( __CLASS__, 'anonymize_customer' ) );
 		add_action( 'woocommerce_privacy_remove_order_personal_data', array( __CLASS__, 'anonymize_customer' ) );

 		add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 15, 2 );
@@ -186,13 +187,14 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 	/**
 	 * Sync customers data after an order was deleted.
 	 *
-	 * When an order is deleted, the customer record is deleted from the
-	 * table if the customer has no other orders.
+	 * Remove unused guest rows while retaining registered customer profiles.
 	 *
 	 * @param int $order_id Order ID.
 	 * @param int $customer_id Customer ID.
 	 */
 	public static function sync_on_order_delete( $order_id, $customer_id ) {
+		global $wpdb;
+
 		$customer_id = absint( $customer_id );

 		if ( 0 === $customer_id ) {
@@ -203,7 +205,24 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 		$order_count = self::get_order_count( $customer_id );

 		if ( 0 === $order_count ) {
-			self::delete_customer( $customer_id );
+			$deleted = $wpdb->delete(
+				self::get_db_table_name(),
+				array(
+					'customer_id' => $customer_id,
+					'user_id'     => null,
+				),
+				array( '%d', '%d' )
+			);
+			if ( $deleted ) {
+				/**
+				 * Fires when an unused guest customer is deleted.
+				 *
+				 * @since 4.0.0
+				 * @param int $customer_id Customer lookup ID.
+				 */
+				do_action( 'woocommerce_analytics_delete_customer', $customer_id );
+				ReportsCache::invalidate();
+			}
 		}
 	}

@@ -213,7 +232,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 	 * Only updates customer if it is the customers last order.
 	 *
 	 * @param int $post_id of order.
-	 * @return true|-1
+	 * @return bool|-1
 	 */
 	public static function sync_order_customer( $post_id ) {
 		global $wpdb;
@@ -238,17 +257,32 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {

 		list($data, $format) = self::get_customer_order_data_and_format( $order );

-		$result = $wpdb->update( self::get_db_table_name(), $data, array( 'customer_id' => $customer_id ), $format );
+		// Report attribution does not authorize updating a different customer's data.
+		$user_id = (int) $order->get_customer_id();
+		$result  = $wpdb->update(
+			self::get_db_table_name(),
+			$data,
+			array(
+				'customer_id' => $customer_id,
+				'user_id'     => $user_id > 0 ? $user_id : null,
+			),
+			$format,
+			array( '%d', '%d' )
+		);
+
+		if ( 1 !== $result ) {
+			return false;
+		}

 		/**
-		 * Fires when a customer is updated.
+		 * Fires after an order changes a customer's report data.
 		 *
 		 * @param int $customer_id Customer ID.
 		 * @since 4.0.0
 		 */
 		do_action( 'woocommerce_analytics_update_customer', $customer_id );

-		return 1 === $result;
+		return true;
 	}

 	/**
@@ -1148,6 +1182,10 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 			$order = wc_get_order( absint( $order ) );
 		}

+		if ( ! $order instanceof \WC_Order ) {
+			return;
+		}
+
 		$customer_id = $wpdb->get_var(
 			$wpdb->prepare( "SELECT customer_id FROM {$wpdb->prefix}wc_order_stats WHERE order_id = %d", $order->get_id() )
 		);
@@ -1174,7 +1212,8 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 						billing_phone = %s,
 						shipping_phone = %s
 					WHERE
-						customer_id = %d",
+						customer_id = %d
+						AND ( user_id = %d OR ( user_id IS NULL AND %d = 0 ) )",
 				array(
 					$deleted_text,
 					$deleted_text,
@@ -1186,6 +1225,8 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
 					$deleted_text,
 					$deleted_text,
 					$customer_id,
+					$order->get_customer_id(),
+					$order->get_customer_id(),
 				)
 			)
 		);
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/reports/class-wc-tests-reports-customers.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/reports/class-wc-tests-reports-customers.php
index 0e77c8adff4..8ea317b64a5 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/reports/class-wc-tests-reports-customers.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/reports/class-wc-tests-reports-customers.php
@@ -6,12 +6,180 @@
  */

 use Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore;
+use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
+use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
+use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;

 /**
  * Class WC_Admin_Tests_Reports_Customers
  */
 class WC_Admin_Tests_Reports_Customer extends WC_Unit_Test_Case {

+	/**
+	 * @testdox Should preserve registered customer data when importing guest orders with the same email.
+	 */
+	public function test_guest_import_preserves_registered_customer_data(): void {
+		WC_Helper_Reports::reset_stats_dbs();
+		$customer = WC_Helper_Customer::create_customer();
+		CustomersDataStore::update_registered_customer( $customer->get_id() );
+		$customer_id       = CustomersDataStore::get_customer_id_by_user_id( $customer->get_id() );
+		$original_row      = $this->get_customer_record( $customer_id );
+		$updated_customers = array();
+		add_action(
+			'woocommerce_analytics_update_customer',
+			static function ( $updated_customer_id ) use ( &$updated_customers ) {
+				$updated_customers[] = $updated_customer_id;
+			}
+		);
+
+		$order = new WC_Order();
+		$order->set_billing_email( $customer->get_email() );
+		$order->set_billing_first_name( 'Guest' );
+		$order->set_billing_last_name( 'Customer' );
+		$order->set_billing_city( 'Boston' );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+		OrdersScheduler::import( $order->get_id() );
+
+		$this->assertEquals( $original_row, $this->get_customer_record( $customer_id ), 'A guest import must preserve the entire registered lookup row.' );
+		$this->assertSame( $customer_id, (int) CustomersDataStore::get_existing_customer_id_from_order( $order ), 'Historical email attribution should remain unchanged.' );
+		$this->assertSame( array(), $updated_customers, 'A blocked update must not emit a customer-update notification.' );
+	}
+
+	/**
+	 * @testdox Should protect registered customer data on repeated guest imports after the billing email changes.
+	 */
+	public function test_guest_reimport_preserves_registered_customer_data(): void {
+		WC_Helper_Reports::reset_stats_dbs();
+		$customer = WC_Helper_Customer::create_customer();
+		CustomersDataStore::update_registered_customer( $customer->get_id() );
+		$customer_id  = CustomersDataStore::get_customer_id_by_user_id( $customer->get_id() );
+		$original_row = $this->get_customer_record( $customer_id );
+
+		$order = new WC_Order();
+		$order->set_billing_email( $customer->get_email() );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+		OrdersStatsDataStore::sync_order( $order->get_id() );
+
+		$order->set_billing_email( 'updated-guest@example.com' );
+		$order->set_billing_first_name( 'Updated Guest' );
+		$order->set_billing_city( 'Boston' );
+		$order->save();
+		OrdersScheduler::import( $order->get_id() );
+		OrdersScheduler::import( $order->get_id() );
+
+		$this->assertEquals( $original_row, $this->get_customer_record( $customer_id ), 'A stale order statistics association must not authorize a registered customer update.' );
+		$this->assertSame( $customer_id, (int) CustomersDataStore::get_existing_customer_id_from_order( $order ), 'The existing order statistics association should remain unchanged.' );
+		$this->assertFalse( CustomersDataStore::sync_order_customer( $order->get_id() ), 'A guest order must not report updating a registered lookup row.' );
+	}
+
+	/**
+	 * @testdox Should update customer data when the order and lookup row have matching user identities.
+	 * @testWith [true]
+	 *           [false]
+	 * @param bool $registered Whether to use a registered customer or a guest.
+	 */
+	public function test_sync_order_customer_with_matching_identity( bool $registered ): void {
+		WC_Helper_Reports::reset_stats_dbs();
+		$customer = $registered ? WC_Helper_Customer::create_customer() : null;
+		if ( $customer ) {
+			CustomersDataStore::update_registered_customer( $customer->get_id() );
+		}
+
+		$order = new WC_Order();
+		$order->set_customer_id( $customer ? $customer->get_id() : 0 );
+		$order->set_billing_email( $customer ? $customer->get_email() : 'guest@example.com' );
+		$order->set_billing_city( 'Boston' );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+		OrdersStatsDataStore::sync_order( $order->get_id() );
+		$customer_id = (int) CustomersDataStore::get_existing_customer_id_from_order( $order );
+
+		$order->set_billing_city( 'Chicago' );
+		$order->save();
+		$updated_customers = array();
+		add_action(
+			'woocommerce_analytics_update_customer',
+			static function ( $updated_customer_id ) use ( &$updated_customers ) {
+				$updated_customers[] = (int) $updated_customer_id;
+			}
+		);
+
+		$this->assertTrue( CustomersDataStore::sync_order_customer( $order->get_id() ), 'Matching identities should permit an update.' );
+		$row = $this->get_customer_record( $customer_id )[0];
+		$this->assertSame( 'Chicago', $row->city, 'The lookup row should receive the latest order location.' );
+		$this->assertSame( $customer ? (string) $customer->get_id() : null, $row->user_id, 'Synchronizing must preserve the registered or guest identity.' );
+		$this->assertSame( array( $customer_id ), $updated_customers, 'An actual update should emit one customer-update notification.' );
+		$this->assertFalse( CustomersDataStore::sync_order_customer( $order->get_id() ), 'An unchanged lookup row should report no update.' );
+		$this->assertSame( array( $customer_id ), $updated_customers, 'A no-op synchronization should not emit another notification.' );
+	}
+
+	/**
+	 * @testdox Removing a guest order preserves a registered lookup row attributed by email.
+	 * @testWith ["delete"]
+	 *           ["erase"]
+	 * @param string $operation Order operation.
+	 */
+	public function test_guest_removal_preserves_registered_identity( string $operation ): void {
+		WC_Helper_Reports::reset_stats_dbs();
+		$customer = WC_Helper_Customer::create_customer();
+		CustomersDataStore::update_registered_customer( $customer->get_id() );
+		$customer_id = CustomersDataStore::get_customer_id_by_user_id( $customer->get_id() );
+		$order       = WC_Helper_Order::create_order( 0 );
+		$order->set_billing_email( $customer->get_email() );
+		$order->save();
+		OrdersScheduler::import( $order->get_id() );
+		$before = $this->get_customer_record( $customer_id );
+
+		if ( 'delete' === $operation ) {
+			$order->delete( true );
+		} else {
+			WC_Privacy_Erasers::remove_order_personal_data( $order );
+		}
+
+		$this->assertEquals( $before, $this->get_customer_record( $customer_id ), 'Guest order cleanup must preserve the registered customer profile.' );
+	}
+
+	/**
+	 * @testdox Erasing an order anonymizes its own guest or registered Analytics identity.
+	 * @testWith [false]
+	 *           [true]
+	 * @param bool $registered Whether the order belongs to a registered customer.
+	 */
+	public function test_order_erasure_preserves_authorized_anonymization( bool $registered ): void {
+		WC_Helper_Reports::reset_stats_dbs();
+		$customer = $registered ? WC_Helper_Customer::create_customer() : null;
+		$order    = WC_Helper_Order::create_order( $customer ? $customer->get_id() : 0 );
+		$order->set_billing_email( $customer ? $customer->get_email() : 'erase-guest@example.com' );
+		$order->save();
+		OrdersScheduler::import( $order->get_id() );
+		$customer_id = CustomersDataStore::get_existing_customer_id_from_order( $order );
+
+		WC_Privacy_Erasers::remove_order_personal_data( $order );
+
+		$row = $this->get_customer_record( $customer_id )[0];
+		$this->assertNull( $row->user_id, 'Authorized erasure must remove the Analytics account association.' );
+		$this->assertSame( 'deleted@site.invalid', $row->email, 'Authorized erasure must remove the Analytics email.' );
+	}
+
+	/**
+	 * @testdox Deleting the last guest order removes its guest Analytics row.
+	 */
+	public function test_last_guest_order_deletion_cleans_lookup(): void {
+		WC_Helper_Reports::reset_stats_dbs();
+		$order = WC_Helper_Order::create_order( 0 );
+		$order->set_billing_email( 'delete-guest@example.com' );
+		$order->save();
+		OrdersScheduler::import( $order->get_id() );
+		$customer_id = CustomersDataStore::get_existing_customer_id_from_order( $order );
+
+		$order->delete( true );
+
+		$this->assertCount( 0, $this->get_customer_record( $customer_id ), 'Unused guest rows should still be removed.' );
+	}
+
 	/**
 	 * Test order count calculation for customer.
 	 *
@@ -53,13 +221,13 @@ class WC_Admin_Tests_Reports_Customer extends WC_Unit_Test_Case {
 	}

 	/**
-	 * Test customer lookup tables are cleaned after deleting an order.
+	 * Test registered customer lookup rows survive order deletion.
 	 *
-	 * A customer record should only be deleted if the customer has no other orders.
+	 * Registered profiles remain until account or privacy operations remove them.
 	 *
 	 * @covers \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::sync_on_order_delete
 	 */
-	public function test_order_deletion_removes_customer() {
+	public function test_order_deletion_preserves_registered_customer() {
 		WC_Helper_Reports::reset_stats_dbs();

 		// Create a customer.
@@ -95,9 +263,9 @@ class WC_Admin_Tests_Reports_Customer extends WC_Unit_Test_Case {
 		$order1->delete( true );
 		$this->assertCount( 1, $this->get_customer_record( $customer_id ), 'customer remains' );

-		// Customer should be removed in lookup table after both orders are deleted.
+		// Registered profiles remain available after their last order is deleted.
 		$order2->delete( true );
-		$this->assertCount( 0, $this->get_customer_record( $customer_id ), 'customer removed' );
+		$this->assertCount( 1, $this->get_customer_record( $customer_id ), 'registered customer remains' );
 	}

 	/**