yanknudtskov
11/9/2016 - 10:41 AM

LearnDash and WooCommerce Subscriptions for selling installments

LearnDash and WooCommerce Subscriptions for selling installments

<?php

function render_course_selector() {
		global $post;
		$courses = $this->list_courses();
		echo '<div class="options_group show_if_course show_if_simple show_if_variable-subscription show_if_subscription show_if_variable">';

		$values = get_post_meta( $post->ID, '_related_course', true );
		if ( ! $values ) {
			$values = array();
		}

		woocommerce_wp_select( array(
			'id'          => '_related_course[]',
			'label'       => __( 'Related Courses', 'learndash' ),
			'options'     => $courses,
			'desc_tip'    => true,
			'description' => __( 'You can select multiple courses to sell together holding the SHIFT key when clicking.', 'learndash' )
		) );

		echo '<script>ldRelatedCourses = ' . json_encode( $values ) . '</script>';

		echo '</div>';
	}
<?php

if( class_exists('WC_Subscriptions_Manager') ) {

    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); // Keep in mind
    add_action('activated_subscription', 'give_course_access', 10, 2);

    // 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);
    		}

    	}
    }

    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));
    			}
    		}

    	}
    }

    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));
								}

							}
						}
					}
				}
			}
		}
	}
}