I’m working on a LearnDash project where I have a WordPress multisite, the main network site contains the LearnDash courses, and the sub sites are portals.
Courses are purchased from the portals, and after purchase the student is directed from the portal to the main site my-account page. From that page, they have a link to the course using LearnDash profile shortcode.
When the student clicks the link, the course table that lists all parts of the course opens, but when they click any of the links, instead of opening, nothing happens (because they do not have access).
The course is listed as enrolled in the User’s WordPress Profile, User Enrolled in Courses section. The problem I’m having is the student doesn’t have access to the course unless their User ID is manually added to the Course access list (when editing the course, it’s in the Courses, Course access list).
If the course is purchased on the main site 1, where the courses reside, there’s no problem and the student can access the course.
I’m looking for a short term solution where the User ID is automatically added to the Course, Course access list, for every course the student is enrolled in.
I’ve tried adding the following to the functions.php (just to testing on my dev system to see if I could get the meta_value to update)
`add_action( 'loop_start', 'your_function' ); function your_function() { $ course_id = 1646; $ metas = array( '_sfwd-courses_course_access_list' => '126', ); foreach($ metas as $ key => $ value) { update_post_meta( $ course_id, $ key, $ value ); }`
but it’s not working, mostly because I shouldn’t try that from the functions.php, and also I suspect because the meta_value contains many options and there’s more to it than just adding the update_post_meta line.
Here’s the wp_postmeta, meta_value:
a:15:{s:37:”sfwd-courses_course_short_description”;s:0:””;s:29:”sfwd-courses_course_materials”;s:0:””;s:30:”sfwd-courses_course_price_type”;s:4:”open”;s:30:”sfwd-courses_custom_button_url”;s:0:””;s:25:”sfwd-courses_course_price”;s:0:””;s:31:”sfwd-courses_course_access_list”;s:13:”9,11,13,15,14″;s:34:”sfwd-courses_course_lesson_orderby”;s:10:”menu_order”;s:32:”sfwd-courses_course_lesson_order”;s:3:”ASC”;s:40:”sfwd-courses_course_prerequisite_compare”;s:3:”ANY”;s:26:”sfwd-courses_course_points”;s:1:”0″;s:33:”sfwd-courses_course_points_access”;s:1:”0″;s:31:”sfwd-courses_expire_access_days”;s:0:””;s:24:”sfwd-courses_certificate”;s:4:”1565″;s:40:”sfwd-courses_course_prerequisite_enabled”;s:3:”off”;s:32:”sfwd-courses_course_prerequisite”;a:0:{}}
I did find a place in the LearnDash profile.php, where the code is to list the students courses that they have purchased, and that code is being called in the my-account, dashboard where the student clicks to the link to access the course.
<?php foreach ( $ user_courses as $ course_id ) : ?> <?php $ course = get_post( $ course_id); $ course_link = get_permalink( $ course_id ); $ progress = learndash_course_progress( array( 'user_id' => $ user_id, 'course_id' => $ course_id, 'array' => true ) ); $ status = ( $ progress['percentage'] == 100 ) ? 'completed' : 'notcompleted'; ?>
I just need to find a method to update the sfwd-courses_course_access_list meta_value. I’ve tried adding the following directly below the $ course = get_post( $ course_id);
$ course_id = 1646; $ metas = array( '_sfwd-courses_course_access_list' => '126', ); foreach($ metas as $ key => $ value) { update_post_meta( $ course_id, $ key, $ value ); }
The above doesn’t work and I’ve searched high and low for the answer and I’m completely stuck. My question is, what is the proper way to update the meta_value in this situation?