导师 LMS 如何从课程 ID 或 post ID 获取链接产品
Tutor LMS How to get Linked Product from course id or post id
有没有办法根据课程 ID 获取链接的 Woocommerce 产品?我知道课程 ID 与 post ID 相同。我想在课程详细信息页面上显示链接的产品
add_filter( 'the_content', 'codemode__show_linked_product' );
function codemode__show_linked_product( $content ){
global $post;
$post_id = $post->ID;
if( is_single( $post ) ){
if( get_post_type( $post ) == 'courses' ){
// get the linked product here
$linked_product_id = get_linked_product( $post_id );
}
}
return $content;
}
我研究了 Tutor LMS 源代码和第 119 行 /tutor/classes/woocommerce.php
如果您使用的是 100.9.9 版本的 Tutor LMS,我发现了这个:
$has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true);
因此,要获取课程的链接产品 ID,您可以这样获取:
$product_id_array = get_post_meta( $course_id, '_tutor_course_product_id' );
$product_id = $product_id_array[0];
请注意课程 ID 只是正常的 post 类型 courses
的任何 post 的 ID
我的完整代码在这里:
add_filter( 'the_content', 'codemode__show_linked_product' );
function codemode__show_linked_product( $content ){
global $post;
$post_id = $post->ID;
$data = "";
if( is_single( $post ) ){
if( get_post_type( $post ) == 'courses' ){
// get the linked product here
$has_product_id = get_post_meta( $post_id, '_tutor_course_product_id', true );
$product_id = get_post_meta( $post_id, '_tutor_course_product_id' );
if( $has_product_id ){
$product = wc_get_product( $product_id[0] );
$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[0] );
$name = $product->get_name();
$data = "
<style>
.flex-horiz{
display: flex; /* establish flex sys-container */
flex-direction: column; /* make main axis vertical */
align-items: center; /* center items horizontally, in this case */
}
.product-item{
padding: 7px 5px;
text-align: center;
}
.codemode-linked-product{
padding: 15px;
}
</style>
<div class='codemode-linked-product flex-horiz'>
<center><h4>The Linked Product</h4></center>
<div style='max-width: 300px; height: auto; border: 1px solid #E1E1E1; pading: 10px; border-radius: 5px;'>
<center><img src='$image_url' style='max-height: 250px;' /></center>
<div class='product-item product-name'>$name</div>
<div class='product-item product-price'>Price: $price</div>
<div class='product-item product-short-descriptioin'>$short_description</div>
<div class='product-item product-permalink'><center><a href='$permalink' class='gradiant gradiant-hover btn'>View</a></center></div>
</dikv>
</div>";
}
}
}
return $content.$data;
}
有没有办法根据课程 ID 获取链接的 Woocommerce 产品?我知道课程 ID 与 post ID 相同。我想在课程详细信息页面上显示链接的产品
add_filter( 'the_content', 'codemode__show_linked_product' );
function codemode__show_linked_product( $content ){
global $post;
$post_id = $post->ID;
if( is_single( $post ) ){
if( get_post_type( $post ) == 'courses' ){
// get the linked product here
$linked_product_id = get_linked_product( $post_id );
}
}
return $content;
}
我研究了 Tutor LMS 源代码和第 119 行 /tutor/classes/woocommerce.php
如果您使用的是 100.9.9 版本的 Tutor LMS,我发现了这个:
$has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true);
因此,要获取课程的链接产品 ID,您可以这样获取:
$product_id_array = get_post_meta( $course_id, '_tutor_course_product_id' );
$product_id = $product_id_array[0];
请注意课程 ID 只是正常的 post 类型 courses
我的完整代码在这里:
add_filter( 'the_content', 'codemode__show_linked_product' );
function codemode__show_linked_product( $content ){
global $post;
$post_id = $post->ID;
$data = "";
if( is_single( $post ) ){
if( get_post_type( $post ) == 'courses' ){
// get the linked product here
$has_product_id = get_post_meta( $post_id, '_tutor_course_product_id', true );
$product_id = get_post_meta( $post_id, '_tutor_course_product_id' );
if( $has_product_id ){
$product = wc_get_product( $product_id[0] );
$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[0] );
$name = $product->get_name();
$data = "
<style>
.flex-horiz{
display: flex; /* establish flex sys-container */
flex-direction: column; /* make main axis vertical */
align-items: center; /* center items horizontally, in this case */
}
.product-item{
padding: 7px 5px;
text-align: center;
}
.codemode-linked-product{
padding: 15px;
}
</style>
<div class='codemode-linked-product flex-horiz'>
<center><h4>The Linked Product</h4></center>
<div style='max-width: 300px; height: auto; border: 1px solid #E1E1E1; pading: 10px; border-radius: 5px;'>
<center><img src='$image_url' style='max-height: 250px;' /></center>
<div class='product-item product-name'>$name</div>
<div class='product-item product-price'>Price: $price</div>
<div class='product-item product-short-descriptioin'>$short_description</div>
<div class='product-item product-permalink'><center><a href='$permalink' class='gradiant gradiant-hover btn'>View</a></center></div>
</dikv>
</div>";
}
}
}
return $content.$data;
}