Skip to content

Commit

Permalink
Merge pull request #2853 from dparker1005/better-sub-migration-defaults
Browse files Browse the repository at this point in the history
Better subs table migration defaults
  • Loading branch information
ideadude authored Feb 20, 2024
2 parents 476d6d2 + bfed1d5 commit f8824af
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions classes/class-pmpro-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -1247,22 +1247,24 @@ private function maybe_fix_default_migration_data() {
* created for.
* 4. If we do not find a membership level that matches the subscription level and is recurring,
* then let's use the default membership level settings if it is recurring.
*/
$all_user_levels = pmpro_getMembershipLevelsForUser( $this->user_id, true ); // True to include old memberships.
// Looping through $all_user_levels backwards to get the most recent first.
for ( end( $all_user_levels ); key( $all_user_levels ) !== null; prev( $all_user_levels ) ) {
$level_check = current( $all_user_levels );

// Let's check if level the same level as this subscription and if it's a recurring level.
if ( $level_check->id == $this->membership_level_id && ! empty( $level_check->billing_amount ) && ! empty( $level_check->cycle_number ) ) {
$subscription_level = $level_check;
break;
*/
if ( 'active' === $this->status ) { // Only guess for active subscriptions. For cancelled subscriptions, we would rather show $0/month than a potentially wrong amount.
$all_user_levels = pmpro_getMembershipLevelsForUser( $this->user_id, true ); // True to include old memberships.
// Looping through $all_user_levels backwards to get the most recent first.
for ( end( $all_user_levels ); key( $all_user_levels ) !== null; prev( $all_user_levels ) ) {
$level_check = current( $all_user_levels );

// Let's check if level the same level as this subscription and if it's a recurring level.
if ( $level_check->id == $this->membership_level_id && ! empty( $level_check->billing_amount ) && ! empty( $level_check->cycle_number ) ) {
$subscription_level = $level_check;
break;
}
}
}

// If the user hasn't had a recurring membership for this level,
// pull from the level settings instead.
if ( empty( $subscription_level ) ) {
// If the user hasn't had a recurring membership for this level, pull from the level settings instead.
// Only do this if the subscription is active since we can guess wrong and may not be able to sync with the gateway since this is an old subscription.
if ( empty( $subscription_level ) && 'active' === $this->status) {
$level = pmpro_getLevel( $this->membership_level_id );
if ( ! empty( $level ) && ! empty( $level->billing_amount ) && ! empty( $level->cycle_number ) ) {
$subscription_level = $level;
Expand Down Expand Up @@ -1303,11 +1305,20 @@ private function maybe_fix_default_migration_data() {
}


// Let's also take a guess at the next payment date.
$newest_orders = $this->get_orders( array( 'limit' => 1 ) );
// Let's also take a guess at the next payment date or end date.
$newest_orders = $this->get_orders(
[
'limit' => 1,
'orderby' => '`timestamp` DESC, `id` DESC',
]
);
if ( ! empty( $newest_orders ) ) {
$newest_order = current( $newest_orders );
$this->next_payment_date = date_i18n( 'Y-m-d H:i:s', strtotime( '+ ' . $this->cycle_number . ' ' . $this->cycle_period, $newest_order->getTimestamp( true ) ) );
if ( 'active' === $this->status ) {
$this->next_payment_date = date_i18n( 'Y-m-d H:i:s', strtotime( '+ ' . $this->cycle_number . ' ' . $this->cycle_period, $newest_order->getTimestamp( true ) ) );
} else {
$this->enddate = date_i18n( 'Y-m-d H:i:s', $newest_order->getTimestamp( true ) );
}
}

// Now that we have the basic data filled in, the `update()` method will take care of the rest.
Expand Down

0 comments on commit f8824af

Please sign in to comment.