Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save unwiredtech/4aefe420c71608cea13c to your computer and use it in GitHub Desktop.
Save unwiredtech/4aefe420c71608cea13c to your computer and use it in GitHub Desktop.
Add a one-time-use 14 day trial to a monthly recurring membership level with Paid Memberships Pro. Place this file in wp-content/plugins/pmpro-customizations/pmpro-customizations.php on your site, then active the plugin through the WP dashboard.
<?php
/*
Plugin Name: PMPro Customizations
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Customizations for Paid Memberships Pro (14 day trial on level 1)
Version: .1
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
14 day free trial for monthly plan
Assumes level 1 is a level with an initial payment and recurring payments set to the same amount.
*/
//remove the initial payment if we're doing a trial
function my_pmpro_checkout_level($level)
{
global $current_user;
if(!empty($current_user->ID))
$trial_used = $current_user->pmpro_trial_level_used;
else
$trial_used = false;
//check if the trial has already been used
if(!empty($trial_used))
return $level;
if($level->id == 1)
{
//set initial payment to $0
$level->initial_payment = 0;
}
return $level;
}
add_action("pmpro_checkout_level", "my_pmpro_checkout_level");
//push the subscription start date back if we're doing a trial
function my_pmpro_profile_start_date($date, $order)
{
global $current_user;
//logged in at all?
if(!is_user_logged_in())
return $date;
//check if the trial has already been used
$trial_used = $current_user->pmpro_trial_level_used;
if(!empty($trial_used))
return $date;
//add trial
if($order->membership_id == 1)
$date = date("Y-m-d", strtotime("+ 14 Days")) . "T0:0:0";
return $date;
}
add_filter("pmpro_profile_start_date", "my_pmpro_profile_start_date", 10, 2);
//update the level cost to make sense with trial
function my_pmpro_level_cost_text($cost, $level)
{
global $current_user;
//logged in at all?
if(!is_user_logged_in())
return $cost;
//check if the trial has already been used
$trial_used = $current_user->pmpro_trial_level_used;
if(!empty($trial_used))
return $cost;
//add trial text
if($level->id == 1)
{
global $pmpro_currency_symbol;
$cost = "The price for membership is <strong>" . $pmpro_currency_symbol . $level->billing_amount . " per Month after your 14 day trial</strong>.";
}
return $cost;
}
add_filter("pmpro_level_cost_text", "my_pmpro_level_cost_text", 10, 2);
//record when users gain the trial
function my_pmpro_after_change_membership_level($level_id, $user_id)
{
if($level_id == 1)
{
//add user meta to record the fact that this user has had this level before
update_user_meta($user_id, "pmpro_trial_level_used", "1");
}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment