From 16d8da60fdb93de3fceb903b696c029afbe91487 Mon Sep 17 00:00:00 2001 From: Matt Allan Date: Tue, 12 Sep 2023 13:50:35 +1000 Subject: [PATCH] Update Subscriptions with WooPayments eligibility as we move to deprecate this functionality (#7117) Co-authored-by: James Allan --- .../issue-6510-deprecate-wcpay-subscriptions | 4 ++ .../wcpay-subscriptions-toggle.js | 7 +- includes/class-wc-payments-features.php | 65 +++++++++++++++++-- .../class-wc-payments-subscriptions.php | 13 ++++ 4 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 changelog/issue-6510-deprecate-wcpay-subscriptions 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 e2f921c990c..3305739e565 100644 --- a/client/settings/advanced-settings/wcpay-subscriptions-toggle.js +++ b/client/settings/advanced-settings/wcpay-subscriptions-toggle.js @@ -30,11 +30,8 @@ const WCPaySubscriptionsToggle = () => { updateIsWCPaySubscriptionsEnabled( value ); }; - /** - * Only show the toggle if the site is eligible for wcpay subscriptions or - * if wcpay subscriptions are already enabled. - */ - return isWCPaySubscriptionsEligible || isWCPaySubscriptionsEnabled ? ( + return ! wcpaySettings.isSubscriptionsActive && + 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/subscriptions/class-wc-payments-subscriptions.php b/includes/subscriptions/class-wc-payments-subscriptions.php index c30c6248d5c..cd0a5101337 100644 --- a/includes/subscriptions/class-wc-payments-subscriptions.php +++ b/includes/subscriptions/class-wc-payments-subscriptions.php @@ -95,6 +95,8 @@ public static function init( WC_Payments_API_Client $api_client, WC_Payments_Cus include_once __DIR__ . '/class-wc-payments-subscriptions-migrator.php'; self::$stripe_billing_migrator = new WC_Payments_Subscriptions_Migrator( $api_client ); } + + add_action( 'woocommerce_woocommerce_payments_updated', [ __CLASS__, 'maybe_disable_wcpay_subscriptions_on_update' ] ); } /** @@ -156,4 +158,15 @@ public static function is_duplicate_site() { return class_exists( 'WCS_Staging' ) && WCS_Staging::is_duplicate_site(); } + + /** + * 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() && ! WC_Payments_Features::is_wcpay_subscriptions_eligible() ) { + update_option( WC_Payments_Features::WCPAY_SUBSCRIPTIONS_FLAG_NAME, '0' ); + } + } }