Created
August 7, 2020 06:39
-
-
Save ipokkel/49b936eaf50fe3a025176c8d0b013628 to your computer and use it in GitHub Desktop.
Custom PMPro levels page template that changes the level "Select" button text to "Downgrade" or "Upgrade" depending on the level ID being lower or higher than the current user's level.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe is a custom PMPro Levels Template that changes the | |
* level "Select" button text to "Downgrade" or "Upgrade" depending on | |
* the level ID being lower or higher than the current user's level. | |
* | |
* This recipe assumes that level were created in order of level cost, | |
* the cheapest level having the lowest level ID and the most expensive | |
* having the highest level ID. | |
* | |
* To incorporate this custom template: | |
* https://www.paidmembershipspro.com/documentation/templates/ | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
global $wpdb, $pmpro_msg, $pmpro_msgt, $current_user; | |
$pmpro_levels = pmpro_getAllLevels( false, true ); | |
$pmpro_level_order = pmpro_getOption( 'level_order' ); | |
if ( ! empty( $pmpro_level_order ) ) { | |
$order = explode( ',', $pmpro_level_order ); | |
//reorder array | |
$reordered_levels = array(); | |
foreach ( $order as $level_id ) { | |
foreach ( $pmpro_levels as $key => $level ) { | |
if ( $level_id == $level->id ) { | |
$reordered_levels[] = $pmpro_levels[ $key ]; | |
} | |
} | |
} | |
$pmpro_levels = $reordered_levels; | |
} | |
$pmpro_levels = apply_filters( 'pmpro_levels_array', $pmpro_levels ); | |
if ( $pmpro_msg ) { | |
?> | |
<div class="<?php echo pmpro_get_element_class( 'pmpro_message ' . $pmpro_msgt, $pmpro_msgt ); ?>"><?php echo $pmpro_msg; ?></div> | |
<?php | |
} | |
?> | |
<h3>Custom Levels Page</h3> | |
<table id="pmpro_levels_table" class="<?php echo pmpro_get_element_class( 'pmpro_table pmpro_checkout', 'pmpro_levels_table' ); ?>"> | |
<thead> | |
<tr> | |
<th><?php _e( 'Level', 'paid-memberships-pro' ); ?></th> | |
<th><?php _e( 'Price', 'paid-memberships-pro' ); ?></th> | |
<th> </th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
$count = 0; | |
foreach ( $pmpro_levels as $level ) { | |
if ( isset( $current_user->membership_level->ID ) ) { | |
$current_level = ( $current_user->membership_level->ID == $level->id ); | |
} else { | |
$current_level = false; | |
} | |
?> | |
<tr class=" | |
<?php | |
if ( $count++ % 2 == 0 ) { | |
?> | |
odd<?php } ?> | |
<?php | |
if ( $current_level == $level ) { | |
?> | |
active<?php } ?>"> | |
<td><?php echo $current_level ? "<strong>{$level->name}</strong>" : $level->name; ?></td> | |
<td> | |
<?php | |
if ( pmpro_isLevelFree( $level ) ) { | |
$cost_text = '<strong>' . __( 'Free', 'paid-memberships-pro' ) . '</strong>'; | |
} else { | |
$cost_text = pmpro_getLevelCost( $level, true, true ); | |
} | |
$expiration_text = pmpro_getLevelExpiration( $level ); | |
if ( ! empty( $cost_text ) && ! empty( $expiration_text ) ) { | |
echo $cost_text . '<br />' . $expiration_text; | |
} elseif ( ! empty( $cost_text ) ) { | |
echo $cost_text; | |
} elseif ( ! empty( $expiration_text ) ) { | |
echo $expiration_text; | |
} | |
?> | |
</td> | |
<td> | |
<?php if ( empty( $current_user->membership_level->ID ) ) { ?> | |
<a class="<?php echo pmpro_get_element_class( 'pmpro_btn pmpro_btn-select', 'pmpro_btn-select' ); ?>" href="<?php echo pmpro_url( 'checkout', '?level=' . $level->id, 'https' ); ?>"><?php _e( 'Select', 'paid-memberships-pro' ); ?></a> | |
<?php | |
} elseif ( ! $current_level ) { | |
/* | |
Change level button text according to level ID. | |
*/ | |
$my_level_button_text = 'Select'; // Default | |
if ( $current_user->membership_level->ID < $level->id ) { | |
$my_level_button_text = 'Upgrade'; // level ID is higher (larger) than current user's level | |
} else { | |
$my_level_button_text = 'Downgrade'; // level ID is lower (smaller) than current user's level | |
} | |
?> | |
<a class="<?php echo pmpro_get_element_class( 'pmpro_btn pmpro_btn-select', 'pmpro_btn-select' ); ?>" href="<?php echo pmpro_url( 'checkout', '?level=' . $level->id, 'https' ); ?>"><?php /* _e( 'Select', 'paid-memberships-pro' );*/ echo $my_level_button_text; ?></a> | |
<?php | |
} elseif ( $current_level ) { | |
//if it's a one-time-payment level, offer a link to renew | |
if ( pmpro_isLevelExpiringSoon( $current_user->membership_level ) && $current_user->membership_level->allow_signups ) { | |
?> | |
<a class="<?php echo pmpro_get_element_class( 'pmpro_btn pmpro_btn-select', 'pmpro_btn-select' ); ?>" href="<?php echo pmpro_url( 'checkout', '?level=' . $level->id, 'https' ); ?>"><?php _e( 'Renew', 'paid-memberships-pro' ); ?></a> | |
<?php | |
} else { | |
?> | |
<a class="<?php echo pmpro_get_element_class( 'pmpro_btn disabled', 'pmpro_btn' ); ?>" href="<?php echo pmpro_url( 'account' ); ?>"><?php _e( 'Your Level', 'paid-memberships-pro' ); ?></a> | |
<?php | |
} | |
} | |
?> | |
</td> | |
</tr> | |
<?php | |
} | |
?> | |
</tbody> | |
</table> | |
<p class="<?php echo pmpro_get_element_class( 'pmpro_actions_nav' ); ?>"> | |
<?php if ( ! empty( $current_user->membership_level->ID ) ) { ?> | |
<a href="<?php echo pmpro_url( 'account' ); ?>" id="pmpro_levels-return-account"><?php _e( '← Return to Your Account', 'paid-memberships-pro' ); ?></a> | |
<?php } else { ?> | |
<a href="<?php echo home_url(); ?>" id="pmpro_levels-return-home"><?php _e( '← Return to Home', 'paid-memberships-pro' ); ?></a> | |
<?php } ?> | |
</p> <!-- end pmpro_actions_nav --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment