Woocommerce Custom Taxonomy Filter Widget Shop Page not updating

This topic has 5 replies, 3 voices, and was last updated 1 years, 8 months ago ago by Rose Tyler

  • Avatar: Richard
    Richard
    Participant
    August 3, 2022 at 02:43

    Hello,

    we would like to add some custom taxonomies to the Woocommerce products. We have added one called “diet” – which works fine.

    However we want to enable product filtering in the shop for it.
    We used the “Filter by brands”-widget code (et-core-plugin/app/models/widgets/brands-filter.php) as a template, to have the same styling as the rest of the theme.

    The widget is shown correctly, however when we select a “diet” it overwrites the brand filter and completely removes it. If we choose a brand it overwrites the diet filter. Furthermore, the shop page does not respect the selection of the diet filter.

    How can we extend the theme to filter products with our custom taxonomies?

    4 Answers
    Avatar: Awais Ahmed
    Awais Ahmed
    Participant
    August 3, 2022 at 05:40

    Hello, @Richard,

    Can you please provide a screenshot of your issue or if possible then please make a video of your issue and share it with us for a better reference to understand your issue so that I will provide you with a proper solution to solve your issue.

    Note: Please share your site WP-ADMIN Login details in the below-listed format in the private content area so that we’ll check and assist you accordingly.

    WP Admin URL:
    WP Admin Username:
    WP Admin Password:

    Kindly take a complete backup of your site first.

    And I also need your permission to debug your site, which includes deactivating all the third-party plugins and on-site Customization as well.

    Regards 8Themes Team.

    Avatar: Richard
    Richard
    Participant
    August 3, 2022 at 11:56

    Thank you for your message.

    I have put a video in the private content area, as well as the source code.

    Please note, this is currently only on a local dev environment.

    We copied the ‘et-core-plugin/app/models/widgets/brands-filter.php’ to create the widget.

    We replaced all brand references with our diet taxonomy.

    As you can see on the video the behaviour is a little bit different to the one of the brand selection. When a brand is selected, the page is not actively refreshed, instead the JS loader popups and refreshes the content.
    However, on the diet filter the page gets actually refreshed and as it seems the products are not filtered by the filter.

    Please contact administrator
    for this information.
    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”.

    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    August 5, 2022 at 15:49

    Hello,

    Sorry, but your request is outside our basic support. If you want to customize something and need help with that you may submit a customization request here.
    Our support is able to help only with issues that are related strictly to our theme, not with your additional customization.
    We hope for your understanding.

    Regards

  • Viewing 5 results - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.