diff --git a/changelog/issue-6510-deprecate-wcpay-subscriptions b/changelog/issue-6510-deprecate-wcpay-subscriptions new file mode 100644 index 00000000000..b40eaae8139 --- /dev/null +++ b/changelog/issue-6510-deprecate-wcpay-subscriptions @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Only display the WCPay Subscriptions setting to existing users as part of deprecating this feature. diff --git a/client/settings/advanced-settings/wcpay-subscriptions-toggle.js b/client/settings/advanced-settings/wcpay-subscriptions-toggle.js index 0f1f3f2937b..687ffa723b9 100644 --- a/client/settings/advanced-settings/wcpay-subscriptions-toggle.js +++ b/client/settings/advanced-settings/wcpay-subscriptions-toggle.js @@ -35,7 +35,7 @@ const WCPaySubscriptionsToggle = () => { * for wcpay subscriptions or if wcpay subscriptions are already enabled. */ return ! wcpaySettings.isSubscriptionsActive && - ( isWCPaySubscriptionsEligible || isWCPaySubscriptionsEnabled ) ? ( + isWCPaySubscriptionsEligible ? ( 1, + 'subscription_status' => 'any', + 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => '_wcpay_subscription_id', + 'compare' => 'EXISTS', + ], + ], + ] + ); + + if ( count( $wcpay_subscriptions ) > 0 ) { + return true; + } } - $store_base_location = wc_get_base_location(); - return ! empty( $store_base_location['country'] ) && 'US' === $store_base_location['country']; + /** + * Check if they have at least 1 Stripe Billing enabled product. + */ + $stripe_billing_meta_query_handler = function ( $query, $query_vars ) { + if ( ! empty( $query_vars['stripe_billing_product'] ) ) { + $query['meta_query'][] = [ + 'key' => '_wcpay_product_hash', + 'compare' => 'EXISTS', + ]; + } + + return $query; + }; + + add_filter( 'woocommerce_product_data_store_cpt_get_products_query', $stripe_billing_meta_query_handler, 10, 2 ); + + $subscription_products = wc_get_products( + [ + 'limit' => 1, + 'type' => [ 'subscription', 'variable-subscription' ], + 'status' => 'publish', + 'return' => 'ids', + 'stripe_billing_product' => 'true', + ] + ); + + remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', $stripe_billing_meta_query_handler, 10, 2 ); + + if ( count( $subscription_products ) > 0 ) { + return true; + } + + return false; } /** diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index ab8b476503c..bee75837a4a 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -654,6 +654,8 @@ public static function init() { add_action( 'woocommerce_onboarding_profile_data_updated', 'WC_Payments_Features::maybe_enable_wcpay_subscriptions_after_onboarding', 10, 2 ); } + add_action( 'woocommerce_woocommerce_payments_updated', [ __CLASS__, 'maybe_disable_wcpay_subscriptions_on_update' ] ); + add_action( 'rest_api_init', [ __CLASS__, 'init_rest_api' ] ); add_action( 'woocommerce_woocommerce_payments_updated', [ __CLASS__, 'set_plugin_activation_timestamp' ] ); @@ -1780,4 +1782,15 @@ private static function should_load_stripe_billing_integration() { ) ); } + + /** + * Disable the WCPay Subscriptions feature on WooPayments plugin update if it's enabled and the store is no longer eligible. + * + * @see WC_Payments_Features::is_wcpay_subscriptions_eligible() for eligibility criteria. + */ + public static function maybe_disable_wcpay_subscriptions_on_update() { + if ( WC_Payments_Features::is_wcpay_subscriptions_enabled() && ( class_exists( 'WC_Subscriptions' ) || ! WC_Payments_Features::is_wcpay_subscriptions_eligible() ) ) { + update_option( WC_Payments_Features::WCPAY_SUBSCRIPTIONS_FLAG_NAME, '0' ); + } + } }