Skip to content

filter_options

The filter_subcategories_slider macro is used to render filter subcategories slider.

Definition

{% filter_subcategories_slider(filters, options) %}

Input parameters

filters

Filters object representing a set of filters we want to render.

options

object represents an object of filter options attributes

Option key Type Default Required Description
options.shouldHideEmptySubcategories number - no If set to 1 filter subcategories with no items will be hidden
options.showSubcategoryCounters number - no If set to 1 a number of products from subcategories meeting the filter's criteria will be displayed
options.showCounters number - no If set to 1 a number of products from general categories meeting the filter's criteria will be displayed
options.title object null no Object of keys representing languages in ISO format and values representing titles for those languages
options.instanceId number - yes Unique identifier commonly provided by a special module variable moduleInstance

Example

To get filters, we use methods from Object Api - getFilters(). If using this macro inside a module, we can pass a moduleInstance as an instanceId parameter, otherwise you can provide and unique identifier.

{% from "@macros/filter_options.twig" import filter_options %}

{% set filters = ObjectApi.getFilters() %}

{{ 
    filter_subcategories_slider(filters, moduleConfig|merge({
        instanceId: moduleInstance,
    })) 
}}

Macro source code

{% macro filter_subcategories_slider(filters, options) %}
    {% from "@macros/slider.twig" import slider %}
    {% from "@macros/icon.twig" import icon %}

    {%
        set settings = {
            "mountOnConnectedCallback": true,
            "autoWidth:": true,
            "perPage": 1,
            "perMove": 1,
            "arrows": false,
            "pagination": false,
            "hasArrowsOnMobile": false,
            "gap": "0.5rem"
        }
    %}

    {% if filters.categoriesFilter  %}
        {% set categories = filters.categoriesFilter.values|filter(category => category.name != filters.categoriesFilter.current.name) %}

        {% if options.shouldHideEmptySubcategories %}
            {% set categories = categories|filter(category => category.counter != 0) %}
        {% endif %}

        <h-slider
                id="slider-{{ options.instanceId }}"
                class="slider splide"
                settings="{{ settings | json_encode }}"
        >
            <div class="splide__track">
                <ul class="splide__list filter-subcategories__slider">
                    {% if filters.categoriesFilter.parent %}
                        <li class="splide__slide">
                            <a class="btn btn_secondary btn_xs" href="{{ filters.categoriesFilter.parent.urlWithFilters }}">
                                {{
                                    icon('icon-chevron-left', {
                                        size: 'm',
                                        classNames: ['btn__icon', 'btn__icon_left']
                                    })
                                }}

                                {{ filters.categoriesFilter.parent.name }}
                            </a>
                        </li>
                    {% endif %}

                    {% for category in categories %}
                        <li class="splide__slide">
                            <a class="btn btn_outline btn_xs" href="{{ category.urlWithFilters }}">
                                {{ category.name }}

                                {% if filters.categoriesFilter.hasCounters and category.counter and options.showSubcategoryCounters %}
                                    <span class="filter-options__category-link-counter ml-xs-1">{{ category.counter }}</span>
                                {% endif %}
                            </a>
                        </li>
                    {% endfor %}
                </ul>
            </div>
        </h-slider>
    {% endif %}
{% endmacro %}

Object Api methods reference

Object Api objects reference