Created
July 26, 2024 19:20
-
-
Save kimcoleman/75c9cab754f07c29a9152f3b47616303 to your computer and use it in GitHub Desktop.
Some logic to set up free access plans that require PMPro membership level to enroll in.
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
/** | |
* Require a specific membership level IDs to enroll in free access plans. | |
*/ | |
function my_pmpro_llms_access_plan_is_available_to_user( $access, $user_id, $access_plan ) { | |
// Bail if streamline is not enabled. | |
if ( ! get_option( 'pmpro_lifter_streamline' ) ) { | |
return $access; | |
} | |
// If the plan isn't free, let LifterLMS handle it. | |
if ( $access_plan->get( 'price' ) > 0 ) { | |
return $access; | |
} | |
// Allow members of level IDs 1, 2, or 3 enroll in free access plans. | |
$level_ids = array( 1, 2, 3 ); | |
if ( pmpro_hasMembershipLevel( $level_ids, $user_id ) ) { | |
return true; | |
} | |
return false; | |
} | |
add_filter( 'llms_plan_is_available_to_user', 'my_pmpro_llms_access_plan_is_available_to_user', 10, 3 ); | |
/** | |
* Redirect members-only plans to the Membership Levels page. | |
*/ | |
function my_pmpro_llms_plan_get_checkout_url( $url, $plan ) { | |
// If the plan isn't free, let LifterLMS handle it. | |
if ( $plan->get( 'price' ) > 0 ) { | |
return $url; | |
} | |
// If the plan isn't available to the user, change URL to levels page. | |
$available = $plan->is_available_to_user( get_current_user_id() ); | |
if ( ! $available ) { | |
$url = pmpro_url( 'levels' ); | |
} | |
return $url; | |
} | |
add_filter( 'llms_plan_get_checkout_url', 'my_pmpro_llms_plan_get_checkout_url', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment