首页 > 解决方案 > 导师 LMS 如何从课程 ID 或帖子 ID 获取链接产品

问题描述

有没有办法在给定课程 ID 的情况下获得链接的 Woocommerce 产品?我了解课程 ID 与帖子 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; 
}

标签: wordpresswoocommercelms

解决方案


我研究了 Tutor LMS 源代码,/tutor/classes/woocommerce.php在第 119 行,如果您使用的是 Tutor LMS 版本 100.9.9,我发现了这个:

$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 只是任何类型帖子的普通帖子 IDcourses

我的完整代码在这里:

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; 
}

推荐阅读