Home / Archive / MemberPress: Dynamic Trail Periods
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Dynamic Trail Periods

Sets up a dynamic trial period so that all users renew on the same date. In this example, it is March 31st.

Code Preview
php
<?php
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( 4480 ); //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' );
  if( $curr_month_of_year >= 4 ) {
    //Get next year's march 31st
    $days = mepr_days_until( gmdate( ( gmdate( 'Y' )+1 ).'-03-31' ) );
  }
  else {
    //Get this year's march 31st
    $days = mepr_days_until( gmdate( gmdate( 'Y' ).'-03-31' ) );
  }
  $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