Search code examples
wordpresswoocommercelms

Tutor LMS How to get Linked Product from course id or post id


Is there a way to get a linked Woocommerce product given the course id? I understand that the course id is the same as the post id. I want to display linked products on the course detail page

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

Solution

  • I have studied the Tutor LMS source code and on /tutor/classes/woocommerce.php on line 119 that is if you are using version 100.9.9 of Tutor LMS, I found this:

    $has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true);
    

    Therefore to get the linked product id of a course, you would get it like this:

    $product_id_array = get_post_meta( $course_id, '_tutor_course_product_id' ); 
    $product_id = $product_id_array[0];
    

    Please note course id is just normal post id of any post of type courses

    my full code is here:

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