I'm trying to code a custom function to load set of products in seperate divs to be used inside tabs. Each tab has the name of the category, and each tab-content holds the products.
I'm trying to use woocommerce template to display the product in the loop, but it fires an echo right away, which breaks the design. I need to receive the results into output param, and echo it at the end.
My code, after looping the categories (foreach as $prod_cat) looks like this:
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'tag_ID',
'terms' => $prod_cat['id']
)
),
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
$counter = 0;
if ($my_query->post_count) {
$output .= '<ul class="products">';
if ($my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post();
setup_postdata($post);
$output .= woocommerce_get_template_part('content', 'product');
endwhile;
}
$output .= '</ul>';
}
So it actually works and loads the products fine, but since "woocommerce_get_template_part" calls "load_template", it echos the file content. I want it to return to $output without any echo. Is that possible ?
Thanks
If I've understood the problem correctly, you should be able to do this with PHP's output buffering functionality. Something like this (untested):
while ($my_query->have_posts()) : $my_query->the_post();
setup_postdata($post);
ob_start(); // Start buffering
woocommerce_get_template_part('content', 'product');
$output .= ob_get_clean(); // Get the content of the buffer, and end buffering
endwhile;