Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Last active July 8, 2021 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbrocks/691bd2f5613b0b3937190333559d809e to your computer and use it in GitHub Desktop.
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.
<?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' );
@dparker1005
Copy link

The === in this recipe should actually be == as $level->id is a string.

@pbrocks
Copy link
Author

pbrocks commented Jul 8, 2021

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 )?

@dparker1005
Copy link

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