Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active May 6, 2021 09:00
Show Gist options
  • Save strangerstudios/5861525 to your computer and use it in GitHub Desktop.
Save strangerstudios/5861525 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);
@davidsharpbell
Copy link

Thanks for the code. :) I love that you guys share your work so openly. Really great stuff!

@Tech1Resource
Copy link

We want to offer this for a site running v1.8.12.1

The code does not appear to be working for our site. Are there updated instructions for how this works?

Thanks! :)

@block4
Copy link

block4 commented Aug 3, 2017

The code seems to work only for someone who has already signed up and has a membership level. Doesn't work if a person hasn't yet signed up.

@djangossoul
Copy link

Hi there,
Does anybody try this customization with the current version (i.e. 4.4)? Or is this solution already merged with the "pmpro-subscription-delays" addon?
Cause I'm wondering about user in trial version who unsubscribe, and then re-subscribe to still be in the free trial ;)
Thx in advance,
G

@nitesh4690
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment