Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding setting filters during pmpro_checkout_level #29

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions includes/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,23 @@ function pmprorate_pmpro_checkout_level( $level ) {
$level->initial_payment = 0;

// If purchasing a subscription, make sure payment date stays the same.
add_filter( 'pmpro_profile_start_date', 'pmprorate_set_startdate_to_next_payment_date', 10, 2 );

// Set up the delayed downgrade.
$pmprorate_is_downgrade = true;
// Check if we are using PMPro v3.4+. That version starts supporting the `profile_start_date` property on the level object.
if ( defined( 'PMPRO_VERSION' ) && version_compare( PMPRO_VERSION, '3.4', '>=' ) ) {
// Get the subscription.
$current_subscriptions = PMPro_Subscription::get_subscriptions_for_user( get_current_user_id(), $clevel_id );

// Use the new `profile_start_date` property on the level object.
$level->profile_start_date = empty( $current_subscriptions ) ? null : $current_subscriptions[0]->get_next_payment_date( 'Y-m-d H:i:s' );

// Remember the fact that this is a downgrade.
$level->pmprorate_is_downgrade = true;
} else {
// Use the old filter.
add_filter( 'pmpro_profile_start_date', 'pmprorate_set_startdate_to_next_payment_date', 10, 2 );

// Set up the delayed downgrade.
$pmprorate_is_downgrade = true;
}

// Bail to avoid further proration logic.
return $level;
Expand Down Expand Up @@ -262,8 +275,15 @@ function pmprorate_pmpro_checkout_level( $level ) {
$remaining_cost_for_new_level = $level->billing_amount * $per_left;
$level->initial_payment = min( $level->initial_payment, round( $remaining_cost_for_new_level - $credit, 2 ) );

//make sure payment date stays the same
add_filter( 'pmpro_profile_start_date', 'pmprorate_set_startdate_to_next_payment_date', 10, 2 );
// If purchasing a subscription, make sure payment date stays the same.
// Check if we are using PMPro v3.4+. That version starts supporting the `profile_start_date` property on the level object.
if ( defined( 'PMPRO_VERSION' ) && version_compare( PMPRO_VERSION, '3.4', '>=' ) ) {
// Use the new `profile_start_date` property on the level object.
$level->profile_start_date = date_i18n( 'Y-m-d H:i:s', $next_payment_date );
} else {
// Use the old filter.
add_filter( 'pmpro_profile_start_date', 'pmprorate_set_startdate_to_next_payment_date', 10, 2 );
}
} else {
/*
Upgrade with different payment periods in a nutshell:
Expand Down
12 changes: 10 additions & 2 deletions includes/delayed-downgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ function pmprorate_added_order_mark_order_as_downgrade( $order ) {
return;
}

// Get the level for this order.
$level = $order->getMembershipLevelAtCheckout();

// Update order meta.
if ( ! empty( $pmprorate_is_downgrade ) ) {
if ( ! empty( $pmprorate_is_downgrade ) ) { // This will only ever be set before PMPro v3.4.
update_pmpro_membership_order_meta( $order->id, 'pmprorate_is_downgrade', $pmprorate_is_downgrade );
} elseif( ! empty( $level->pmprorate_is_downgrade ) ) { // This will only ever be set after PMPro v3.4.
update_pmpro_membership_order_meta( $order->id, 'pmprorate_is_downgrade', $level->pmprorate_is_downgrade );
}
}

Expand Down Expand Up @@ -79,10 +84,13 @@ function pmprorate_checkout_before_change_membership_level_remember_downgrade( $
$order->membership_id = $pmpro_level->id;
}

// Get the level for the order.
$level = $order->getMembershipLevelAtCheckout();

// If this order was not marked as a downgrade, bail.
// $pmprorate_is_downgrade checks if the checkout started processing on this page load.
// Checking order meta checks if the checkout started processing on a previous page load, such as with offsite payment gateways.
if ( empty( get_pmpro_membership_order_meta( $order->id, 'pmprorate_is_downgrade', true ) ) && empty( $pmprorate_is_downgrade) ) {
if ( empty( get_pmpro_membership_order_meta( $order->id, 'pmprorate_is_downgrade', true ) ) && empty( $pmprorate_is_downgrade ) && empty( $level->pmprorate_is_downgrade ) ) {
return;
}

Expand Down