如果知道 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