Reputation: 407
I have an issue with a product loop in WooCommerce. My code is as follows:
<?
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)
),
'suppress_filters' => 0
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
wc_get_template_part('content', 'product');
endwhile;
wp_reset_postdata();
?>
As you can see the loop shows featured products and excludes products out of stock. The problem is that the WC classes "first" and "last" is not added correctly to the results.
The first product in the loop gets the class "last", instead of the fourth as it should. This varies depending on which product I set to featured.
Anyone experienced the same?
EDIT: The issue seems to be related to having two product queries on the same page. The featured query shows 3 products, which makes the first product of the products on sale query have the last class.
Upvotes: 1
Views: 974
Reputation: 101
I removed the unnecessary first/last classes using this piece of dirty code:
function loop_columns() {
return 1000;
}
add_filter('loop_shop_columns', 'loop_columns', 999);
Upvotes: 0
Reputation: 420
I just had the same issue. I checked the source code of WooCommerce and the problem is in the wc_get_loop_class()
method.
To fix it just call:
wc_set_loop_prop( 'loop', 0 );
before each product query. In this way the internal product loop counter is reset correctly.
Upvotes: 3