Quote from frafish on April 1, 2025, 13:17
In some situation could be great if also Slider view can limit values only to possible values based on products in current Category.
Other fields types already do it, but actually Sliders show ALL terms of the selected taxonomy...
here my solution to limit values and automatically hide the filter if products of this category are not related to these terms.
if (!is_admin()) {
add_filter('woof_sort_terms_before_out', function($terms, $type) {
if($type == 'slider') {
// get current term ID
$term_id = 0;
$term = get_queried_object();
if ($term && get_class($term) == 'WP_Term') {
$term_id = get_queried_object_id();
$term_slug = $term->slug;
$taxonomy = $term->taxonomy;
} else {
if (!empty($_GET['really_curr_tax'])) {
$tmp = explode('-', $_GET['really_curr_tax']);
$term_id = intval(reset($tmp));
$taxonomy = end($tmp);
$term = get_term_by('id', $term_id, $taxonomy);
$term_slug = $term->slug;
}
}
if ($term_id) {
$value = reset($terms);
$field = $value['taxonomy'];
$args = array(
'category' => array( $term_slug ),
'limit' => -1,
'type' => ['simple','variable','variation']
);
$products = wc_get_products( $args );
$values = [];
foreach ($products as $product) {
$tmp = get_the_terms($product->get_id(), $field);
if ($tmp) {
foreach ($tmp as $vtmp) {
$values[$vtmp->term_id] = $vtmp;
}
}
}
if (empty($values)) {
// remove filter
return $values;
} else {
// limit filter values for this category
foreach ($terms as $key => $term) {
if (empty($values[$term['term_id']])) {
unset($terms[$key]);
}
}
}
}
}
return $terms;
}, 11, 2);
}
Could be great if you integrate this code and add an extra option for each filter to limit values.
Thanks
In some situation could be great if also Slider view can limit values only to possible values based on products in current Category.
Other fields types already do it, but actually Sliders show ALL terms of the selected taxonomy...
here my solution to limit values and automatically hide the filter if products of this category are not related to these terms.
if (!is_admin()) {
add_filter('woof_sort_terms_before_out', function($terms, $type) {
if($type == 'slider') {
// get current term ID
$term_id = 0;
$term = get_queried_object();
if ($term && get_class($term) == 'WP_Term') {
$term_id = get_queried_object_id();
$term_slug = $term->slug;
$taxonomy = $term->taxonomy;
} else {
if (!empty($_GET['really_curr_tax'])) {
$tmp = explode('-', $_GET['really_curr_tax']);
$term_id = intval(reset($tmp));
$taxonomy = end($tmp);
$term = get_term_by('id', $term_id, $taxonomy);
$term_slug = $term->slug;
}
}
if ($term_id) {
$value = reset($terms);
$field = $value['taxonomy'];
$args = array(
'category' => array( $term_slug ),
'limit' => -1,
'type' => ['simple','variable','variation']
);
$products = wc_get_products( $args );
$values = [];
foreach ($products as $product) {
$tmp = get_the_terms($product->get_id(), $field);
if ($tmp) {
foreach ($tmp as $vtmp) {
$values[$vtmp->term_id] = $vtmp;
}
}
}
if (empty($values)) {
// remove filter
return $values;
} else {
// limit filter values for this category
foreach ($terms as $key => $term) {
if (empty($values[$term['term_id']])) {
unset($terms[$key]);
}
}
}
}
}
return $terms;
}, 11, 2);
}
Could be great if you integrate this code and add an extra option for each filter to limit values.
Thanks