Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created March 15, 2022 09:45
Show Gist options
  • Save andrewlimaza/11ecec0ec91f67cb5f2dee389e8cb295 to your computer and use it in GitHub Desktop.
Save andrewlimaza/11ecec0ec91f67cb5f2dee389e8cb295 to your computer and use it in GitHub Desktop.
Cancel on Next Payment Date + Subscriptions Delay Add On. Cancel Members Immediately if on trial.
<?php
/**
* Tries to cancel members immediately if they are cancelling within the Subscription Delay Limit of their level.
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
/**
* Is a user currently in a subscription delay based on their membership level.
*
* @param int $user_id - The user's ID you want to query against.
* @param int $level_id - The level ID the user currently holds.
* @return boolean $is_trial - Whether or not the user is in a trial period based on the level's option settings.
*/
function pmpro_is_member_maybe_trial( $user_id, $level_id = NULL ) {
$member_level = pmpro_getMembershipLevelForUser( $user_id );
$sub_days = get_option( 'pmpro_subscription_delay_' . $level_id, '' );
if ( ! $member_level ) {
return false;
}
if ( empty( $sub_days ) ) {
return false;
}
// Are members still within trial period.
if ( $member_level->startdate >= strtotime( '-' . $sub_days . ' days' ) ) {
return true;
}
// Just in case we make it here.
return false;
}
/**
* Cancel the member immediately instead of setting an expiration date if they are currently on trial.
* This only works for Subscription Delay Add On.
*/
function my_pmpro_cancel_sub_members_immediately( $next_payment, $gateway, $level, $user_id ) {
if ( pmpro_is_member_maybe_trial( $user_id, $level ) ) {
$next_payment = false;
}
return $next_payment;
}
add_filter( 'pmproconpd_next_payment_timestamp_to_cancel_on', 'my_pmpro_cancel_sub_members_immediately', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment