Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/976d6a5af5bc0bc30700a7e99bbdf1dd to your computer and use it in GitHub Desktop.
Save dwanjuki/976d6a5af5bc0bc30700a7e99bbdf1dd to your computer and use it in GitHub Desktop.
Cancel Members Immediately if they cancel during the trial period (Subscription Delays)
<?php
/**
* Cancel members immediately if they are cancelling within the Subscription Delay Limit of their level
*
* Works on PMPro v3.0+
*
* 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 pmpro_is_member_maybe_trial( $user_id, $level_id ) {
$member_level = pmpro_getSpecificMembershipLevelForUser( $user_id, $level_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( $conpd, $level_id, $user_id ) {
if ( pmpro_is_member_maybe_trial( $user_id, $level_id ) ) {
$conpd = false;
}
return $conpd;
}
add_filter( 'pmpro_cancel_on_next_payment_date', 'my_pmpro_cancel_sub_members_immediately', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment