-
-
Save indigetal/e172e17230ceb1f9516b7c147b6898d4 to your computer and use it in GitHub Desktop.
<?php | |
function my_pmpro_adjustable_level_cost($level) | |
{ | |
// Specify the monthly and annual levels | |
$monthly_levels = array(1, 2); | |
$annual_levels = array(3, 4); | |
// Set the field name here | |
$field_name = 'field_name'; | |
// Set the available field options and their monthly and annual fees | |
$options = array( | |
'option1' => array('monthly_fee' => 10, 'annual_fee' => 100), | |
'option2' => array('monthly_fee' => 20, 'annual_fee' => 200), | |
'option3' => array('monthly_fee' => 30, 'annual_fee' => 300) | |
); | |
// Stop editing. Enjoy! | |
$extra_fee = 0; // Default additional fee | |
if (!empty($_REQUEST[$field_name]) && isset($options[$_REQUEST[$field_name]])) { | |
$option_values = $options[$_REQUEST[$field_name]]; | |
if (in_array($level->id, $monthly_levels)) { | |
$extra_fee = $option_values['monthly_fee']; | |
} elseif (in_array($level->id, $annual_levels)) { | |
$extra_fee = $option_values['annual_fee']; | |
} | |
// Check if there is an extra fee | |
if ($extra_fee > 0) { | |
// Check if the level has a recurring subscription | |
if (pmpro_isLevelRecurring($level)) { | |
// Apply the additional fee for recurring plans | |
$level->initial_payment = $level->initial_payment + $extra_fee; | |
$level->billing_amount = $level->billing_amount + $extra_fee; | |
} else { | |
// Apply the additional fee for one-time payment plans | |
$level->initial_payment = $level->initial_payment + $extra_fee; | |
} | |
} | |
} | |
return $level; | |
} | |
add_filter("pmpro_checkout_level", "my_pmpro_adjustable_level_cost"); | |
?> |
There are a couple of housekeeping items to ensure you are displaying accurate information to your users. In Memberships > Settings > Email Templates, select the email template “Checkout - Paid,” and “Checkout - Paid (admin)” and remove the following line: <p>Membership Fee: !!membership_cost!!</p>
That line not only displays the initial cost of the membership plan and not the updated membership plan with the options, but the way that it is phrased: “Membership Fee: The price for membership is $x now, and $x every Month” may confuse users who did not purchase a renewable plan (I have contacted PMPro about this).
Finally, if you have the Auto-Renewal Checkbox addon, the checkbox label includes a code-generated calculation of the plan cost. This is also no longer accurate once our custom code is implemented. To update the calculation based on the selected options would require Ajax to handle the checkbox toggle event and a server-side request to calculate and return the calculation, as well as significant modifications to our custom code. That is beyond the scope of this tutorial. Instead, let’s just replace the label of the checkbox with the static text, “Renew your plan” using jquery. Place this code in your child theme’s functions.php file:
function custom_auto_renewal_checkbox_label() {
if ( is_page( 'membership-checkout' ) ) {
?>
<style>
/* Hide the original auto-renewal checkbox label initially */
label[for="autorenew"].pmpro_clickable {
visibility: hidden;
}
</style>
<script>
jQuery(document).ready(function($) {
// Show the updated auto-renewal checkbox label
$('label[for="autorenew"].pmpro_clickable').css('visibility', 'visible');
$('label[for="autorenew"].pmpro_clickable').html('Automatically renew your plan.');
});
</script>
<?php
}
}
add_action('wp_footer', 'custom_auto_renewal_checkbox_label');
To implement this functionality, create both monthly and annual plans and input their ID's in the $monthly_levels and $annual_levels arrays respectively. Next, navigate to the Memberships > Settings > User Fields page, create a new field group and call it something like, “Add X to Your Plan.” Select “Yes” for “Show fields at Checkout,” and choose your Levels in the “Restrict Fields for Membership Levels” dropdown. Add a field of type “Select/Dropdown,” name it something in the Label field, make note of the generated Name field and input that as the field_name in the code. In the Options text field, input “none,” “option1,” “option2,” and so on. The option values of the field will be input as the keys in the $options array. Be sure to click “Save All Changes” at the top of the User Fields settings page in Paid Memberships Pro. Adjust the number value of the monthly and annual fees for each option key in the $options array.
Need more functionality? Check these out:
Version 1.1 of the code only allows for a single checkbox and a one-time payment.
Version 1.2 of the code that only allows for a single checkbox and works for one-time payments and recurring plans.
Version 1.3 of the code that only allows for a single checkbox for one time payments and recurring plans and also adjusts the cost of the option based on if it's a monthly or annual plan.
Version 1.4 of the code that adds more options and allows for recurring plans.
Version 1.5 of the code to add more options, allow for recurring plans, and adjusts the cost based on if it's a monthly or annual plan.