Show course purchase status on the course page.

Please add this code in your wplms-customizer.php file present in your wplms customizer plugin:

add_filter('wplms_course_details_widget','custom_show_course_status',99,2);
function custom_show_course_status($course_details,$course_id){
  if(!is_user_logged_in())
    return $course_details;

  $user_id = get_current_user_id();
  $customer_email = $current_user->email;
  $product_id = get_post_meta($course_id,'vibe_product',true);

  global $wpdb;
  $customer_data = array( $user_id );

    if ( $user_id ) {
      $user = get_user_by( 'id', $user_id );

      if ( isset( $user->user_email ) ) {
        $customer_data[] = $user->user_email;
      }
    }

    if ( is_email( $customer_email ) ) {
      $customer_data[] = $customer_email;
    }

    $customer_data = array_map( 'esc_sql', array_filter( array_unique( $customer_data ) ) );

    if ( sizeof( $customer_data ) == 0 ) {
      return false;
    }
  $result = $wpdb->get_results( "
      SELECT im.meta_value, im.meta_key, i.order_id FROM {$wpdb->posts} AS p
      INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
      INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
      INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
      WHERE p.post_status IN ( 'wc-completed', 'wc-processing', 'wc-on-hold', 'wc-pending' )
      AND pm.meta_key IN ( '_billing_email', '_customer_user' )
      AND im.meta_key IN ( '_product_id', '_variation_id' )
      AND im.meta_value != 0
      AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
    " );
  
  $order_ids = array();
  foreach ($result as $key => $value) {
    if($value->meta_value == $product_id){
      $order_ids[] = $value->order_id;
    }
  }
  if(empty($order_ids))
    return $course_details;
  
  $id = max($order_ids);
  $order = new WC_Order($id);
  $course_status = $order->post_status;
  $status = '';
  switch ($course_status) {
    case 'wc-pending':
    case 'wc-processing':
    case 'wc-on-hold':
      $status = 'Order processing';
      break;
    case 'wc-completed':
    case 'wc-cancelled':
    case 'wc-refunded':
    case 'wc-failed':
    default:
      $status = 0;
      break;
  }
   
  if(!empty($status))
  $course_details['status'] = $status;

  return $course_details;
}

RESULT: If the order is not completed then it will show Order processing in course details section refer:

Was this article helpful?

Related Articles

1 Comment

  1. jackal

    Hi! How to add a class to it and put it under list tag?

Leave A Comment?