This topic has 2 replies, 2 voices, and was last updated 3 years, 7 months ago ago by Olga Barlow
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',
)
));
}
}
}
Hello,
1) There are warnings on the page https://prnt.sc/11iiexw Maybe vecause of your custom or third-party plugins.
2) Try to replace your line of code with following code https://prnt.sc/11iitmn
4) Try to use the loop_shop_post_in filter
add_filter( 'loop_shop_post_in', array( $this, 'show_on_sale_products' ) );
// define the loop_shop_post_in callback
function custom_loop_shop_post_in( $query ){
//custom code here
return $query;
}
//add the action
add_filter('loop_shop_post_in', 'custom_loop_shop_post_in', 10, 1);
Regards
You must be logged in to reply to this topic.Log in/Sign up