Filter Brands page loop with tax_query - by saimana

This topic has 2 replies, 2 voices, and was last updated 2 years, 10 months ago ago by Olga Barlow

  • Avatar: saimana
    saimana
    Participant
    April 15, 2021 at 21:39

    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',
                    )
                ));
            }
        }
    }
    
    1 Answer
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    April 16, 2021 at 13:49

    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

  • Viewing 2 results - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.