Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/25929cc40c0677ff66bc723b091d2bf1 to your computer and use it in GitHub Desktop.
Save dwanjuki/25929cc40c0677ff66bc723b091d2bf1 to your computer and use it in GitHub Desktop.
Require user to have a prerequisite level before purchasing a restricted level
<?php // copy from below
/*
* Require user to have a prerequisite level before purchasing a restricted level.
*
* 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/
*
*/
function my_pmpro_enforce_prerequisite_level( $okay ) {
$prerequisite_levels = array( 1, 2 ); // at least one of these levels is required to buy the restricted level
$restricted_level = 3; // Level that requires a prerequisite level to be purchased
$pmpro_level = pmpro_getLevelAtCheckout(); // checkout level
if ( $okay && $pmpro_level->id == $restricted_level && ! pmpro_hasMembershipLevel( $prerequisite_levels ) ) {
pmpro_setMessage( 'You must be a member of level 1 or level 2 to purchase level 3.', 'pmpro_error' );
$okay = false;
}
return $okay;
}
add_action( 'pmpro_registration_checks', 'my_pmpro_enforce_prerequisite_level' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment