Skip to content

Instantly share code, notes, and snippets.

@indigetal
Last active August 4, 2023 16:21
Show Gist options
  • Save indigetal/189984225abe0fc7ab4d5afd935f2190 to your computer and use it in GitHub Desktop.
Save indigetal/189984225abe0fc7ab4d5afd935f2190 to your computer and use it in GitHub Desktop.
For Paid Memberships Pro - Adjusts the cost of a level at checkout based on the selection of a custom user field checkbox for upselling an additional option to a plan. Accounts for both recurring plans and adjust the fee based on if it's a monthly or an annual plan (fixes and expands on the code snippet at https://www.paidmembershipspro.com/mult…
<?php
function my_pmpro_adjustable_level_cost($level) {
$monthly_levels = array(1, 2); // specify monthly level IDs here
$annual_levels = array(3, 4); // specify annual level IDs here
$extra_fee = 0;
if (in_array($level->id, $monthly_levels)) {
$extra_fee = 10; //specify the cost of the selected option for monthly plans
}
elseif (in_array($level->id, $annual_levels)) {
$extra_fee = 100; //specify the cost of the selected option for annual plans
}
if ($extra_fee > 0) {
// Check if the level has a recurring subscription
if ( pmpro_isLevelRecurring( $level ) ) {
// Recurring payments
$level->initial_payment = $level->initial_payment + $extra_fee;
$level->billing_amount = $level->billing_amount + $extra_fee;
} else {
// One-time payment only
$level->initial_payment = $level->initial_payment + $extra_fee;
}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_adjustable_level_cost");
?>
@indigetal
Copy link
Author

indigetal commented Aug 4, 2023

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');

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