Last active
May 13, 2020 03:17
-
-
Save yanknudtskov/9a302dc8985060e957dfe31a053fc6bc to your computer and use it in GitHub Desktop.
WooCommerce Subscriptions and LearnDash, based on the work of https://thomaslecoz.com/learndash-with-woocommerce-subscriptions/#code-update
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
<?php | |
add_action('cancelled_subscription', 'remove_course_access', 10, 2); | |
add_action('subscription_put_on-hold', 'remove_course_access', 10, 2); | |
add_action('subscription_expired', 'remove_course_access', 10, 2); | |
add_action('activated_subscription', 'give_course_access', 10, 2); | |
function send_receipt($order_id){ | |
//if($new_status == 'processing' && $status != 'completed' || $new_status == 'completed' && $status == 'processing'){ | |
if($status != 'processing' && $status != 'completed') { | |
$order = new WC_Order($order_id); | |
$products = $order->get_items(); | |
foreach($products as $product){ | |
$courses_id = get_post_meta($product['product_id'], '_related_course', true); | |
if($courses_id && is_array($courses_id)){ | |
foreach($courses_id as $cid) { | |
$already_in = false; | |
if(empty($order->customer_user) || empty($cid)) { | |
error_log("[EMPTY] User id: " . $order->customer_user . " Course Id:" . $cid); | |
return; | |
} | |
$meta = get_post_meta( $cid, '_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); | |
$access_list = explode(",", $access_list); | |
foreach($access_list as $c) { | |
if(trim($c) == $order->customer_user) | |
$already_in = true; | |
} | |
} | |
if (empty( $access_list ) || !$already_in) { | |
error_log("Empty list or user doesn't have access yet"); | |
ld_update_course_access($order->customer_user, $cid, $remove = false); | |
// If it's a subscription... | |
if (WC_Subscriptions_Order::order_contains_subscription($order) || WC_Subscriptions_Renewal_Order::is_renewal( $order )) { | |
error_log("Subscription (may be renewal) detected"); | |
if ($sub_key = WC_Subscriptions_Manager::get_subscription_key($order_id, $product['product_id'])) { | |
error_log("Subscription key: " . $sub_key); | |
$subscription_r = WC_Subscriptions_Manager::get_subscription( $sub_key ); | |
$start_date = $subscription_r['start_date']; | |
error_log("Start Date:" . $start_date); | |
update_user_meta($order->customer_user, "course_".$cid."_access_from", strtotime($start_date)); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
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); | |
} | |
} | |
} | |
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