pdaleramirez / super-filter

Other
6 stars 2 forks source link

How to associate certain setup with category #2

Closed nitech closed 4 years ago

nitech commented 4 years ago

Is it possible to associate a certain setup with a category? In my case, I have a productFamily category. Every product is associated with at least one productFamily category.

I create a nav from the productFamily items, and allow the user to click one of the nav items to pre-filter which products should be displayed.

image

It would be nice to be able to associate a filter setup with a certain category (perhaps by exposing super filter setups as a field type?)

pdaleramirez commented 4 years ago

There is a pre-filter feature by adding a second parameter on the setup twig function. Json format with field handle and the values to be filtered. Checkout customization readme for an example implementation

nitech commented 4 years ago

Thanks @pdaleramirez. I know of the pre-filter, and I'm using it. What I'm really asking is how I can somehow associate a certain Super Filter setup with a category. And I am suggesting that you might expose Super Filter setups as possible values for a dropdown-field.

Or am I overlooking some obvious way to solve this?

pdaleramirez commented 4 years ago

What about passing url params to twig setup like this: So for example, if you have a route url myshop.com/products/steamkjeler your twig set up would be

 {% set categorySlug = craft.request.getSegment(2)  %}

{% set currentCategory = craft.categories.where({'slug': categorySlug }).one() %}

{{ craft.superFilter.setup('siteProducts', {
      productFamilyField: [currentCategory.id]
}) }}

That way you get different pre-filter for every page category

nitech commented 4 years ago

Thanks (and sorry for a late reply).

I solved this in the following manner:

  1. Create a custom field (dropdown) on the category.

image

The values of the dropdown represent each super filter and are manually inserted:

image

image

  1. Pick up the selected productFilter from the current category or default to the default filter if no category selected:
{% if category is defined %}
    {# Specific product category #}
    {# Get super filter type from Product Family Category's productFilter field - or get default value #}
    {% set superFilterSetup = category.productFilter.value|default('general') %}
    {{ 
        craft.superFilter.setup(superFilterSetup, 
        {
            filter: {
                productFamily: category.id
            }
        }) 
    }}                
{% else %}
    {# Start page of products - create generic super filter for all products and replace 'kompressorer' #}
    {{ craft.superFilter.setup('general') }} 
{% endif %}