Commit 11b1c68d5fe for woocommerce

commit 11b1c68d5fe8a6c4658a4a5f26e38ebb2fb1d122
Author: Lazizbek Ergashev <lazerg2@gmail.com>
Date:   Wed Nov 25 17:57:38 2026 +0500

    Erase download logs before anonymizing orders in personal data erasure (#71422)

    * Erase download logs before anonymizing orders in personal data erasure

    * Erase download logs of guest orders placed with a registered customer's email

diff --git a/plugins/woocommerce/changelog/71391-erase-download-logs b/plugins/woocommerce/changelog/71391-erase-download-logs
new file mode 100644
index 00000000000..47b38abcb8f
--- /dev/null
+++ b/plugins/woocommerce/changelog/71391-erase-download-logs
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Remove download logs and permissions when erasing personal data with download and order removal enabled.
diff --git a/plugins/woocommerce/includes/class-wc-privacy-erasers.php b/plugins/woocommerce/includes/class-wc-privacy-erasers.php
index d7bc03f0316..5ec3668ad7f 100644
--- a/plugins/woocommerce/includes/class-wc-privacy-erasers.php
+++ b/plugins/woocommerce/includes/class-wc-privacy-erasers.php
@@ -201,10 +201,9 @@ class WC_Privacy_Erasers {

 		// Revoke download permissions.
 		if ( apply_filters( 'woocommerce_privacy_erase_download_personal_data', $erasure_enabled, $email_address ) ) {
+			$result = $customer_download_data_store->delete_by_user_email( $email_address );
 			if ( $user instanceof WP_User ) {
-				$result = $customer_download_data_store->delete_by_user_id( (int) $user->ID );
-			} else {
-				$result = $customer_download_data_store->delete_by_user_email( $email_address );
+				$result = $customer_download_data_store->delete_by_user_id( (int) $user->ID ) || $result;
 			}
 			if ( $result ) {
 				$response['messages'][]    = __( 'Removed access to downloadable files.', 'woocommerce' );
diff --git a/plugins/woocommerce/includes/class-wc-privacy.php b/plugins/woocommerce/includes/class-wc-privacy.php
index 0026721de56..782148f753c 100644
--- a/plugins/woocommerce/includes/class-wc-privacy.php
+++ b/plugins/woocommerce/includes/class-wc-privacy.php
@@ -69,8 +69,8 @@ class WC_Privacy extends WC_Abstract_Privacy {

 		// This hook registers WooCommerce data erasers.
 		$this->add_eraser( 'woocommerce-customer-data', __( 'WooCommerce Customer Data', 'woocommerce' ), array( 'WC_Privacy_Erasers', 'customer_data_eraser' ) );
-		$this->add_eraser( 'woocommerce-customer-orders', __( 'WooCommerce Customer Orders', 'woocommerce' ), array( 'WC_Privacy_Erasers', 'order_data_eraser' ) );
 		$this->add_eraser( 'woocommerce-customer-downloads', __( 'WooCommerce Customer Downloads', 'woocommerce' ), array( 'WC_Privacy_Erasers', 'download_data_eraser' ) );
+		$this->add_eraser( 'woocommerce-customer-orders', __( 'WooCommerce Customer Orders', 'woocommerce' ), array( 'WC_Privacy_Erasers', 'order_data_eraser' ) );
 		$this->add_eraser( 'woocommerce-customer-tokens', __( 'WooCommerce Customer Payment Tokens', 'woocommerce' ), array( 'WC_Privacy_Erasers', 'customer_tokens_eraser' ) );
 	}

diff --git a/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php b/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php
index b95c2e3b01f..258b4ea889b 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php
@@ -124,4 +124,103 @@ class WC_Privacy_Test extends WC_Unit_Test_Case {
 		$this->assertContains( 'Removed customer email from coupon usage records', $response['messages'] );
 		$this->assertSame( array( '777:owner@example.com' ), get_post_meta( $coupon_id, '_used_by_email', false ) );
 	}
+
+	/**
+	 * @testdox Erasing a customer with order and download removal enabled removes their download logs.
+	 * @dataProvider customer_type_provider
+	 *
+	 * @param bool $registered Whether the erased customer has an account.
+	 * @return void
+	 */
+	public function test_erasers_remove_download_logs_of_anonymized_orders( bool $registered ): void {
+		update_option( 'woocommerce_erasure_request_removes_order_data', 'yes' );
+		update_option( 'woocommerce_erasure_request_removes_download_data', 'yes' );
+
+		wc_get_container()->get( Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register::class )->add_approved_directory( 'https://always.trusted' );
+		$product  = WC_Helper_Product::create_downloadable_product(
+			array(
+				array(
+					'name' => 'File',
+					'file' => 'https://always.trusted/file.pdf',
+				),
+			)
+		);
+		$customer = $registered ? WC_Helper_Customer::create_customer( 'eraseme', 'password', 'eraseme@example.com' )->get_id() : 0;
+		$erased   = $this->create_downloaded_order( $product, 'eraseme@example.com', $customer );
+		$retained = $this->create_downloaded_order( $product, 'other@example.com', 0 );
+
+		foreach ( apply_filters( 'wp_privacy_personal_data_erasers', array() ) as $eraser ) {
+			call_user_func( $eraser['callback'], 'eraseme@example.com', 1 );
+		}
+
+		$data_store = WC_Data_Store::load( 'customer-download-log' );
+		$this->assertEmpty( $data_store->get_download_logs_for_permission( $erased ), 'The erased customer has no download logs left.' );
+		$this->assertCount( 1, $data_store->get_download_logs_for_permission( $retained ), 'Other customers keep their download logs.' );
+	}
+
+	/**
+	 * @testdox Erasing a registered customer also removes download logs of guest orders placed with their email.
+	 *
+	 * @return void
+	 */
+	public function test_erasers_remove_download_logs_of_guest_orders_of_registered_customer(): void {
+		update_option( 'woocommerce_erasure_request_removes_order_data', 'yes' );
+		update_option( 'woocommerce_erasure_request_removes_download_data', 'yes' );
+
+		wc_get_container()->get( Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register::class )->add_approved_directory( 'https://always.trusted' );
+		$product = WC_Helper_Product::create_downloadable_product(
+			array(
+				array(
+					'name' => 'File',
+					'file' => 'https://always.trusted/file.pdf',
+				),
+			)
+		);
+		$guest   = $this->create_downloaded_order( $product, 'eraseme@example.com', 0 );
+		WC_Helper_Customer::create_customer( 'eraseme', 'password', 'eraseme@example.com' );
+
+		foreach ( apply_filters( 'wp_privacy_personal_data_erasers', array() ) as $eraser ) {
+			call_user_func( $eraser['callback'], 'eraseme@example.com', 1 );
+		}
+
+		$this->assertEmpty( WC_Data_Store::load( 'customer-download-log' )->get_download_logs_for_permission( $guest ), 'The guest order has no download logs left.' );
+	}
+
+	/**
+	 * Erased customer types.
+	 *
+	 * @return array<string, array<bool>>
+	 */
+	public function customer_type_provider(): array {
+		return array(
+			'guest'      => array( false ),
+			'registered' => array( true ),
+		);
+	}
+
+	/**
+	 * Create a completed order for a downloadable product, with one logged download.
+	 *
+	 * @param WC_Product $product     The downloadable product.
+	 * @param string     $email       The billing email.
+	 * @param int        $customer_id The customer ID, 0 for a guest.
+	 * @return int The download permission ID.
+	 */
+	private function create_downloaded_order( WC_Product $product, string $email, int $customer_id ): int {
+		$order = WC_Helper_Order::create_order( $customer_id, $product );
+		$order->set_billing_email( $email );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+
+		$downloads     = WC_Data_Store::load( 'customer-download' )->get_downloads( array( 'order_id' => $order->get_id() ) );
+		$permission_id = $downloads[0]->get_id();
+
+		$log = new WC_Customer_Download_Log();
+		$log->set_permission_id( $permission_id );
+		$log->set_user_id( $customer_id );
+		$log->set_user_ip_address( '1.2.3.4' );
+		$log->save();
+
+		return $permission_id;
+	}
 }