diff --git a/includes/reader-revenue/class-stripe-connection.php b/includes/reader-revenue/class-stripe-connection.php index 58b3b1fe17..929738e11f 100644 --- a/includes/reader-revenue/class-stripe-connection.php +++ b/includes/reader-revenue/class-stripe-connection.php @@ -662,9 +662,10 @@ public static function receive_webhook( $request ) { return $customer; } + $active_subs = 0; if ( Donations::is_woocommerce_suite_active() ) { if ( $payload['ended_at'] ) { - WooCommerce_Connection::end_subscription( + $active_subs = WooCommerce_Connection::end_subscription( $payload['id'], $payload['ended_at'] ); @@ -679,7 +680,7 @@ public static function receive_webhook( $request ) { Newspack_Newsletters::$metadata_keys['sub_end_date'] => $sub_end_date, ], ]; - if ( in_array( $payload['plan']['interval'], [ 'month', 'year' ] ) ) { + if ( 0 === $active_subs && in_array( $payload['plan']['interval'], [ 'month', 'year' ] ) ) { $membership_status = 'Ex-' . self::get_membership_status_field_value( $payload['plan']['interval'] ); $contact['metadata'][ Newspack_Newsletters::$metadata_keys['membership_status'] ] = $membership_status; } diff --git a/includes/reader-revenue/class-woocommerce-connection.php b/includes/reader-revenue/class-woocommerce-connection.php index 7a75627ad5..b2c3d3654d 100644 --- a/includes/reader-revenue/class-woocommerce-connection.php +++ b/includes/reader-revenue/class-woocommerce-connection.php @@ -542,10 +542,14 @@ public static function create_transaction( $order_data ) { * * @param string $stripe_subscription_id Stripe subscription ID. * @param int $end_date Timestamp of when to cancel the subscription. + * + * @return int Number of remaining active subscriptions for the user. */ public static function end_subscription( $stripe_subscription_id, $end_date ) { $subscription = self::get_subscription_by_stripe_subscription_id( $stripe_subscription_id ); + $active_subs = 0; if ( $subscription ) { + $wc_user_id = $subscription->get_user_id(); $subscription->delete_date( 'next_payment' ); $subscription->update_dates( [ @@ -555,7 +559,19 @@ public static function end_subscription( $stripe_subscription_id, $end_date ) { $subscription->set_status( 'cancelled' ); $subscription->save(); Logger::log( 'Cancelled WC subscription with id: ' . $subscription->get_id() ); + + if ( $wc_user_id && function_exists( 'wcs_get_users_subscriptions' ) ) { + $all_subs = \wcs_get_users_subscriptions( $wc_user_id ); + + foreach ( $all_subs as $sub ) { + if ( 'active' === $sub->get_status() ) { + $active_subs ++; + } + } + } } + + return $active_subs; } /**