In the previous article, we did learn how to get a course id given product id. Today we shall be doing the vice versa, getting product Id from course Id.

Although Tutor LMS is one of the most used LMS systems in the space, it lacks a codebase.

So most of the time one is forced to peruse the codes in the plugin to understand how things have been done.

As a freelancer WordPress Developer, I have been faced with scenarios where the client needs customizations only available in code.

How to get Product Id from Course Id in Tutor LMS

When we did an article on how to check if a course is free or paid in tutor LMS, we learned that courses are just normal posts, just as products are posts of type products.

The following code gives an illustration on how to fetch product id given course Id. In simple terms, how to get product Id from course Id

$has_product_id = get_post_meta( $post_id, '_tutor_course_product_id', true ); 
$product_id_data = get_post_meta( $post_id, '_tutor_course_product_id' );

 if( $has_product_id ){ 
 	$product_id = $product_id_data [0];
 	$product = wc_get_product( $product_id );
 	$short_description = $product->get_short_description();
 	$permalink = get_permalink( $product->get_id() );
 	$price = $product->get_price();
 	$image_url = get_the_post_thumbnail_url( $product_id );
 	$name = $product->get_name();

To get the Product id from the course id just do this:

$product_id_data = get_post_meta( $post_id, '_tutor_course_product_id' );
$product_id = $product_id_data [0];


Similar Posts