I have this code below:
function get_user_fields($user, $output = true, $main_title = 'Anmäld till kurs/kurser', $course_update = false) {
if ($output === true) {
if (strlen($title)>0) {
echo '<h3>' . $main_title . '</h3>';
}
}
//Get locations (course-categories taxonomy) that starts with kurser_ in it's permalink (slug)
$locations = get_categories_with_slug('kurser_');
//Get list of all categorycourses
$categorycourses = get_terms('categorycourses');
$save_meta = array(); //When no output, use function for storing metadata-array to save
foreach($locations as $location) {
if ($output === true) {
echo '<h3 class="location">'.$location->name.'</h3>';
}
foreach($categorycourses as $categorycourse) {
//When a coursecategory has location as it's parent, then show courses for category
if ($categorycourse->parent == $location->cat_ID) {
wp_reset_query();
$args = array('post_type' => 'course',
'tax_query' => array(
array(
'taxonomy' => 'categorycourses',
'field' => 'slug',
'terms' => $categorycourse->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
$course_field_name = 'course_' . get_the_ID();
//Check when updating course
if ($course_update === true) {
echo 'course update is true';
//Course is selected by user in db, is it set as reserve?
if (get_user_meta($user->ID, $course_field_name) == 1) {
echo 'course field name is set';
//User is selected as reserve for this course in db
if (get_user_meta($user->ID, $course_field_name.'_reserve') == 1) {
echo 'course field reserve is 1';
//Can user be selected as non-reserve?
//(course is not full any longer)
$nr_available = intval(get_field('nr_available'));
echo 'nr available = ' . $nr_available;
if ($nr_available > 0) {
//User is no longer reserve for this course, save info to db
update_usermeta($user->ID, $course_field_name, 0); //FOR TESTING
update_usermeta($user->ID, $course_field_name.'_reserve', 0);
}
}
}
}
if ($output === true) {
echo '<h4 class="rubrik">'.$categorycourse->name.'</h4>';
echo '<table class="form-table courses" cellspacing="0" cellpadding="0"><thead>';
echo '<tr><th>Kursnamn</th><th>Startvecka</th><th>Veckodag</th><th>Starttid</th><th>Pris</th><th>Antal platser</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
}
while($loop->have_posts()) : $loop->the_post();
$nr_available = intval(get_field('nr_available'));
$status_available_full = 0;
if ($nr_available == 0) {
$nr_message = 'fullt';
$status_available_full = 1;
}
else if ($nr_available<=4) {
$nr_message = 'fåtal platser';
}
else {
$nr_message = 'platser finns';
}
if ($output === true) {
$checked = get_user_meta($user->ID, 'course_' . get_the_ID(), true);
if ($checked == 1) {
$checked = ' checked="checked"';
}
else {
$checked = ' ';
}
echo '<tr>';
echo '<td class="title course-name">';
echo '<input type="checkbox"' . $checked . 'name="course_' . get_the_ID() . '"> ' . get_the_title();
//If course is full then be able to book pupil as reserve
if ($status_available_full === 1) {
echo '<span class="reserve">(reserv)</span>';
}
echo '</td>';
echo '<td class="title start-week">' . get_field('start_week') . '</td>';
echo '<td class="title week-day">' . get_field('week_day') . '</td>';
echo '<td class="title start-time">' . get_field('start_time') . '</td>';
echo '<td class="title price">' . get_field('course_cost') . '</td>';
echo '<td class="title available">' . $nr_message . '</td>';
echo '</tr>';
}
else {
//Add info to array when using not output
$save_meta[] = array(
'course' => $course_field_name,
'is_reserve' => $status_available_full
);
}
endwhile;
if ($output === true) {
echo '</tbody>';
echo '</table>';
}
}
}
} //End foreach categorycourses
} //End foreach locations
//Return array when no output is done
if ($output !== true) {
return $save_meta;
}
}
/* SAVE EXTRA FIELDS */
/* helper-function to save extra fields */
function save_user_fields($user_id) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
//Save values of checkbox-fields into db
$save_meta = get_user_fields($current_user, false); //False tells that no output should be done
//Go through all courses and save them
foreach($save_meta as $current_meta) {
$course = $current_meta['course'];
$is_reserve = $current_meta['is_reserve'];
$update = 0;
if (isset($_POST[$course])) {
$chkbox = $_POST[$course];
if ($chkbox == 'on') {
$update = 1;
}
}
//Save values from form into db
update_usermeta($user_id, $course, $update); //Save course into db with format course_{id of course)
update_usermeta($user_id, $course . '_reserve', $is_reserve); //Save if user is reserve (1) or not (0) for this course
}
}
//Hooks for user (Update/add)
add_action('personal_options_update', 'save_user_fields');
add_action('edit_user_profile_update', 'save_user_fields');
add_action('user_register', 'save_user_fields'); //When adding new users
I use function save_user_fields for saving meta-data for user into database. I'm not sure I can use $current_user
variable inside this function? (when calling get_user_fields)
Basically, the code is generating a list of custom taxonomy categories in userprofile with checkbox and when updating user-profile the value of the checkboxes (category selected or not) are saved into db. This seems to work.
I'm saving this metadata in save_user_fields()
//Save course into db with format course_{id of course) (1=checked, 0 = unchecked)
update_usermeta($user_id, $course, $update);
//Save if user is reserve (1) or not (0) for this course
update_usermeta($user_id, $course . '_reserve', $is_reserve);
I wonder why the code does not get past the statement (in get_user_fields-function, beginning of "the loop") if (get_user_meta($user->ID, $course_field_name) == 1) {
?
It does not echo out 'course field name is set'
I'm also using the plugin Advanced Custom Fields, so therefore I'm using get_field()