Reply 331007 to: Woocommerce Custom Taxonomy Filter Widget Shop Page not updating

Avatar: Richard
Richard
Participant
August 3, 2022 at 21:16

Apologies for re-answering, but I wanted to save anyone the time, as we have progressed. Though parts of the initial question are still unknown to us, whereas we fixed others.

First of all. We got the custom taxonomy filter working to filter the products on the shop page by making use of:


add_action('init', function () {
    add_filter('posts_clauses', function ($clauses, $query) {
        global $wpdb;

        if (isset($_GET['filter_diet']) && !empty($_GET['filter_diet'])) {

            $brands = explode(',', $_GET['filter_diet']);
            $ids    = array();

            foreach ($brands as $key => $value) {
                $term = get_term_by('slug', $value, 'diet');
                if (!isset($term->term_taxonomy_id) || empty($term->term_taxonomy_id)) // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
                {
                } else {
                    $ids[] = $term->term_taxonomy_id;
                }
            }

            if (!implode(',', $ids)) {
                $ids = 0;
            } else {
                $ids = implode(',', $ids);
            }

            $clauses['where'] .= " AND {$wpdb->posts}.ID IN ( SELECT {$wpdb->term_relationships}.object_id  FROM {$wpdb->term_relationships} WHERE term_taxonomy_id  IN (" . $ids . ") )";
        }
        // print_r($clauses);
        return $clauses;
    }, 21, 2);
}, 20, 2);

The second thing is, that the brands-filter does not respect any other filter options, just like the status filter. Both are using hardcoded GET param checks in the theme.

The only way to get around this is if you were to change the param behaviour to something like the $_SERVER['QUERY_STRING']; or similar.
As for now, we can support in our own custom taxonomy widget your xtheme brands & status filter by simply adjusting the widget to something along the line:


...
$current_filter = isset($_GET['filter_diet']) ? explode(',', wc_clean(wp_unslash($_GET['filter_diet']))) : array();
                $brand_filter = isset($_GET['filter_brand']) ? explode(',', wc_clean(wp_unslash($_GET['filter_brand']))) : array();
                $current_filter = array_map('sanitize_title', $current_filter);

                $all_filters = $current_filter;

                if (!in_array($diet->slug, $current_filter, true)) {
                    $all_filters[] = $diet->slug;
                } else {
                    $key = array_search($diet->slug, $all_filters);
                    unset($all_filters[$key]);
                    $class .= ' current-item';
                }

                if (!empty($all_filters)) {
                    $link = add_query_arg('filter_diet', implode(',', $all_filters), $link);
                }
                if (!empty($brand_filter)) {
                    $link = add_query_arg('filter_brand', implode(',', $brand_filter), $link);
                }
...

That kind of solves the parameter issue.

However, it is still unclear how the JS loading works in this case, as the widget link – even though it is created exactly like the brand link – refreshes the page, rather than using the overlay.

I guess, once we are done we will hand in a feature request, to allow customers to add custom taxonomy & filtering widgets via an “easier” to use API, based on some hooks etc. You could set a rule like “filter query has to start with filter_$taxonomy”. From what I see it would not be too much work to make the brand and the product status filters “non-destructive”.

Go To The Whole Conversation In Topic
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.