Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 405
Victor Ferreira Avatar asked Jul 02 '16 05:07

Victor Ferreira


Video Answer


2 Answers

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

like image 113
silver Avatar answered Oct 19 '22 04:10

silver


Try the following code.
Paste it in functions.php of your theme.
Replace $product_category_id with your value.

function _new_updated_query( $query ) {
if ( is_product_category() && $query->is_main_query() ) {
    $query->set( 'tax_query', array (
        array(
          'taxonomy' => 'product_cat',
          'field'    => 'term_id',
          'terms'    => absint($product_category_id),
        )
    ));
}
}
add_action( 'pre_get_posts', '_new_updated_query' );
like image 1
Pranay Srivastava Avatar answered Oct 19 '22 06:10

Pranay Srivastava