Skip to content

Instantly share code, notes, and snippets.

@dparker1005
Last active April 26, 2024 13:55
Show Gist options
  • Save dparker1005/3919d9a6604763a8ae017af41dab7088 to your computer and use it in GitHub Desktop.
Save dparker1005/3919d9a6604763a8ae017af41dab7088 to your computer and use it in GitHub Desktop.
Require user to have a prerequisite level before purchasing a restricted level. Requires PMPro MMPU to be active.
<?php
// Copy from below here...
/*
* Require user to have a prerequisite level before purchasing a restricted level.
* Requires PMPro MMPU to be active.
*
* 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_levels( $okay ) {
$prerequisite_levels = array( 1, 2 ); // One of these levels is required to buy a restricted level
$restricted_levels = array( 3, 4 ); // Levels that require a prerequisite level to be purchased
global $pmpro_checkout_level_ids;
//only check if things are okay so far and this is a MMPU checkout.
if ( $okay && ! empty( $pmpro_checkout_level_ids ) && 0 !== count( array_intersect( $restricted_levels, $pmpro_checkout_level_ids ) ) ) {
global $pmpro_checkout_level_ids;
if ( ! pmpro_hasMembershipLevel( $prerequisite_levels ) && 0 === count( array_intersect( $prerequisite_levels, $pmpro_checkout_level_ids ) ) ) {
pmpro_setMessage("You must purchase a prerequisite level to purchase a restricted level.", "pmpro_error");
$okay = false;
}
}
return $okay;
}
add_action("pmpro_registration_checks", "my_pmpro_enforce_prerequisite_levels");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment