Cant find full loop example on the web with wc_get_template_part()
and wc_get_products()
, so looking for help here:
global $woocommerce;
global $product;
$args = array(
'limit' => 15,
'category' => array('printers', 'laptop')
);
$query_cats = wc_get_products($args);
foreach ($query_cats as $query_cat) {
echo $query_cat->get_id();
echo $query_cat->get_title();
// echo "<pre>";
// var_dump($query_cat);
wc_get_template_part('content', 'product');
}
?>
Titles and ids are displayed, var_dump also, bu wc_get_template_part – no. I have add_theme_support('woocommerce')
; and also body_class()
;
2
Answers
WooCommerce content-product.php template only works only with standard loop(with instance of Wp_Query). May be following solution can help:
Thanks
Yes, it can be done (2022 update)
If you want to avoid native
WP_Query
or using shortcodes, this actually can be done usingwc_get_products()
.You just need to setup and reset postdata within your foreach loop and setup WooCommerce loop properly.
Important note: this will only work if queried products are within any HTML element with the
woocommerce
class (otherwise WooCommerce CSS won’t load for your products). In some templates, thewoocommerce
class is already part of the DOM (e.g.<body>
element), but if it isn’t, wrap your loop within an element as such:TIP: You can set various loop properties using
wc_set_loop_prop
in the "Set loop properties" part of the code