How to change product loop options on a Woocommerce archive page?

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

But I want to change the parameters for the request, so it joins some product categories or excludes some products or some categories from the cycle (the same as we do in the .php category in the Wordpress project).

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

Thank!

+4
source share
2 answers

Woocommerce wordpress global $wp_query, pre_get_posts action hook ,

.

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

checkout woocommerce

+3

.
functions.php .
$product_category_id .

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' );
0

All Articles