Last active
July 8, 2021 16:27
-
-
Save pbrocks/691bd2f5613b0b3937190333559d809e to your computer and use it in GitHub Desktop.
An example of how to use `pmpro_checkout_level` to redirect and interject communication with users during checkout. Sample taken from an attempt to prevent downgrading.
This file contains hidden or 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 // Do not include in Customizations plugin | |
/** | |
* pmpro_checkout_level Use `pmpro_checkout_level` to redirect and interject communication with users during checkout. | |
* | |
* @param array $levels The array created in admin | |
* | |
* @return array Output of the array | |
*/ | |
function pmpro_redirect_during_checkout( $level ) { | |
// Check to see if the current user is downgrading (Gold to Silver or Gold to Free) | |
if ( pmpro_hasMembershipLevel( 3 ) && ( 1 === intval( $level->id ) || 2 === intval( $level->id ) ) ) { | |
// Re-direct them to page explaining why they cannot downgrade | |
wp_redirect( home_url( '/gold-downgrade-message' ) ); | |
exit; | |
} // Check to see if the current user is downgrading (Silver to Free) | |
else if ( pmpro_hasMembershipLevel( 2 ) && 1 === intval( $level->id ) ) { | |
// Re-direct them to page explaining why they cannot downgrade | |
wp_redirect( home_url( '/silver-downgrade-message' ) ); | |
exit; | |
} | |
return $level; | |
} | |
add_filter( 'pmpro_checkout_level', 'pmpro_redirect_during_checkout' ); |
Hey David, thanks for catching that, and you may recall that I have an aversion to not using strict comparisons, so will you accept the above updated with intval( $level->id )
?
Absolutely! I just wanted to leave a comment here with a possible fix in case others stumble across this. Thanks Paul!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
===
in this recipe should actually be==
as$level->id
is a string.