GET FREE VERSION GET THE PRO VERSION

Adding breadcrumbs for categories

Learn how to display breadcrumbs ( full hierarchy ) for product categories when using the Advanced Woo Search plugin.

In this article

Overview

With the Advanced Woo Search plugin you can search for both WooCommerce products results and archive pages like categories, tags, attributes, etc.

When searching for taxonomies like product categories in the search results you will see this category name and the number of products that have this category.

Default categories search results

Default categories search results

But it is possible that you need not only to show the name of this category, but also the names of all its parent categories.

It is very handy when, for example, you have two categories with the same name but different parent categories. For example T-shirts: Women's Clothing -> T-shirts and Men's Clothing -> T-shirts.

Categories search results with hierarchy view

Categories search results with hierarchy view

Below we will learn how to archive this by using some custom code snippets that extend search results output.

To add a hierarchy view for categories search results as mentioned above you need to use the following code snippet:

add_filter( 'aws_search_tax_results', 'my_aws_search_tax_results', 10, 2 );
function my_aws_search_tax_results( $result_array, $taxonomy ) {
    $taxonomy_name = 'product_cat';
    if ( isset( $result_array[$taxonomy_name] ) ) {
        foreach ( $result_array[$taxonomy_name] as $tax_key => $tax_item ) {
            $tax_name = $tax_item['name'];
            $parent_tax = isset( $tax_item['parent'] ) && $tax_item['parent'] ? $tax_item['parent'] : 0;
            if ( $parent_tax ) {
                do {
                    $term = get_term( $parent_tax, $taxonomy_name );
                    if ( $term ) {
                        $tax_name = $term->name . ' -> ' . $tax_name;
                        $parent_tax = $term->parent;
                    }
                } while ($parent_tax);
            }
            $result_array[$taxonomy_name][$tax_key]['name'] = $tax_name;
        }
    }
    return $result_array;
}

Note: it is possible to add search hierarchy views not only for product categories, but for any hierarchical taxonomy. Just replace product_cat with the needed taxonomy slug.

Category breadcrumbs for products results

Just as for taxonomies we can add the same categories hierarchical view for the products search results.

Categories hierarchy view for products search results

Categories hierarchy view for products search results

Just use the following code snippet:

add_filter( 'aws_search_pre_filter_single_product', 'my_aws_search_pre_filter_single_product', 10, 3 );
function my_aws_search_pre_filter_single_product( $result, $post_id, $product ) {

    $terms = get_the_terms( $post_id, 'product_cat' );

    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {

        $cats_array_temp = array();

        foreach ( $terms as $term ) {
            if ( is_object( $term ) && property_exists( $term, 'name' ) ) {

                $term_name = $term->name;
                $parent_term = $term->parent ? $term->parent : 0;

                if ( $parent_term ) {
                    do {
                        $new_term = get_term( $parent_term, 'product_cat'  );
                        if ( $new_term ) {
                            $term_name = $new_term->name . ' -> ' . $term_name;
                            $parent_term = $new_term->parent;
                        }
                    } while ($parent_term);
                }

                $cats_array_temp[] = $term_name;

            }
        }

        if ( ! empty( $cats_array_temp ) ) {
            $result['categories'] = implode( ', ', $cats_array_temp );
        }

    }

    return $result;
}

Note: if you want to display other product taxonomies - read this article that includes some examples of how to do that.