2

I'm trying to create a product search page, where the categories are listed on the sidebar with checkboxes. I'd like to show how many products are in each category.

  • Checking one of the categories would filter your results to show products in that category.
  • Once the results are filtered, the entry counts on the categories should be updated. (Number of entries that match the current search and is also in the respective category)

I've created the search using Low Search, and can show the entry count using GWcode Categories. But I don't know how I would show the 'filtered' count. Is something like this even possible?

3 Answers 3

3

This is a tricky issue. Low Search sits 'on top of' the native Channel Entries method. It optionally executes some non-native filters (like keywords, ranges, search:title, etc) and feeds that to the channel:entries method, which takes care of any native filtering (category, status, search:channel_field, etc).

That means that before calling channel:entries, LS doesn't know how many entries are actually going to be displayed. If you don't know which entries are going to be displayed, you also don't know which categories apply to them. Pagination throws another spanner in the works, as you could determine the applicable categories but only if the results were not paginated.

In this scenario, it seems impossible to get a filtered count.

The way to get to them, would be to have a list of entry_ids available from the channel:entries tag (and thus the low_search:results tag) that make up the entire list of results, regardless of pagination. With that list, it would be possible to have a category count. But I'm pretty sure that info isn't available through EE's hooks at the moment.

1
  • This was only a client's "nice to have" feature request. Seems like way more effort than its worth. Thanks for the helpful response. Feb 8, 2013 at 17:02
1

Is something like this even possible?

I tried relying on categories once for a really complex product hierarchy. I ran out of segment variables (there's a max of 9) for the full tree, and my listing page ended up being a ton of PHP & SQL for each level.

I just took a look back at that, and I did not show filtered counts. In each tier of categories, I highlighted the selected category, left the unfiltered counts on each category name, and had an "Undo" link at the bottom of a filtered category list that would reset/undo the filter. That way, from a UX perspective, it was obvious that you were in a filtered view within the broader view, and could easily back out.

0

Just in case anyone else has this problem I solved it with the query tag. This code is pretty specific to my situation, but if you look through it you should be able to swap in your own values. I use also use the low:seg2cat plugin, and this code is within the {exp:low_search:form} tag. I use segment 2 to indicate a particular brand category, and then this code returns only non-empty categories and entry counts that are also tagged with that brand.

My search form posts through the brand category as low_search_category:group1 and the 'type(s)' as low_search_category:group2:

  {exp:query sql="SELECT
      type.cat_id,
      type.cat_name,
      type.cat_url_title,
      t.title,
      COUNT(*) AS entries
    FROM
      exp_categories type JOIN
      exp_category_posts p ON type.cat_id = p.cat_id JOIN
      exp_channel_titles t ON p.entry_id = t.entry_id JOIN
      exp_channel_data d ON t.entry_id = d.entry_id JOIN
      exp_category_posts p2 ON p.entry_id = p2.entry_id AND p2.cat_id = '{segment_2_category_id}'
    WHERE
      type.group_id = 9 AND
      t.channel_id = 2
    GROUP BY
      type.cat_id
    ORDER BY
        type.cat_order;"}
        <label>
          <input type="checkbox" name="category:group2[]" value="{cat_id}"
          {if cat_id IN ({low_search_category:group2})} checked="checked"{/if} />
          {cat_name} [{entries}]
        </label>
  {/exp:query}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.