Skip to content

Commit

Permalink
Schedule individual subscription migrations and add manual migration …
Browse files Browse the repository at this point in the history
…tool under Status > Tools (#6942)

Co-authored-by: James Allan <[email protected]>
  • Loading branch information
mattallan and james-allan authored Aug 31, 2023
1 parent 1a5af35 commit d0db2a3
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 65 deletions.
5 changes: 5 additions & 0 deletions changelog/issue-6526-schedule-subscription-migration-tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: This PR is part of a larger feature coming to WCPay and not single entry is needed for this PR.


25 changes: 18 additions & 7 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public static function init() {
}

// Load WCPay Subscriptions.
if ( WC_Payments_Features::is_wcpay_subscriptions_enabled() ) {
if ( self::should_load_wcpay_subscriptions() ) {
include_once WCPAY_ABSPATH . '/includes/subscriptions/class-wc-payments-subscriptions.php';
WC_Payments_Subscriptions::init( self::$api_client, self::$customer_service, self::$order_service, self::$account );
}
Expand All @@ -633,12 +633,6 @@ public static function init() {
add_action( 'woocommerce_onboarding_profile_data_updated', 'WC_Payments_Features::maybe_enable_wcpay_subscriptions_after_onboarding', 10, 2 );
}

// Load the WCPay Subscriptions migration class.
if ( WC_Payments_Features::is_subscription_migration_enabled() ) {
include_once WCPAY_ABSPATH . '/includes/subscriptions/class-wc-payments-subscriptions-migrator.php';
new WC_Payments_Subscriptions_Migrator( self::$api_client );
}

add_action( 'rest_api_init', [ __CLASS__, 'init_rest_api' ] );
add_action( 'woocommerce_woocommerce_payments_updated', [ __CLASS__, 'set_plugin_activation_timestamp' ] );

Expand Down Expand Up @@ -1711,4 +1705,21 @@ public static function wcpay_show_old_woocommerce_for_hungary_sweden_and_czech_r
</div>
<?php
}

/**
* Determines whether we should load WCPay Subscription related classes.
*
* Return true when:
* - the WCPay Subscriptions feature flag is enabled, or
* - the migration feature flag is enabled && the store has WC Subscriptions activated
*
* @return bool
*/
private static function should_load_wcpay_subscriptions() {
if ( WC_Payments_Features::is_wcpay_subscriptions_enabled() ) {
return true;
}

return WC_Payments_Features::is_subscription_migration_enabled() && class_exists( 'WC_Subscriptions' );
}
}
7 changes: 1 addition & 6 deletions includes/subscriptions/class-wc-payments-invoice-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ public function mark_pending_invoice_paid_for_subscription( WC_Subscription $sub
* @throws API_Exception If the request to mark the invoice as paid fails.
*/
public function maybe_record_invoice_payment( int $order_id ) {

if ( WC_Payments_Subscriptions::is_duplicate_site() ) {
return;
}

$order = wc_get_order( $order_id );

if ( ! $order || self::get_order_invoice_id( $order ) ) {
Expand All @@ -203,7 +198,7 @@ public function maybe_record_invoice_payment( int $order_id ) {
foreach ( wcs_get_subscriptions_for_order( $order, [ 'order_type' => [ 'parent', 'renewal' ] ] ) as $subscription ) {
$invoice_id = self::get_subscription_invoice_id( $subscription );

if ( ! $invoice_id ) {
if ( ! $invoice_id || ! WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) {
continue;
}

Expand Down
12 changes: 8 additions & 4 deletions includes/subscriptions/class-wc-payments-product-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ public function __construct( WC_Payments_API_Client $payments_api_client ) {
return;
}

add_action( 'shutdown', [ $this, 'create_or_update_products' ] );
// Only create, update and restore/unarchive WCPay Subscription products when the WC Subscriptions plugin is not active.
if ( ! $this->is_subscriptions_plugin_active() ) {
add_action( 'shutdown', [ $this, 'create_or_update_products' ] );
add_action( 'untrashed_post', [ $this, 'maybe_unarchive_product' ] );

$this->add_product_update_listeners();
}

add_action( 'wp_trash_post', [ $this, 'maybe_archive_product' ] );
add_action( 'untrashed_post', [ $this, 'maybe_unarchive_product' ] );
add_filter( 'woocommerce_duplicate_product_exclude_meta', [ $this, 'exclude_meta_wcpay_product' ] );

$this->add_product_update_listeners();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
class WC_Payments_Subscription_Minimum_Amount_Handler {

use WC_Payments_Subscriptions_Utilities;

/**
* The API client object.
*
Expand Down Expand Up @@ -38,7 +40,10 @@ class WC_Payments_Subscription_Minimum_Amount_Handler {
*/
public function __construct( WC_Payments_API_Client $api_client ) {
$this->api_client = $api_client;
add_filter( 'woocommerce_subscriptions_minimum_processable_recurring_amount', [ $this, 'get_minimum_recurring_amount' ], 10, 2 );

if ( ! $this->is_subscriptions_plugin_active() ) {
add_filter( 'woocommerce_subscriptions_minimum_processable_recurring_amount', [ $this, 'get_minimum_recurring_amount' ], 10, 2 );
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public function maybe_attempt_payment_for_subscription( $subscription, WC_Paymen

$wcpay_invoice_id = WC_Payments_Invoice_Service::get_pending_invoice_id( $subscription );

if ( ! $wcpay_invoice_id ) {
if ( ! $wcpay_invoice_id || ! self::is_wcpay_subscription( $subscription ) ) {
return;
}

Expand Down Expand Up @@ -868,7 +868,6 @@ private function update_subscription( WC_Subscription $subscription, array $data
$response = null;

if ( ! $wcpay_subscription_id ) {
Logger::log( 'There was a problem updating the WCPay subscription in: Subscription does not contain a valid subscription ID.' );
return;
}

Expand Down Expand Up @@ -1045,7 +1044,7 @@ private function validate_subscription_data( $subscription_data ) {
* @return bool True if store has active WCPay subscriptions, otherwise false.
*/
public static function store_has_active_wcpay_subscriptions() {
$results = wcs_get_subscriptions(
$active_wcpay_subscriptions = wcs_get_subscriptions(
[
'subscriptions_per_page' => 1,
'subscription_status' => 'active',
Expand All @@ -1059,7 +1058,6 @@ public static function store_has_active_wcpay_subscriptions() {
]
);

$store_has_active_wcpay_subscriptions = count( $results ) > 0;
return $store_has_active_wcpay_subscriptions;
return count( $active_wcpay_subscriptions ) > 0;
}
}
Loading

0 comments on commit d0db2a3

Please sign in to comment.