Home / Archive / MemberPress: Dynamic Trial Periods
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Dynamic Trial Periods

This code snippet will set the renewal date of all subscriptions to the specific yearly recurring membership to a specific day, no mater when users subscribe. Furthermore, the code snippet will set the first period as a free trial.

Code Preview
php
<?php
//Sets up a trial period on MemberPress such that ALL users renew on March 31st.
function mepr_days_until($date){
    return (isset($date)) ? floor((strtotime($date) - time())/60/60/24) : false;
}
function mepr_update_mepr_trial_period() {
  $prd = new MeprProduct(123); //CHANGE 123 to the ID of your Membership level
  $last_check = get_option('mepr_trial_period_last_checked-'.$prd->ID, 0);
  if(time() - $last_check <= 3600) { return; } //Only do this once per hour
  $curr_month_of_year = gmdate('n');
  //change 4 to be the month after your month 
  if($curr_month_of_year >= 4) {
    //Get next year's March 31st
    $days = mepr_days_until(gmdate((gmdate('Y')+1).'-03-31')); //Change renewal date in format -03-14 Month-Day
  }
  else {
    //Get this year's March 31st
    $days = mepr_days_until(gmdate(gmdate('Y').'-03-31')); //Change renewal date in format -03-14 Month-Day
  }
  $prd->trial = true;
  $prd->trial_days = $days;
  $prd->store();
  update_option('mepr_trial_period_last_checked-'.$prd->ID, time());
}
add_action('init', 'mepr_update_mepr_trial_period');

Comments

Add a Comment