Integrating WPLMS with Custom ecommerce Plugin

Assuming that your custom eCommerce structure is similar to WooCommerce.

Meaning it has a Custom Post type for Products.

Here is a general way of how you can do this :

a. You need to redirect a user to a Custom Page/Product page where the user can Purchase the Product.

b. Attach the Course Meta box to the Custom Post type for Products.

c. You need to run a function when the order is complete, which identifies the courses connected to the product purchased

PART I : Redirect a User to Product page when user Clicks on Take this course

1. Go to WP Admin -> Plugins -> Editor -> WPLMS Customizer -> customizer_class.php
2. Add the following code in the function_construct()

PHP Code:
add_filter('wplms_course_product_id',array($this,'wplms_course_product_id'),1,2);

3. Add the following function in the class:

PHP Code:

function wplms_course_product_id($pid,$id=NULL){
$pid = get_post_meta($pid,'vibe_product',true);    
            return $pid;
        }

 

 

PART II : Attach the Course Meta box to the Course Post type and Custom Post type for Products assumed as product.

1. Go to WP Admin -> Plugins -> Editor -> WPLMS Customizer -> customizer_class.php
2. Add the following code in the function_construct()

PHP Code:
add_filter('wplms_course_product_metabox',array($this,'wplms_course_product_metabox'));
add_filter('wplms_product_metabox',array($this,'wplms_product_metabox'));

3. Add the function in the Class:

PHP Code:
function wplms_course_product_metabox($course_product_metabox){
  $course_product_metabox[] =array(
            'label'    => __('Associated Product','vibe'), // <label>
            'desc'    => __('Associated Product with the Course.','vibe'), // description
            'id'    => $prefix.'product', // field id and name
            'type'    => 'selectcpt', // type of field
            'post_type'=> 'product',
            'std'   => ''
        );
 
return $course_product_metabox;
}
 
function wplms_product_metabox($product_metabox){
   $product_box = new custom_add_meta_box( 'page-settings', __('Product Course Settings','vibe'), $product_metabox, 'product', true ); // the product here is the custom post type
return $product_metabox;
}

PART III : Run the function which grants access to the course on order completion .

1. Go to WP Admin -> Plugins -> Editor -> WPLMS Customizer -> customizer_class.php

2. Add the following code in the _construct function :

PHP Code:
add_action('custom_order_status_completed_action',array($this,'wplms_course_enable_access'));

3. Add the following function in the Class

PHP Code:

function wplms_course_enable_access($order_id){
 
    $items = // Get the list of products associated with the order.
    $user_id= //Get the USerid of the User who placed the order.
 
    foreach($items as $item){
        $product_id = $item['product_id']; // You need to know how items/products are saved in the Order and get the ID of the product here.
 
        $subscribed=get_post_meta($product_id,'vibe_subscription',true);
 
        $courses=vibe_sanitize(get_post_meta($product_id,'vibe_courses',false));
 
 
        if(vibe_validate($subscribed) ){
 
            $duration=get_post_meta($product_id,'vibe_duration',true);
            $t=time()+$duration*86400;
 
            foreach($courses as $course){
                update_post_meta($course,$user_id,0);
                update_user_meta($user_id,$course,$t);
                $group_id=get_post_meta($course,'vibe_group',true);
                if(isset($group_id) && $group_id !='')
                groups_join_group($group_id, $user_id ); 
 
                bp_course_record_activity(array(
                      'action' => __('Student subscribed for course ','vibe').get_the_title($course),
                      'content' => __('Student ','vibe').bp_core_get_userlink( $user_id ).__(' subscribed for course ','vibe').get_the_title($course).__(' for ','vibe').$duration.__(' days','vibe'),
                      'type' => 'subscribe_course',
                      'item_id' => $course,
                      'primary_link'=>get_permalink($course),
                      'secondary_item_id'=>$user_id
                ));     
            }
        }else{   
            if(isset($courses) && is_array($courses)){
            foreach($courses as $course){
                $duration=get_post_meta($course,'vibe_duration',true);
                $t=time()+$duration*86400;
                update_post_meta($course,$user_id,0);
                update_user_meta($user_id,$course,$t);
                $group_id=get_post_meta($course,'vibe_group',true);
                if(isset($group_id) && $group_id !='')
                groups_join_group($group_id, $user_id );
 
                bp_course_record_activity(array(
                      'action' => __('Student subscribed for course ','vibe').get_the_title($course),
                      'content' => __('Student ','vibe').bp_core_get_userlink( $user_id ).__(' subscribed for course ','vibe').get_the_title($course).__(' for ','vibe').$duration.__(' days','vibe'),
                      'type' => 'subscribe_course',
                      'item_id' => $course,
                      'primary_link'=>get_permalink($course),
                      'secondary_item_id'=>$user_id
                ));
                }
            }
        }
         
    }
     
}