[Tutor LMS] 透過課程 id 取得相關資料

分享:

如果知道 Tutor LMS 的課程 id 後,就可以透過該 id 取得更多課程相關的資料。

首先先透過函式檢查該 id 是否為課程,因為 Tutor LMS 回傳的 post 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' );

接著就是判斷只要是課程,就近一步使用 wc_get_product( ), get_permalink( $product->get_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();
 }

 

來源:
Tutor LMS Get Linked Product from Course ID Programatically

 

課程推薦

和我們交流

加入我們的社群,裡面會有一些技術的內容、有趣的技術梗,以及職缺的分享,歡迎和我們一起討論。