My site uses LMS plugin and WooCommerce.
On the cart page, when there is a product bundle, it will appear
Warning: Invalid argument supplied for foreach()
This invalid argument appears, to display the related courses (upsell) at the bottom of the cart page. But the related course is still showing even if this warning appears
This warning, appears for
foreach ($items as $item) {
However, when there are no bundled products in the cart, this warning does not appear.
Here's my code :
<?php
$category_ids = [];
$course_ids = [];
if (!empty($args['categories'])) {
foreach ($args['categories'] as $items) {
foreach ($items as $item) {
$category_ids[] = $item->term_id;
}
}
}
$category_ids = array_unique($category_ids);
$categories = theme_get_category_terlaris();
$filter_cat = array_intersect_key($categories, array_flip($category_ids));
$terms_ids = array_keys($filter_cat);
$params = array(
'fields' => 'ids',
'posts_per_page' => 6,
'ignore_sticky_posts' => true,
'orderby' => 'meta_value_num',
'meta_key' => '_theme_course_sales',
'order' => 'DESC',
'post_type' => 'courses',
'post_status' => 'publish',
'author__not_in' => theme_get_stopped_mentors(),
);
if (!empty($args['posts'])) {
$params['post__not_in'] = $args['posts'] ?? [];
}
if (count($terms_ids) == 1) {
$params['tax_query'] = array(
array(
'taxonomy' => 'course-category',
'field' => 'term_id',
'terms' => $terms_ids,
)
);
$courses = new WP_Query($params);
$course_ids = $courses->posts;
} else {
$params['posts_per_page'] = 3;
$params['tax_query'] = array(
array(
'taxonomy' => 'course-category',
'field' => 'term_id',
'terms' => array($terms_ids[0]),
)
);
$courses_1 = new WP_Query($params);
$params['tax_query'] = array(
array(
'taxonomy' => 'course-category',
'field' => 'term_id',
'terms' => array($terms_ids[1]),
)
);
$courses_2 = new WP_Query($params);
$course_ids = array_merge($courses_1->posts, $courses_2->posts);
}
if (count($terms_ids) > 2 && count($course_ids) < 6) {
$params['posts_per_page'] = 6 - count($course_ids);
$params['tax_query'] = array(
array(
'taxonomy' => 'course-category',
'field' => 'term_id',
'terms' => array($terms_ids[2]),
)
);
$courses = new WP_Query($params);
$course_ids = array_merge($course_ids, $courses->posts);
}
if (count($course_ids) < 6) {
$additional = new WP_Query(
array(
'fields' => 'ids',
'posts_per_page' => 6 - count($course_ids),
'ignore_sticky_posts' => true,
'orderby' => 'meta_value_num',
'meta_key' => '_theme_course_sales',
'order' => 'DESC',
'post_type' => 'courses',
'post_status' => 'publish',
'author__not_in' => theme_get_stopped_mentors(),
'tax_query' => array(
array(
'taxonomy' => 'course-category',
'field' => 'term_id',
'terms' => $category_ids,
'operator' => 'NOT IN',
)
),
)
);
$course_ids = array_merge($course_ids, $additional->posts);
}
if (!empty($course_ids)):
?>
<div class="cart--more-courses">
<div class="container">
<div class="row">
<div class="col-md-12">
<h3 class="heading-3 cart--courses-heading">Related Courses</h3>
<div class="theme-course-carousel">
<div class="theme-course-carousel--entries">
<?php
foreach ($course_ids as $course_id):
set_query_var('course_id', $course_id);
load_template(PN_THEME_PATH . '/template-parts/course-card.php', false);
endforeach;
?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
endif;
?>
Any help and answers, really appreciate. Thanks
I'm try to add $product_ids = [];
But this warning still appears when there is product bundle in cart page.