Commit 5ca2eb2ec01 for woocommerce
commit 5ca2eb2ec012257a54fdaec81c682c44c0168038
Author: Anuj Singh <80690679+Anuj-Rathore24@users.noreply.github.com>
Date: Fri Jun 5 12:18:52 2026 +0530
Fix guest purchases in wc_customer_bought_product when user ID is supplied (#65517)
* feat: Fix guest purchases in wc_customer_bought_product when user ID is supplied
* feat: Update aggregation version to force cache refresh
* chore: Add changelog
* feat: Add test for empty email in wc_customer_bought_product
diff --git a/plugins/woocommerce/changelog/fix-customer-bought-product-guest-email b/plugins/woocommerce/changelog/fix-customer-bought-product-guest-email
new file mode 100644
index 00000000000..d93346fd5a6
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-customer-bought-product-guest-email
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix wc_customer_bought_product() returning false for unlinked guest orders when a matching user ID is supplied.
diff --git a/plugins/woocommerce/includes/wc-user-functions.php b/plugins/woocommerce/includes/wc-user-functions.php
index 2d15d5621f9..f4c28eb9392 100644
--- a/plugins/woocommerce/includes/wc-user-functions.php
+++ b/plugins/woocommerce/includes/wc-user-functions.php
@@ -439,7 +439,7 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
$cache_version = WC_Cache_Helper::get_transient_version( 'orders' );
}
- $aggregation_version = 'v2'; // Update the version when modifying the aggregation implementation to ensure the cache is repopulated.
+ $aggregation_version = 'v3'; // Update the version when modifying the aggregation implementation to ensure the cache is repopulated.
$cache_group = 'orders';
$cache_key = 'wc_customer_bought_product_' . md5( $customer_email . '-' . $user_id . '-' . $use_lookup_tables . '-' . $aggregation_version );
$cache_value = wp_cache_get( $cache_key, $cache_group );
@@ -449,8 +449,7 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
} else {
// Identify the customer using the provided data. Use Customer ID to optimize performance in the following SQL
// queries. If an account has been deleted, use the supplied ID to ensure graceful handling.
- $user = null;
- $original_user_id = $user_id;
+ $user = null;
if ( ! $user_id && $customer_email && is_email( $customer_email ) ) {
$user = get_user_by( 'email', $customer_email );
$user_id = $user->ID ?? $user_id;
@@ -460,14 +459,7 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
$user_id = $user->ID ?? $user_id;
}
- // Deduplicate emails to ensure the Customer ID remains the primary performance driver in subsequent SQL queries.
$emails = array( $customer_email );
- if ( $original_user_id ) {
- $user_email = $user->user_email ?? '';
- if ( $user_email && is_email( $user_email ) && strtolower( $user_email ) === strtolower( $customer_email ) ) {
- $emails = array();
- }
- }
$emails = array_unique( array_filter( $emails, static fn( $email ) => $email && is_email( $email ) ) );
if ( empty( $emails ) && ! $user_id ) {
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/customer/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/customer/functions.php
index bacc447aa90..ae33244281f 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/customer/functions.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/customer/functions.php
@@ -330,6 +330,8 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
$this->assertTrue( wc_customer_bought_product( 'billing@example.com', 0, $product_id_4 ) );
$this->assertTrue( wc_customer_bought_product( 'test@example.com', 0, $product_id_5 ) );
+ $this->assertTrue( wc_customer_bought_product( 'test@example.com', $customer_id_1, $product_id_5 ) );
+ $this->assertFalse( wc_customer_bought_product( '', $customer_id_1, $product_id_5 ) );
remove_filter( 'woocommerce_customer_bought_product_use_lookup_tables', $lookup_tables );
}
diff --git a/plugins/woocommerce/tests/php/includes/wc-user-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-user-functions-test.php
index ef0f701ddb0..465885cbe89 100644
--- a/plugins/woocommerce/tests/php/includes/wc-user-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-user-functions-test.php
@@ -108,6 +108,8 @@ class WC_User_Functions_Tests extends WC_Unit_Test_Case {
$this->assertTrue( wc_customer_bought_product( 'billing@example.com', 0, $product_id_4 ) );
$this->assertTrue( wc_customer_bought_product( 'test@example.com', 0, $product_id_5 ) );
+ $this->assertTrue( wc_customer_bought_product( 'test@example.com', $customer_id_1, $product_id_5 ) );
+ $this->assertFalse( wc_customer_bought_product( '', $customer_id_1, $product_id_5 ) );
remove_filter( 'woocommerce_customer_bought_product_use_lookup_tables', $lookup_tables );
}