As a Freelance WordPress developer, many are the times I find myself repeating the same codes over and over again.

As we have seen in some earlier articles on how to get a course Id given product Id in Tutor LMS or vice versa, getting linked product Id from course Id at times you may not always have direct access to the course.

Understanding how to get all course details is vital as a WordPress Tutorial LMS developer.

Some of the details we shall get about a course are, instructor, course price, course id, course name, e.t.c.

As we saw when determining if a course is free or paid, courses are just normal posts. As a matter of fact, courses in Tutor LMS are custom post type of type courses. That makes even work easier as many of the stuff we shall access them natively as WordPress posts.

In this article, I’m going to illustrate how to get all course details given either product id or course id in Tutuorl LMS programmatically.

How to Get All Course Details in Tutorl LMS programmatically

Following is all details about a course in Tutor LMS programmatically

/* has linked product id */
$if_has_course = tutor_utils()->product_belongs_with_course( $product_id );

if ($if_has_course) {
  /* you must check if indeed the product id has a course */
  $course_id = $if_has_course->post_id;
}

/* has course id get the linked product id */
$has_product_id = get_post_meta( $course_id, '_tutor_course_product_id', true ); 
$product_id_data = get_post_meta( $course_id, '_tutor_course_product_id' );
if( $has_product_id ){ 
  /*again confirm if course id has the product id */
   	$product_id = $product_id_data [0];
}


// now we can get all details about the course with above info

/* determine if course if free or charged */
$is_purchasable = tutor_utils()->is_course_purchasable( $course_id );
if( $is_purchasable ){
  // course is NOT FREE
} else {
  // course is free
}


$course_name = get_the_title( $course_id );
$course_thumbnail_url = get_the_post_thumbnail_url( $course_id );


/* author details. Who created the course?*/
global $wpdb;
$course_author_user_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $course_id ) );

$author =  new WP_User( $course_author_user_id );

$display_name = $author->display_name;

$avartar = get_avatar( $course_author_user_id, 90 ); // get_avatar( $user_id, size );

$author_post_url = get_author_posts_url( $course_author_user_id );

/*Other Details*/
$course_level_data = get_post_meta( $course_id, '_tutor_course_level' ); // array
$course_level = $course_level_daa[0];

/* course permalink, both ways are correct and tested */
$course_permalink = get_permalink( $course_id );
$course_permalink = get_the_permalink( $course_id ); 


/* check if a user is an instructor */
tutor_utils()->is_instructor( $user_id ); // true or false
tutils()->can_user_manage('course', $course_id); // true or false
$instructor_data = get_user_meta( $user_id, '_is_tutor_instructor'); 
$instructor = $instructor_data[0]; // you must chekc if user isn instructor before doing this

/* all instructors in a course */
$saved_instructors = tutor_utils()->get_instructors_by_course($course_id); // returns array, to see what this array has just say, print_r( $saved_instructors )

/*number of enrolled students*/
tutor_utils()->count_enrolled_users_by_course( $course_id );

/* number of lessons in  course */
tutor_utils()->get_lesson_count_by_course( $course_id );

/* another way to get price if monetized by woocommerce */
$genearl_price = tutor_utils()->get_course_price( $course_id );
$monetize_by = tutils()->get_option('monetize_by');

if (function_exists('wc_price') && $monetize_by === 'wc'){
  $price = wc_price( $genearl_priceĀ );
}else{
  $price = $genearl_price;
}


/* check what is monetizing the whole system  */
$monetize_by = tutils()->get_option('monetize_by'); // if woocom, then value is 'wc' without quotes




 

 

Similar Posts