Learn how to create dynamic search form filters for all available top level product categories.
In this article, we will learn how to create dynamic search form filters for all available top-level product categories.
As you may know, with Advanced Woo Search PRO, it is possible to create search form filters. These filters can show or hide search results based on a variety of parameters, such as taxonomies, attributes, custom fields, etc.
But what if we want to create search form filters for all available product categories, like these ones:
Choosing any of these filters will display product results only for the selected category.
By default, you would need to manually create a filter for each category. However, this is not practical if you have a large number of categories or if they change frequently.
Fortunately, there is a solution to create such search form filters automatically.
Simply use the following code snippet:
class AWS_Dynamic_Filters { private $inherit_filter = 1; private $current_filter = false; function __construct() { add_filter( 'shortcode_atts_aws_search_form', array( $this, 'shortcode_atts_aws_search_form' ) ); add_action( 'init', array( $this, 'init' ) ); add_filter( 'aws_front_current_filter', array( $this, 'aws_front_current_filter' ) ); add_filter( 'aws_front_data_parameters', array( $this, 'aws_front_data_parameters' ) ); add_filter( 'aws_search_query_array', array( $this, 'aws_search_query_array' ) ); } function shortcode_atts_aws_search_form( $out ) { $terms = get_terms([ 'taxonomy' => 'product_cat', 'hide_empty' => false, ]); $parent_terms = array_filter($terms, function($term) { return $term->parent === 0; }); $filters_arr = array(); if ( $parent_terms ) { foreach ($parent_terms as $parent_term) { $name = str_replace( "'", '"', $parent_term->name ); $filter_id = 'cat_' . $parent_term->term_id; $filters_arr[$filter_id] = array( 'filter_name' => $name, ); } } if ( ! empty( $filters_arr ) ) { foreach ( $filters_arr as $filter_id => $filter_opts ) { $out['filters'][$filter_id]= $filter_opts; } } return $out; } function init() { if ( isset( $_REQUEST['aws_filter'] ) && strpos( $_REQUEST['aws_filter'], 'cat_' ) !== false ) { $filter = str_replace( 'cat_', '', $_REQUEST['aws_filter'] ); $this->current_filter = $filter; $_REQUEST['aws_filter'] = $this->inherit_filter; } } function aws_front_current_filter( $filter_current ) { if ( $this->current_filter ) { $filter_current = 'cat_' . $this->current_filter; } return $filter_current; } function aws_front_data_parameters( $params ) { $params['data-init-filter'] = 'cat_' . $this->current_filter; return $params; } function aws_search_query_array( $query ) { global $wpdb; if ( $this->current_filter ) { $terms = $this->current_filter; $include_childs = " OR parent IN ({$terms}) OR parent IN ( SELECT term_id from {$wpdb->term_taxonomy} WHERE parent IN ({$terms}) )"; $string = "( id IN ( SELECT $wpdb->posts.ID FROM $wpdb->term_relationships JOIN $wpdb->posts ON ( $wpdb->term_relationships.object_id = $wpdb->posts.post_parent OR $wpdb->term_relationships.object_id = $wpdb->posts.ID ) WHERE $wpdb->term_relationships.term_taxonomy_id IN ( select term_taxonomy_id from $wpdb->term_taxonomy WHERE term_id IN ({$terms}) {$include_childs} ) ))"; $query['adv_filters'] .= ' AND ' . $string; } return $query; } } new AWS_Dynamic_Filters();
Customizable Parameters
$inherit_filter
- The filter ID whose settings should be inherited for newly created category filters. Make sure that the filter exists. Default = 1
.
This code will generate filters for all your top level categories. Choosing any category will lead to showing results only for this category and all its child categories.