Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active March 21, 2018 14:54
Show Gist options
  • Save strangerstudios/ab11879e0c4839c4fc7f to your computer and use it in GitHub Desktop.
Save strangerstudios/ab11879e0c4839c4fc7f to your computer and use it in GitHub Desktop.
Add a box to choose to auto renew at checkout with PMPro.
<?php
/*
*************** !!
!! IMPORTANT NOTE: This can more easily be done using the addon maintained here:
!! https://github.com/strangerstudios/pmpro-auto-renewal-checkbox
*************** !!
Allow users to choose to auto renew
1. Edit the global variable below to set the renewal amount (can be the same or less) for each level.
2. Paste this code into your active theme's functions.php or a custom plugin.
*/
global $pmpro_auto_renewal_amounts; //level_id => billing_amount
$pmpro_auto_renewal_amounts = array(
"6"=>"47",
"17"=>"147",
"18"=>"147",
"19"=>"147",
"20"=>"147"
);
//draw the box
function pmpro_auto_renew_box()
{
global $pmpro_auto_renewal_amounts, $pmpro_review, $discount_code;
//only for certain levels
$level = intval($_REQUEST['level']);
if(!isset($pmpro_auto_renewal_amounts[$level]))
return;
//not if this is an addon package
if(!empty($_REQUEST['ap']) || !empty($_SESSION['ap']))
return;
//not if using a discount code
if(!empty($discount_code))
return;
if(!empty($_REQUEST['autorenew']))
$autorenew = 1;
else
$autorenew = 0;
if(!$pmpro_review)
{
?>
<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="checkbox" id="autorenew" name="autorenew" value="1" <?php checked($autorenew, 1);?> />
<input type="hidden" id="autorenew_present" name="autorenew_present" value="1" />
<label class="pmpro_normal pmpro_clickable" for="autorenew">
Yes, renew annually at $<?php echo $pmpro_auto_renewal_amounts[$level];?>/yr.
</label>
</div>
</td>
</tr>
</tbody>
</table>
<?php
}
else
{
if(!empty($_SESSION['autorenew']))
?>
<input type="hidden" id="autorenew" name="autorenew" value="<?php $autorenew;?>" />
<input type="hidden" id="autorenew_present" name="autorenew_present" value="1" />
<?php
}
}
add_action('pmpro_checkout_boxes', 'pmpro_auto_renew_box', 15);
//save autorenew to session for PayPal Express
function pmpro_auto_renew_pmpro_paypalexpress_session_vars()
{
if(!empty($_REQUEST['autorenew']))
$autorenew = 1;
else
$autorenew = 0;
$_SESSION['autorenew'] = $autorenew;
$_SESSION['autorenew_present'] = 1;
}
add_action('pmpro_paypalexpress_session_vars', 'pmpro_auto_renew_pmpro_paypalexpress_session_vars');
//update level based on selection
function pmpro_auto_renew_checkout_level($level)
{
global $pmpro_auto_renewal_amounts, $discount_code;
//only for certain levels
if(!isset($pmpro_auto_renewal_amounts[$level->id]))
return $level;
//not if addon package
if(!empty($_REQUEST['ap']) || !empty($_SESSION['ap']))
return $level;
//not if using a discount code
if(!empty($discount_code))
return $level;
if(!empty($_REQUEST['autorenew']))
$autorenew = 1;
elseif(isset($_REQUEST['autorenew_present']))
$autorenew = 0;
elseif(isset($_SESSION['autorenew_present']))
$autorenew = intval($_SESSION['autorenew']);
else
$autorenew = 0;
if($autorenew)
{
$level->billing_amount = $pmpro_auto_renewal_amounts[$level->id];
$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", "pmpro_auto_renew_checkout_level", 7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment