Skip to content

Commit

Permalink
Update Subscriptions with WooPayments eligibility as we move to depre…
Browse files Browse the repository at this point in the history
…cate this functionality (#7117)

Co-authored-by: James Allan <[email protected]>
  • Loading branch information
mattallan and james-allan authored Sep 12, 2023
1 parent 83c0c4c commit 16d8da6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
4 changes: 4 additions & 0 deletions changelog/issue-6510-deprecate-wcpay-subscriptions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Only display the WCPay Subscriptions setting to existing users as part of deprecating this feature.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
<CheckboxControl
label={ sprintf(
/* translators: %s: WooPayments */
Expand Down
65 changes: 60 additions & 5 deletions includes/class-wc-payments-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,72 @@ public static function is_wcpay_subscriptions_enabled() {
}

/**
* Returns whether WCPay Subscriptions is eligible, based on the stores base country.
* Returns whether the store is eligible to use WCPay Subscriptions (the free subscriptions bundled in WooPayments)
*
* Stores are eligible for the WCPay Subscriptions feature if:
* 1. The store has existing WCPay Subscriptions, or
* 2. The store has Stripe Billing product metadata on at least 1 product subscription product.
*
* @return bool
*/
public static function is_wcpay_subscriptions_eligible() {
if ( ! function_exists( 'wc_get_base_location' ) ) {
return false;
/**
* Check if they have at least 1 WCPay Subscription.
*
* Note: this is only possible if WCPay Subscriptions is enabled, otherwise the wcs_get_subscriptions function wouldn't exist.
*/
if ( function_exists( 'wcs_get_subscriptions' ) ) {
$wcpay_subscriptions = wcs_get_subscriptions(
[
'subscriptions_per_page' => 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;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions includes/subscriptions/class-wc-payments-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}

/**
Expand Down Expand Up @@ -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' );
}
}
}

0 comments on commit 16d8da6

Please sign in to comment.