wordpresspointslearndash

WP Learndash plugin, get course points of user from subsite


I have a multisite setup of wordpress. I am using Learndash in subsite(xyz.com) and need to get the course points on my main site(abc.com).

I have a reward system in abc.com. When an end user buy some product, he will get 100 points.

On xyz.com I have Installed Learndash, if that user completes a course he will get 100 points and I need to add those course points in main site points. So it will display 200 points in his account.

How can I get the course points from xyz.com to abc.com?

I am using the multisite so the database is same.


Solution

  • <?php
    $userID = get_current_user_id();
    $blog_id = 0;
    if (is_subdomain_install()) {
        // For subdomain installs
        $blog_id = get_blog_id_from_url($_SERVER['HTTP_HOST']);
    } else{
        // For subdirectory installs
        $blog_id = get_blog_id_from_url($_SERVER['HTTP_HOST'] , "/risconew/university/" );
        if (empty($blog_id)) {
            $blog_id = get_blog_id_from_url($_SERVER['HTTP_HOST'] , "/risco-university/" );
        }
    }
    
    global $wpdb;
    $tablePrifix = $wpdb->get_blog_prefix($blog_id);
    $totalPoints = array();
    $activitytable =  $tablePrifix . "learndash_user_activity";
    $postmetatable =  $tablePrifix . "postmeta";
    $PostMetaArray = $wpdb->get_results("SELECT act.*,postmeta.* FROM $activitytable AS act JOIN $postmetatable AS postmeta ON act.post_id = postmeta.post_id WHERE act.activity_type = 'course' AND act.activity_status = 1 AND act.user_id = " . $userID);
    foreach ($PostMetaArray as $PostMeta) {
        $postMetaValue = $PostMeta->meta_value;
        $postMetaValueUnserialize = unserialize($postMetaValue);
        $points = $postMetaValueUnserialize['sfwd-courses_course_points'];
        if (!empty($points)) {
            $totalPoints[] = (int) $points;
        }
    }
    $totalCoursePoints = array_sum($totalPoints);
    echo 'Total Course Points: ' . $totalCoursePoints;
    ?>