• I have a lot of products on my site, so I only search by category.
    There is such a hierarchy:
    All goods – Women’s clothing – Women’s outerwear – Women’s jackets.
    When I search for “Women’s Jackets” everything is ok. If I search for “Women’s Clothing”, it finds nothing. What do i do?

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

    (@mihail-barinov)

    Hello,

    Looks like these products are attached to “Women’s Jackets” but not additionally to ?“Women’s Clothing” category.

    But still you can enable the searching for these parent categories. Please use following code snippet

    add_filter( 'aws_indexed_data', 'my_aws_indexed_data' );
    function my_aws_indexed_data( $data ) {
        if ( $data && is_array( $data ) && isset( $data['terms'] ) ) {
            foreach( $data['terms'] as $source => $all_terms ) {
                $term_id = 0;
    
                if ( preg_match( '/\%(\d+)\%/', $source, $matches ) ) {
                    if ( isset( $matches[1] ) ) {
                        $term_id = $matches[1];
                        $source = preg_replace( '/\%(\d+)\%/', '', $source );
                    }
                }
    
                if ( $source === 'category' && $term_id && is_array( $all_terms ) && ! empty( $all_terms ) ) {
                    $term_parent = get_term( $term_id, 'product_cat' );
                    while ( ! is_wp_error( $term_parent ) ) {
                        $term_name  = $term_parent->name;
                        $term_name = AWS_Helpers::normalize_string( $term_name );
                        $data['terms'][$source][$term_name] = 1;
                        $term_parent = get_term( $term_parent->parent, 'product_cat' );
                    }
                }
    
            }
        }
        return $data;
    }

    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 need to re-index the plugin table.

    Regards

    Thread Starter ilyapokrov

    (@ilyapokrov)

    @mihail-barinov,
    Thank you for helping me.
    But this code didn’t help me. The search doesn’t work anyway.

    Plugin Author ILLID

    (@mihail-barinov)

    Hm, maybe I understand you wrong. Are you searching for WooCommerce products by categories or you want to show only category archive pages inside search results?

    Thread Starter ilyapokrov

    (@ilyapokrov)

    @mihail-barinov,
    You got it right.
    But I’ll try to explain it again.
    For example, there are 1 million products on the site. If you search by categories, product titles and descriptions, then this significantly loads the server and the search result takes a long time.
    Therefore, I only search by category.
    The problem is that the plugin only searches for one category in which the product is located. And you need to search for parent categories as well.
    Example:
    Clothing – women’s clothing – women’s jackets – adidas jackets – product

    If you look for “Adidas jackets”, then everything is ok. If you search for “women’s jackets”, you will not find anything.

    Plugin Author ILLID

    (@mihail-barinov)

    Ok, then the code that I give you must work for your case. Do you reindex the plugin table after adding this code snippet?

    Thread Starter ilyapokrov

    (@ilyapokrov)

    @mihail-barinov,
    Yes, I just reindexed the table. But the result is unsuccessful.
    For example, there is a category “Men’s jackets”:
    https://look-catalog.ru/muzhskie-kurtki/
    The plugin quickly finds this category.

    But this category has a child “Men’s Outerwear”. And the plugin can’t find it.
    Search is on the home page:
    https://look-catalog.ru/

    Plugin Author ILLID

    (@mihail-barinov)

    Well please try to use this addition code

    add_filter( 'aws_terms_search_query', 'my_aws_terms_search_query' );
    function my_aws_terms_search_query( $sql ) {
        $sql = str_replace( 'AND count > 0', '', $sql );
        return $sql;
    }
    
    add_filter('aws_search_tax_results', 'my_aws_search_tax_results');
    function my_aws_search_tax_results( $result_array   ) {
        if ( isset( $result_array['product_cat'] ) ) {
    
            $new_array = array();
    
            foreach( $result_array['product_cat'] as $key => $product_cat  ) {
                $term_childrens = get_term_children( $product_cat['id'], 'product_cat' );
    
                if ( $product_cat['count'] ) {
                    $new_array[] = $product_cat;
                }
    
                if ( is_wp_error( $term_childrens ) || ! $term_childrens ) {
                    continue;
                }
    
                foreach( $term_childrens as $term_children ) {
                    $term = get_term( $term_children, 'product_cat' );
                    if ( $term != null && !is_wp_error( $term ) ) {
                        $new_array[] = array(
                            'name'     => $term->name,
                            'id'       => $term->term_id,
                            'count'    => ( $term->count > 0 ) ? $term->count : '',
                            'link'     => get_term_link( $term ),
                            'excerpt'  => '',
                            'parent'   => $product_cat['id']
                        );
                    }
                }
    
            }
    
            $result_array['product_cat'] = $new_array;
    
        }
        return $result_array;
    }
    Thread Starter ilyapokrov

    (@ilyapokrov)

    @mihail-barinov,
    That didn’t help either =(

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘No search in parent categories’ is closed to new replies.