Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created November 17, 2020 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/9c97e285aee6c1ee070c7449d8bc1d54 to your computer and use it in GitHub Desktop.
Save ipokkel/9c97e285aee6c1ee070c7449d8bc1d54 to your computer and use it in GitHub Desktop.
Cancel On Next Payment Date Add On - Do not extend membership to next payment date when member in a trial period
<?php
/**
* This recipe will cancel membership immediately when
* using the Cancel On Next Payment Date Add On and
* not extend the membership to the next payment date
* if the member is still in a trial period.
*
* This works for default trial levels as well as
* trial periods set with the Subscription Delay Add On.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmproconpd_cancel_trial_immediately( $level_id, $user_id, $old_levels, $cancel_level ) {
// Only run when Cancel On Next Payment Date is active.
if ( ! function_exists( 'pmproconpd_pmpro_before_change_membership_level' ) ) {
return;
}
global $pmpro_pages, $pmpro_next_payment_timestamp;
// Are we on the cancel page and cancelling a level?
if ( $level_id == 0 && ( is_page( $pmpro_pages['cancel'] ) || ( is_admin() && ( empty( $_REQUEST['from'] ) || $_REQUEST['from'] != 'profile' ) ) ) ) {
if ( empty( $pmpro_next_payment_timestamp ) || ! $pmpro_next_payment_timestamp ) {
return;
}
global $pmpro_pages, $current_user;
if ( ! is_admin() && is_user_logged_in() && pmpro_hasMembershipLevel() ) {
$level = pmpro_getMembershipLevelForUser( $user_id );
$subscription_delay = get_option( 'pmpro_subscription_delay_' . $level->id, '' );
$today = strtotime( 'today' );
$user_is_trialing = false;
if ( $subscription_delay ) {
$level_trial_enddate = get_user_meta( $user_id, 'pmprosd_trialing_until', true );
if ( ! empty( $level_trial_enddate ) ) {
$level_trial_enddate = intval( $level_trial_enddate );
}
$new_date_timestamp = strtotime( $level_trial_enddate, $today );
if ( $today < $level_trial_enddate ) {
$user_is_trialing = true;
}
}
if ( pmpro_isLevelTrial( $level ) ) {
$current_user->membership_level = $level;
$user_startdate = intval( $current_user->membership_level->startdate );
$level_trial_enddate = strtotime( '+' . $level->trial_limit . ' ' . $level->cycle_period, $user_startdate );
if ( $today < $level_trial_enddate ) {
$user_is_trialing = true;
}
}
}
if ( $user_is_trialing ) {
$pmpro_next_payment_timestamp = false;
}
}
}
add_action( 'pmpro_before_change_membership_level', 'my_pmproconpd_cancel_trial_immediately', 20, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment