@@ -456,6 +456,75 @@ public static function sync_stripe_subscriptions_to_wc( $args, $assoc_args ) {
456456 \WP_CLI ::success ( 'Finished processing. ' );
457457 }
458458
459+ /**
460+ * CLI command for finding previously migrated Stripe Subscriptions in Woo and ensuring they have a next payment date set.
461+ *
462+ * @param array $args Positional args.
463+ * @param array $assoc_args Associative args.
464+ */
465+ public static function set_next_payment_dates_for_migrated_subscriptions ( $ args , $ assoc_args ) {
466+ $ is_dry_run = ! empty ( $ assoc_args ['dry-run ' ] );
467+ $ batch_size = ! empty ( $ assoc_args ['batch-size ' ] ) ? intval ( $ assoc_args ['batch-size ' ] ) : 10 ;
468+
469+ \WP_CLI ::log (
470+ '
471+
472+ Running script to set next payment dates on migrated subscriptions...
473+
474+ '
475+ );
476+
477+
478+ $ migrated_subscriptions = self ::get_migrated_subscriptions ( $ batch_size );
479+ $ offset = 0 ;
480+ $ processed = 0 ;
481+ while ( $ migrated_subscriptions ) {
482+ $ subscription_id = array_shift ( $ migrated_subscriptions );
483+
484+ // Set next payment date.
485+ $ subscription = \wcs_get_subscription ( $ subscription_id );
486+ if ( $ subscription ) {
487+ \WP_CLI ::log (
488+ sprintf (
489+ 'Found subscription with ID %d and start date %s. ' ,
490+ $ subscription_id ,
491+ $ subscription ->get_date ( 'start ' )
492+ )
493+ );
494+
495+ // Get the next payment date.
496+ $ next_payment_date = $ subscription ->get_date ( 'next_payment ' );
497+
498+ // If there's no next payment, set it.
499+ if ( ! $ next_payment_date ) {
500+ $ next_payment_date = $ subscription ->calculate_date ( 'next_payment ' );
501+ \WP_CLI ::log ( sprintf ( 'No next payment date set. Setting to %s. ' , $ next_payment_date ) );
502+
503+ if ( ! $ is_dry_run ) {
504+ $ subscription ->update_dates (
505+ [
506+ 'next_payment ' => $ next_payment_date ,
507+ ]
508+ );
509+
510+ $ subscription ->save ();
511+ }
512+ $ processed ++;
513+ } else {
514+ \WP_CLI ::log ( sprintf ( 'Next payment date already set to %s. Skipping. ' , $ next_payment_date ) );
515+ }
516+ }
517+
518+ // Get the next batch.
519+ if ( empty ( $ migrated_subscriptions ) ) {
520+ $ offset += $ batch_size ;
521+ $ migrated_subscriptions = self ::get_migrated_subscriptions ( $ batch_size , $ offset );
522+ }
523+ }
524+
525+ \WP_CLI ::success ( sprintf ( 'Finished processing %d subscriptions. ' , $ processed ) );
526+ }
527+
459528 /**
460529 * Get a batch of customers for the Stripe-Connect-to-Stripe CLI tool.
461530 *
@@ -484,6 +553,29 @@ protected static function get_batch_of_customers_with_subscriptions( $limit, $la
484553 }
485554 }
486555
556+ /**
557+ * Get a batch of migrated subscriptions.
558+ *
559+ * @param int $batch_size Number of subscriptions to fetch.
560+ * @param int $offset Offset to start at.
561+ * @return array Array of WC Subscriptions.
562+ */
563+ protected static function get_migrated_subscriptions ( $ batch_size = 0 , $ offset = 0 ) {
564+ $ args = [
565+ 'fields ' => 'ids ' ,
566+ 'offset ' => $ offset ,
567+ 'post_status ' => 'wc-active ' ,
568+ 'post_type ' => 'shop_subscription ' ,
569+ 'posts_per_page ' => $ batch_size ,
570+ 'meta_query ' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
571+ 'key ' => 'cancelled-newspack-stripe-subscription-id ' ,
572+ 'compare ' => 'EXISTS ' ,
573+ ],
574+ ];
575+
576+ return \get_posts ( $ args );
577+ }
578+
487579 /**
488580 * Process one customer's Stripe subscriptions and migrate them.
489581 *
@@ -691,7 +783,28 @@ public static function wp_cli() {
691783 'newspack stripe sync-stripe-subscriptions-to-wc ' ,
692784 [ __CLASS__ , 'sync_stripe_subscriptions_to_wc ' ],
693785 [
694- 'shortdesc ' => __ ( 'Migrate subscribtions from Stripe to WC ' , 'newspack ' ),
786+ 'shortdesc ' => __ ( 'Migrate subscriptions from Stripe to WC ' , 'newspack ' ),
787+ 'synopsis ' => [
788+ [
789+ 'type ' => 'flag ' ,
790+ 'name ' => 'dry-run ' ,
791+ 'optional ' => true ,
792+ ],
793+ [
794+ 'type ' => 'flag ' ,
795+ 'name ' => 'batch-size ' ,
796+ 'default ' => 10 ,
797+ 'optional ' => true ,
798+ ],
799+ ],
800+ ]
801+ );
802+
803+ \WP_CLI ::add_command (
804+ 'newspack stripe set-next-payment-dates-for-migrated-subscriptions ' ,
805+ [ __CLASS__ , 'set_next_payment_dates_for_migrated_subscriptions ' ],
806+ [
807+ 'shortdesc ' => __ ( 'Ensure that all migrated subscriptions have a next payment date. ' , 'newspack ' ),
695808 'synopsis ' => [
696809 [
697810 'type ' => 'flag ' ,
0 commit comments