Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active March 21, 2018 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strangerstudios/9787322 to your computer and use it in GitHub Desktop.
Save strangerstudios/9787322 to your computer and use it in GitHub Desktop.
Allow users to choose to auto renew or not for one annual level with Paid Memberships Pro
/*
Allow users to choose to auto renew
*/
//draw the box
function my_auto_renew_box()
{
//only for level 2
$level = intval($_REQUEST['level']);
if($level != 2)
return;
if(isset($_REQUEST['autorenew']))
$autorenew = intval($_REQUEST['autorenew']);
else
$autorenew = 1;
?>
<table id="pmpro_payment_method" class="pmpro_checkout top1em" width="100%" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Would you like to set up automatic renewals?</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<input type="radio" name="autorenew" value="1" <?php checked($autorenew, 1);?>>
<a href="javascript:void(0);" class="pmpro_radio">Yes, bill me each year.</a> &nbsp;
<input type="radio" name="autorenew" value="0" <?php checked($autorenew, 0);?>>
<a href="javascript:void(0);" class="pmpro_radio">No, I will manually renew.</a> &nbsp;
</div>
</td>
</tr>
</tbody>
</table>
<script>
jQuery(document).ready(function() {
jQuery('.pmpro_radio').click(function() {
jQuery(this).prev().click();
});
});
</script>
<?php
}
add_action('pmpro_checkout_boxes', 'my_auto_renew_box');
//update level based on selection
function my_auto_renew_checkout_level($level)
{
//only for level 2
if($level->id != 2)
return $level;
if(isset($_REQUEST['autorenew']))
$autorenew = intval($_REQUEST['autorenew']);
else
$autorenew = 1;
if($autorenew)
{
$level->billing_amount = $level->initial_payment;
$level->cycle_number = 1;
$level->cycle_period = "Year";
$level->expiration_number = 0;
}
else
{
$level->billing_amount = 0;
$level->cycle_number = 0;
$level->expiration_number = 1;
$level->cycle_period = "Year";
}
return $level;
}
add_filter("pmpro_checkout_level", "my_auto_renew_checkout_level", 7);
//filter level cost to remove per year section
function my_pmpro_level_cost_text($text, $level)
{
global $pmpro_currency_symbol;
if(!empty($level->initial_payment))
$text = "The price for membership is <strong>" . $pmpro_currency_symbol . $level->initial_payment . " per Year</strong>.";
return $text;
}
add_filter("pmpro_level_cost_text", "my_pmpro_level_cost_text", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment