How do I change the product loop parameters in Woocommerce archive page?

Victor Ferreira picture Victor Ferreira · Jul 2, 2016 · Viewed 8.3k times · Source

Well I'm trying to create some custom loop pages for Woocommerce products, and I found out that the process starts in archive-product.php file and then it include template snippets to draw the page.

But I want to change the parameters to be queried, so it will join some product categories, or will exclude some products or some categories from the loop (just like we do in category.php in a Wordpress project).

How do I do it?! Where can I find this part of the script?

Thanks!

Answer

silver picture silver · Jul 2, 2016

Woocommerce simply relies on wordpress global $wp_query, you can use pre_get_posts action hook to modify any query,

e.g.

function _additional_woo_query( $query ) {
    if ( is_product_category() ) {
        $query->set( 'cat', '123' );
    }
}
add_action( 'pre_get_posts', '_additional_woo_query' );

checkout woocommerce conditional tag