skip to Main Content

I am using WPLMS theme for a course management website. I am using following code to display a button in the details page.

add_filter('wplms_before_course_description','custom_add_second_button');
function custom_add_second_button($course_details){
  $custom_link = get_post_meta(get_the_ID(),'Custom_button_link',true);
  if(isset($custom_link)){
    echo '<a class="full button" href="'.$custom_link.'"><span class="vicon vicon-shopping-cart"></span> Buy This Course</a>';
    return $course_details;
  }
  return $course_details;
}

This is to show a custom add to cart button beside the title of the course. Now what I want is if a student has already purchased this course, this button will not come up. Kindly help me to achieve my goal please. Thanks in advance.

2

Answers


  1. You can use WC wc_customer_bought_product() function to check whether the customer already bought it or not.

    add_filter( 'wplms_before_course_description','custom_add_second_button' );
    function custom_add_second_button($course_details){
        
        global $product;
        $custom_link  = get_post_meta( get_the_ID(), 'Custom_button_link', true );
    
        if( is_user_logged_in() ){
            $current_user = wp_get_current_user();
            if( !wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) && isset( $custom_link ) ){
                echo '<a class="full button" href="'.$custom_link.'"><span class="vicon vicon-shopping-cart"></span> Buy This Course</a>';
                return $course_details;
            }
        }else{
            if( isset( $custom_link ) ){
                echo '<a class="full button" href="'.$custom_link.'"><span class="vicon vicon-shopping-cart"></span> Buy This Course</a>';
                return $course_details;
            }
        }
        return $course_details;
    
    }
    

    USEFUL LINKS.

    Login or Signup to reply.
  2. You can use the function bp_course_is_member($course_id,$user_id)) to check if the user is already a member of the course.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search