Price slider interferes with default query (fix included)
-
Hi,
I had a small problem with your plugin. The scenario involved a store that has 200+ items and a frankly ridiculous amount of custom attributes and taxonomies (basically, there were too many of them to properly determine if they are the ones causing a problem in the plugin).The problem in question is that the price filter interfered with the main wp_query of the woocommerce store – but only on the default load of the shop page. In the first load of the shop page, this problem caused pagination to be completely broken. However, if I set practically anything that triggered an ajax reload, (including: clicking on the plugin widgets checkboxes, setting the price silder, or modifying the sort settings) pagination returned as normal.
The odd part of this was that I could set a different sort order, then set the default sort order (returning the shop page to its “default” state), and this also caused pagination to appear as it should.
I traced this problem to includes/widget.php, circa line 296, in the public function get_filter_products(). This function calls a WP_Query with the various arguments set by the other (non slider) widgets, to get a filtered list of product IDs. This is then used elsewhere to look up prices and further filter them according to how the slider is modified on the front end.
The problem was that the get_filter_products() function calls a GLOBAL wp_query… thereby modifying the default shop query. A quick and dirty fix was to rename every $wp_query to $wp_query2 in this function only, and to remove $wp_query from the globals declared at the start of the function. Also, near the end of the function, where you call while_posts() and the_post(), call those with $wp_query2->while_posts() and $wp_query2->the_post().
(However, it might be faster to just do a foreach loop on $wp_query2->posts, since you are only collecting a list of IDs from them!)Please note that I’m only using 3-4 attribute checkboxes with OR queries, and a price slider. I did not test how this affects any of the other filtering modes, so this solution might have bugs on other modes.
I could not see any reason to why a global wp_query is called in that particular function, but I did not look on the workings of the plugin beyond this bug. If there is indeed no reason for that to be there, this fix may help others as well.
https://www.ads-software.com/plugins/woocommerce-ajax-filters/
- The topic ‘Price slider interferes with default query (fix included)’ is closed to new replies.