Support » Plugin: Advanced Woo Search » Automatically filter by Product category

  • Resolved SaraGraybill

    (@saragraybill)


    I’ve added the search bar to the top of the product category pages. Is it possible to automatically filter the search results to the category they were on when they searched?

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hello,

    Yes, it is possible with the little code snippet. Please use code

    add_filter( 'aws_search_query_array', 'my_aws_search_query_array' );
    function my_aws_search_query_array( $query ) {
        if ( isset( $_REQUEST['aws_tax'] ) && $_REQUEST['aws_tax'] === 'product_cat' && isset( $_REQUEST['aws_page'] ) && $_REQUEST['aws_page'] ) {
            global $wpdb;
            $term_id = $_REQUEST['aws_page'];
            $query['search'] .= " AND ( id IN (
                SELECT object_id FROM $wpdb->term_relationships
                WHERE term_taxonomy_id IN ( select term_taxonomy_id from $wpdb->term_taxonomy WHERE term_id IN ({$term_id}))
            ) )";
        }
        return $query;
    }
     
    add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup' );
    function my_aws_searchbox_markup( $markup ) {
        $hidden = '<input type="hidden" name="type_aws" value="true">';
        $new_fields = '<input type="hidden" name="tax" value="'.get_query_var('taxonomy').'"><input type="hidden" name="page" value="'.get_queried_object_id().'">';
        $markup = str_replace( $hidden, $hidden . $new_fields, $markup );
        return $markup;
    }

    You need to add it somewhere outside the plugins folder. For example, inside functions.php file of your theme or use some plugin for adding code snippets.

    Also after adding this code you will need to go to the plugin settings page and click the ‘Clear cache’ button.

    Regards

    Thread Starter SaraGraybill

    (@saragraybill)

    Thank you so much for your help. I added the following to my functions.php file but when I search cable in several different categories it’s still showing the same results each time rather then filtering them based on what category I am in. Is there anything else I should try?

    add_filter('yith_ywraq_quantity_loop','__return_true');
    
    add_action( 'woocommerce_archive_description', 'my_woocommerce_before_shop_loop' );
    function my_woocommerce_before_shop_loop() {
        if ( is_tax( 'product_cat' ) && function_exists( 'aws_get_search_form' ) ) {
            aws_get_search_form( true );
        }
    }
    
    add_filter( 'aws_search_query_array', 'my_aws_search_query_array' );
    function my_aws_search_query_array( $query ) {
        if ( isset( $_REQUEST['aws_tax'] ) && $_REQUEST['aws_tax'] === 'product_cat' && isset( $_REQUEST['aws_page'] ) && $_REQUEST['aws_page'] ) {
            global $wpdb;
            $term_id = $_REQUEST['aws_page'];
            $query['search'] .= " AND ( id IN (
                SELECT object_id FROM $wpdb->term_relationships
                WHERE term_taxonomy_id IN ( select term_taxonomy_id from $wpdb->term_taxonomy WHERE term_id IN ({$term_id}))
            ) )";
        }
        return $query;
    }
     
    add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup' );
    function my_aws_searchbox_markup( $markup ) {
        $hidden = '<input type="hidden" name="type_aws" value="true">';
        $new_fields = '<input type="hidden" name="tax" value="'.get_query_var('taxonomy').'"><input type="hidden" name="page" value="'.get_queried_object_id().'">';
        $markup = str_replace( $hidden, $hidden . $new_fields, $markup );
        return $markup;
    }
    Plugin Author ILLID

    (@mihail-barinov)

    Please modify your code a bit. Use instead

    add_filter('yith_ywraq_quantity_loop','__return_true');
    
    add_action( 'woocommerce_archive_description', 'my_woocommerce_before_shop_loop' );
    function my_woocommerce_before_shop_loop() {
        if ( is_tax( 'product_cat' ) && function_exists( 'aws_get_search_form' ) ) {
            aws_get_search_form( true );
        }
    }
    
    add_filter( 'aws_search_query_array', 'my_aws_search_query_array' );
    function my_aws_search_query_array( $query ) {
        if ( isset( $_REQUEST['aws_tax'] ) && $_REQUEST['aws_tax'] === 'product_cat' && isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
            global $wpdb;
            $term_id = $_REQUEST['page'];
            $query['search'] .= " AND ( id IN (
                SELECT object_id FROM $wpdb->term_relationships
                WHERE term_taxonomy_id IN ( select term_taxonomy_id from $wpdb->term_taxonomy WHERE term_id IN ({$term_id}))
            ) )";
        }
        return $query;
    }
     
    add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup' );
    function my_aws_searchbox_markup( $markup ) {
        $hidden = '<input type="hidden" name="type_aws" value="true">';
        $new_fields = '<input type="hidden" name="aws_tax" value="'.get_query_var('taxonomy').'"><input type="hidden" name="page" value="'.get_queried_object_id().'">';
        $markup = str_replace( $hidden, $hidden . $new_fields, $markup );
        return $markup;
    }
    Thread Starter SaraGraybill

    (@saragraybill)

    I’m sorry, one more question. It looks like that applies to the exact category. Is there a way to make the items show up if you are in the parent category? For example, have lenses show up under Camera/Video?

    Plugin Author ILLID

    (@mihail-barinov)

    Please try to update your code with following one

    add_filter('yith_ywraq_quantity_loop','__return_true');
    
    add_action( 'woocommerce_archive_description', 'my_woocommerce_before_shop_loop' );
    function my_woocommerce_before_shop_loop() {
        if ( is_tax( 'product_cat' ) && function_exists( 'aws_get_search_form' ) ) {
            aws_get_search_form( true );
        }
    }
    
    add_filter( 'aws_search_query_array', 'my_aws_search_query_array' );
    function my_aws_search_query_array( $query ) {
        if ( isset( $_REQUEST['aws_tax'] ) && $_REQUEST['aws_tax'] === 'product_cat' && isset( $_REQUEST['page'] ) && $_REQUEST['page'] ) {
            global $wpdb;
            $term_id = $_REQUEST['page'];
            $term_in = array();
    
            $term_childrens = get_term_children( $term_id, 'product_cat' );
            if ( ! is_wp_error( $term_childrens ) && $term_childrens ) {
                foreach( $term_childrens as $term_children ) {
                    $term_in[] = $term_children;
                }
            }
    
            $term_in[] = $term_id;
    
            $query['search'] .= " AND ( id IN (
                SELECT object_id FROM $wpdb->term_relationships
                WHERE term_taxonomy_id IN ( select term_taxonomy_id from $wpdb->term_taxonomy WHERE term_id IN (" . implode( ',', $term_in ) . "))
            ) )";
        }
        return $query;
    }
    
    add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup' );
    function my_aws_searchbox_markup( $markup ) {
        $hidden = '<input type="hidden" name="type_aws" value="true">';
        $new_fields = '<input type="hidden" name="aws_tax" value="'.get_query_var('taxonomy').'"><input type="hidden" name="page" value="'.get_queried_object_id().'">';
        $markup = str_replace( $hidden, $hidden . $new_fields, $markup );
        return $markup;
    }
    Thread Starter SaraGraybill

    (@saragraybill)

    That solved it, thank you so much working on this and helping me!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Automatically filter by Product category’ is closed to new replies.