Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmasotti/dca12045b7bb6f1861e2f560374469d7 to your computer and use it in GitHub Desktop.
Save dmasotti/dca12045b7bb6f1861e2f560374469d7 to your computer and use it in GitHub Desktop.
Functions to give or remove access to LearnDash courses
// Remove Access to the course linked to the subscription key
function remove_course_access( $user_id, $subscription_key ) {
// Get the course ID related to the subscription
$subscription = WC_Subscriptions_Manager::get_subscription( $subscription_key );
$courses_id = get_post_meta($subscription['product_id'], '_related_course', true);
// Update access to the courses
if ($courses_id && is_array($courses_id)) {
foreach ($courses_id as $course_id) {
ld_update_course_access($user_id, $course_id, $remove = true);
}
}
}
// Give Access to the course linked to the subscription key
function give_course_access( $user_id, $subscription_key ) {
// Get the course ID related to the subscription
$subscription = WC_Subscriptions_Manager::get_subscription( $subscription_key );
$courses_id = get_post_meta($subscription['product_id'], '_related_course', true);
// Update access to the courses
if ($courses_id && is_array($courses_id)) {
foreach ($courses_id as $course_id) {
ld_update_course_access($user_id, $course_id, $remove = false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment