diff --git a/plugins/woocommerce/changelog/fix-show-expiration-related-notice-on-active-sub b/plugins/woocommerce/changelog/fix-show-expiration-related-notice-on-active-sub new file mode 100644 index 00000000000..3c00b65a272 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-show-expiration-related-notice-on-active-sub @@ -0,0 +1,5 @@ +Significance: patch +Type: fix + +Display admin notice of expired and expiring Woo subscription when the product +is connected / activated on the site. diff --git a/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php b/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php index 943a2686b7d..8a0822e8d7a 100644 --- a/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php +++ b/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php @@ -254,26 +254,36 @@ class WC_Helper_Updater { * @return void. */ public static function display_notice_for_expired_and_expiring_subscriptions( $plugin_data, $response ) { - // Extract product ID from the response. $product_id = preg_replace( '/[^0-9]/', '', $response->id ); - // Get the subscription details based on product ID. - $subscription = current( - wp_list_filter( - WC_Helper::get_subscriptions(), - array( 'product_id' => $product_id ) - ) - ); - - // Check if subscription is empty. - if ( empty( $subscription ) ) { + // Product subscriptions. + $subscriptions = wp_list_filter( WC_Helper::get_installed_subscriptions(), array( 'product_id' => $product_id ) ); + if ( empty( $subscriptions ) ) { return; } + $expired_subscription = current( + array_filter( + $subscriptions, + function ( $subscription ) { + return ! empty( $subscription['expired'] ) && ! $subscription['lifetime']; + } + ) + ); + + $expiring_subscription = current( + array_filter( + $subscriptions, + function ( $subscription ) { + return ! empty( $subscription['expiring'] ) && ! $subscription['autorenew']; + } + ) + ); + // Prepare the expiry notice based on subscription status. $expiry_notice = ''; - if ( ! empty( $subscription['expired'] ) && ! $subscription['lifetime'] ) { + if ( ! empty( $expired_subscription ) ) { $renew_link = add_query_arg( array( @@ -284,7 +294,7 @@ class WC_Helper_Updater { ); /* translators: 1: Product regular price */ - $product_price = ! empty( $subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $subscription['product_regular_price'] ) ) : ''; + $product_price = ! empty( $expired_subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $expired_subscription['product_regular_price'] ) ) : ''; $expiry_notice = sprintf( /* translators: 1: URL to My Subscriptions page 2: Product price */ @@ -292,18 +302,19 @@ class WC_Helper_Updater { esc_url( $renew_link ), $product_price ); - } elseif ( ! empty( $subscription['expiring'] ) && ! $subscription['autorenew'] ) { - $renew_link = add_query_arg( + } elseif ( ! empty( $expiring_subscription ) ) { + $renew_link = add_query_arg( array( 'utm_source' => 'pu', 'utm_campaign' => 'pu_plugin_screen_enable_autorenew', ), PluginsHelper::WOO_SUBSCRIPTION_PAGE_URL ); + $expiry_notice = sprintf( /* translators: 1: Expiry date 1: URL to My Subscriptions page */ __( ' Your subscription expires on %1$s, enable auto-renew to continue receiving updates.', 'woocommerce' ), - date_i18n( 'F jS', $subscription['expires'] ), + date_i18n( 'F jS', $expiring_subscription['expires'] ), esc_url( $renew_link ) ); } diff --git a/plugins/woocommerce/includes/admin/helper/class-wc-helper.php b/plugins/woocommerce/includes/admin/helper/class-wc-helper.php index 17f10d73b5a..e14f81f43c6 100644 --- a/plugins/woocommerce/includes/admin/helper/class-wc-helper.php +++ b/plugins/woocommerce/includes/admin/helper/class-wc-helper.php @@ -1297,6 +1297,35 @@ class WC_Helper { return ! empty( $subscription ); } + /** + * Get the user's connected subscriptions that are installed on the current + * site. + * + * @return array + */ + public static function get_installed_subscriptions() { + static $installed_subscriptions = null; + + // Cache installed_subscriptions in the current request. + if ( is_null( $installed_subscriptions ) ) { + $auth = WC_Helper_Options::get( 'auth' ); + $site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0; + if ( 0 === $site_id ) { + $installed_subscriptions = array(); + return $installed_subscriptions; + } + + $installed_subscriptions = array_filter( + self::get_subscriptions(), + function ( $subscription ) use ( $site_id ) { + return in_array( $site_id, $subscription['connections'], true ); + } + ); + } + + return $installed_subscriptions; + } + /** * Get subscription state of a given product ID. * diff --git a/plugins/woocommerce/src/Admin/PluginsHelper.php b/plugins/woocommerce/src/Admin/PluginsHelper.php index 28441d51616..1c56f1c7308 100644 --- a/plugins/woocommerce/src/Admin/PluginsHelper.php +++ b/plugins/woocommerce/src/Admin/PluginsHelper.php @@ -827,6 +827,7 @@ class PluginsHelper { $subscriptions, function ( $sub ) { return ( ! empty( $sub['local']['installed'] ) && ! empty( $sub['product_key'] ) ) + && $sub['active'] && $sub['expiring'] && ! $sub['autorenew']; }, @@ -898,6 +899,7 @@ class PluginsHelper { $subscriptions, function ( $sub ) { return ( ! empty( $sub['local']['installed'] ) && ! empty( $sub['product_key'] ) ) + && $sub['active'] && $sub['expired'] && ! $sub['lifetime']; },