Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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' );
}
Expand Down
40 changes: 40 additions & 0 deletions includes/plugins/woocommerce/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Loading