WC altering $woocommerce_loop after custom query

The problem :

Custom query in archive-page is not altering the $woocommerce_loop. $woocommerce_loop is in original state after running custom query so for shop with 34 products it always will return all the shop items.

Why i need to upadte this variable?

I found out that $woocommerce_loop is responsible for pagination and also for result count. Altering it will fix these problems.

What i was trying to do:

1 Manually set values for this $woocommerce_loop

– wc_set_loop_prop(‘total’, 5);

– $woocommerce_loop[‘loop’][‘total’] => 5;

2 Replacing main wp_query with my_query like :

$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $my_query;

Both methods have no impact for this variable so i cannot understand from where $woocommerce_loop is fetching it results.

Result of ‘var_dump ($woocommerce_loop)

array(11) {
[“loop”]=> int(0)
[“columns”]=> int(4)
[“name”]=> string(0) “”
[“is_shortcode”]=> bool(false)
[“is_paginated”]=> bool(true)
[“is_search”]=> bool(false)
[“is_filtered”]=> bool(false)
[“total”]=> string(2) “34”
[“total_pages”]=> float(3)
[“per_page”]=> int(16)
[“current_page”]=> int(1) }

For exapmle if i go to the ‘category1’ from category tree,
results will be displayed properly and variable also will be updated but if i run query that is doing the same thing (displaying all products from category1) it will display proper products with wrong result count and pagination.

How to fix it, any tips?

Read more here:: WC altering $woocommerce_loop after custom query

Leave a Reply

Your email address will not be published. Required fields are marked *