Hi,
I’m trying to filter products shown on brands page /brand/selected_brand_name/ with $_GET parameter. I when is hooked pre_get_posts on WooCommerce pages /shop archive, product category/ works fine and hooks executes. But in brands pages doesn’t hook at all.
The shop has 2 main categories which should I use to filter brands:
– TV Remote Controls / $_GET[‘tv’] / brand/selected_brand_name/?tv=1
– AC Remote Controls / $_GET[‘ac’] / brand/selected_brand_name/?ac=1
What is the user journey:
1. User lands on home page or 2 main pages:
– For TV Remotes: https://distancionni.com/distancionni-televizori/
– For AC Remosts: https://distancionni.com/distancionni-klimatici/
2. User choose a brand from the list and being forwarded to:
– For TV https://distancionni.com/brand/selected_brand_name/?tv=1
– For AC https://distancionni.com/brand/selected_brand_name/?ac=1
3. User see a list of filtered products by product category from the selected brand. Filtration is made by the $_GET parameter in the URL.
The URL parameter I could prepend to the url of each brand in the brand list with JavaScript,
but could you suggest me where to hook in your brands to be able to filter the products?
Thanks.
Here is my code from child theme functions.php:
add_action( 'pre_get_posts', 'smpl_modify_query' );
function smpl_modify_query($query) {
//if(!is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product') {
if($query->is_main_query() && $query->query_vars['post_type'] == 'product') {
//print_r($query->query_vars);
if( isset( $_GET['ac'] ) ) {
$query->set( 'tax_query', array (
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 170,
'operator' => 'IN',
)
));
}
if( isset( $_GET['tv'] )) {
$query->set( 'tax_query', array (
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 171,
'operator' => 'IN',
)
));
}
}
}