0

I have implemented a product dimensions filtering system, which appears to "mostly" work.

I pass a min & max set of dimensions for the width and/or height of the products via query string. Then, using a custom pre_get_posts action, I detect if the width and/or height variables are set in the URL, and apply the necessary meta_query filter to the main query. It appears to be successfully filtering out the products that don't fit within the specified filter ranges. However, I have encountered a problem when there are no results that are returned by the filtered query (when there are no products in the size range).

I have some template customizations set in my woocommerce > loop > loop-start.php file that includes a customized filtering sidebar system. When no filtered results are returned, the loop appears to not be triggered at all, nor is the woocommerce > loop > no-products-found.php template.

This is a definite issue, as I can't seem to deliver the necessary "No products found" message.

Here's my code for my pre_get_posts filtered query:

add_action('pre_get_posts', 'g2_filter_products');

function g2_filter_products($q) {
    if (is_admin() || !is_archive()) {
        return;
    }

    if ((isset($q->query['post_type']) && ($q->query['post_type'] === 'product'))) {
        $filt_count = 0;
        $meta_query = $q->get('meta_query');

        if (isset($_GET['width-min']) && isset($_GET['width-max'])) {
            // Sanitize input
            $width_min = filter_input(INPUT_GET, 'width-min', FILTER_SANITIZE_SPECIAL_CHARS);
            $width_max = filter_input(INPUT_GET, 'width-max', FILTER_SANITIZE_SPECIAL_CHARS);           

            $meta_query[] = array(
                'key' => '_width',
                'value' => array($width_min, $width_max),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            );

            $filt_count++;
        }

        // Check if height query variables are present
        if (isset($_GET['height-min']) && isset($_GET['height-max'])) {
            // Sanitize input
            $height_min = filter_input(INPUT_GET, 'height-min', FILTER_SANITIZE_SPECIAL_CHARS);
            $height_max = filter_input(INPUT_GET, 'height-max', FILTER_SANITIZE_SPECIAL_CHARS);         

            $meta_query[] = array(
                'key' => '_height',
                'value' => array($height_min, $height_max),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            );
            $filt_count++;
        }

        if ($filt_count >= 2) {
            $meta_rel = array('relation' => 'AND');
            $meta_query = $meta_rel + $meta_query;
        }

        $q->set('meta_query', $meta_query);
    }
}

My end goal is to be able to filter my products based on the size range(s) specified in the Shipping Dimensions within each product entry, and if no results are found, the query simply triggers the no-products-found.php template.

Can anyone shed some light on why I can't get any returned value from my product queries when there's no filtered results (i.e. a "no rows found" result)? Or, if anyone has any ideas for ways around this issue, I'm all ears.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.