Skip to content

Instantly share code, notes, and snippets.

@ThomasLeCoz
Created December 16, 2015 15:52
Show Gist options
  • Save ThomasLeCoz/dfa4c6c10a8995804816 to your computer and use it in GitHub Desktop.
Save ThomasLeCoz/dfa4c6c10a8995804816 to your computer and use it in GitHub Desktop.
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);
$start_date = $subscription['start_date'];
error_log('Start Date :' . $start_date . ' Epoch: ' . strtotime($start_date));
// Update access to the courses
if ($courses_id && is_array($courses_id)) {
foreach ($courses_id as $course_id) {
error_log("Checking for course: " . $course_id . " and User: " . $user_id);
// Check if user already has access
$already_in = false;
if(empty($user_id) || empty($course_id)) {
error_log("User id: " . $user_id . " Course Id:" . $course_id);
return;
}
$meta = get_post_meta( $course_id, '_sfwd-courses', true );
$access_list = $meta['sfwd-courses_course_access_list'];
error_log('Access List: ' .$access_list);
if (!empty( $access_list )) {
error_log("Access List not empty.");
$access_list = explode(",", $access_list);
foreach($access_list as $c) {
if(trim($c) == $user_id) {
error_log("User already in the list, setting flag.");
$already_in = true;
}
}
}
if (empty( $access_list ) || !$already_in) {
error_log("Empty list or user don't have access yet.");
ld_update_course_access($user_id, $course_id, $remove = false);
// Replace start date to keep the drip feeding working
error_log("Updating subscription date to original order");
update_user_meta($user_id, "course_".$course_id."_access_from", strtotime($start_date));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment