From 21c57e72a4e410dd65b40ab6b03bd8e9602901d1 Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Tue, 17 Mar 2026 17:25:44 -0400 Subject: [PATCH] fix: identify returning customers --- .../class-contact-sync-connector.php | 16 +++++--- .../class-woocommerce-connection.php | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/includes/data-events/connectors/class-contact-sync-connector.php b/includes/data-events/connectors/class-contact-sync-connector.php index 0f0bd61f7a..4a9a337337 100644 --- a/includes/data-events/connectors/class-contact-sync-connector.php +++ b/includes/data-events/connectors/class-contact-sync-connector.php @@ -87,6 +87,10 @@ public static function store_user_data_before_deletion( $user_id ) { * @param int $client_id ID of the client that triggered the event. */ public static function reader_registered( $timestamp, $data, $client_id ) { + if ( ! class_exists( 'WC_Customer' ) ) { + return; + } + if ( empty( $data['user_id'] ) ) { return; } @@ -106,18 +110,20 @@ public static function reader_registered( $timestamp, $data, $client_id ) { * @param int $client_id ID of the client that triggered the event. */ public static function reader_logged_in( $timestamp, $data, $client_id ) { - if ( empty( $data['email'] ) || empty( $data['user_id'] ) ) { + if ( ! class_exists( 'WC_Customer' ) ) { return; } - $customer = new \WC_Customer( $data['user_id'] ); + if ( empty( $data['email'] ) || empty( $data['user_id'] ) ) { + return; + } - // If user is not a Woo customer, don't need to sync them. - if ( ! $customer->get_order_count() && empty( WooCommerce_Connection::get_active_subscriptions_for_user( $data['user_id'] ) ) ) { + if ( ! WooCommerce_Connection::is_returning_customer( $data['user_id'] ) ) { return; } - $contact = Sync_WooCommerce::get_contact_from_customer( $customer ); + $customer = new \WC_Customer( $data['user_id'] ); + $contact = Sync_WooCommerce::get_contact_from_customer( $customer ); Contact_Sync::sync( $contact, 'RAS Reader login' ); } diff --git a/includes/plugins/woocommerce/class-woocommerce-connection.php b/includes/plugins/woocommerce/class-woocommerce-connection.php index 7dc241d1cf..9b3563e876 100644 --- a/includes/plugins/woocommerce/class-woocommerce-connection.php +++ b/includes/plugins/woocommerce/class-woocommerce-connection.php @@ -959,6 +959,46 @@ public static function wc_memberships_for_teams_filter_team_name_in_form( $args, return $args; } + + /** + * Check whether the customer is considered a "returning customer" for the purposes of our reader activation flows. + * + * @param int $customer_id The customer ID to check. + * + * @return bool True if the customer is a returning customer, false otherwise. + */ + public static function is_returning_customer( $customer_id ) { + $is_returning_customer = false; + if ( ! class_exists( 'WC_Customer' ) ) { + return $is_returning_customer; + } + + $customer = new \WC_Customer( $user_id ); + if ( $customer->get_order_count() > 0 ) { + $is_returning_customer = true; + } + + if ( ! $is_returning_customer && function_exists( 'wcs_user_has_subscription' ) ) { + $is_returning_customer = wcs_user_has_subscription( + $customer_id, + 0, + [ + 'active', + 'pending-cancel', + 'cancelled', + 'expired', + ] + ); + } + + /** + * Filters whether a user is considered a returning customer. + * + * @param bool $is_returning_customer Whether the user is a returning customer. + * @param int $customer_id The customer ID. + */ + return apply_filters( 'newspack_woocommerce_connection_is_returning_customer', $is_returning_customer, $customer_id ); + } } WooCommerce_Connection::init();