Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where an empty taxonomy parameter would return no results #2672

Merged
merged 4 commits into from
Oct 20, 2020
Merged

Fix issue where an empty taxonomy parameter would return no results #2672

merged 4 commits into from
Oct 20, 2020

Conversation

duncanmcclean
Copy link
Member

This PR fixes #2293.

In v2, when you had an empty taxonomy param on the collection tag, all results would be displayed, just without taxonomy filtering.

However, in v3, if the taxonomy parameter was empty, no results would be returned.

@duncanmcclean duncanmcclean marked this pull request as ready for review October 17, 2020 09:28
@axelferdinand
Copy link

Hi @damcclean

I tried your fix, and it works perfectly, except that passing a pipe ('|') through the taxonomy parameter returns zero results, still (this wasn't a problem in v2, there you could pass pipes without affecting the filter) Is there a way to support pipes in the filter, and still return results?

See example URL from my v3 installation:
http://swisstime.proto/sveitsiske-klokker?brand=|&category=&functions=

image

Test my v2 installation here, where pipes in the filters doesn't affect the results:
https://swisstime.no/en/swiss-watches?brand=|anonimo&category=&functions=

@duncanmcclean
Copy link
Member Author

Thanks for trying out my PR.

Would you mind sharing the code you're using with the pipe that produces the issue?

@axelferdinand
Copy link

`{{ partial:header }}

    <script>
        app.controller('products', function($scope) {

            // Set active filters

            // Brand
            $scope.brand = {
                brand: [{{ taxonomy:brand }}{{ if get:brand | contains:slug }}'{{ slug }}'{{ if !last }},{{ /if }}{{ /if }}{{ /taxonomy:brand }}]
            };

            // Category
            $scope.category = {
                category: [{{ taxonomy:category }}{{ if get:category | contains:slug }}'{{ slug }}'{{ if !last }},{{ /if }}{{ /if }}{{ /taxonomy:category }}]
            };

            // Functions
            $scope.functions = {
                functions: [{{ taxonomy:functions }}{{ if get:functions | contains:slug }}'{{ slug }}'{{ if !last }},{{ /if }}{{ /if }}{{ /taxonomy:functions }}]
            };

            $scope.showFilter = false;

            $scope.toggleFilter = function() {
        		$scope.showFilter = !$scope.showFilter;
        	};

            // Notify submit button of change
            $scope.filterChange = function(){
                $scope.filterChanged = true;
            }
        });
    </script>

    <div class="products" data-ng-controller="products">

        <div class="products__filter" data-ng-class="{'products__filter--expanded': showFilter}">
            <div class="products__filter__content">

                <p class="label">{{ if site == "default" }}Merker{{ else }}Brands{{ /if }}</p>
                <div class="checkbox">
                    {{ taxonomy:brand sort="title:asc" }}
                    <div class="checkbox__item">
                        <label for="{{ slug }}">
                            <input class="checkbox__item__input" type="checkbox" name="{{ slug }}" id="{{ slug }}" value="{{ slug }}" data-checklist-model="brand.brand" data-checklist-change="filterChange()">
                            <a class="checkbox__item__helper checkbox__item__helper--disable-on-mobile" href="?brand={{ if get:brand | contains:slug }}{{ get:brand replace='{{slug}}|' }}{{ else }}{{ get:brand }}|{{ slug }}{{ /if }}&category={{ get:category }}&functions={{ get:functions }}">{{ title }}</a>
                        </label>
                    </div>
                    {{ /taxonomy:brand }}
                </div>

                <p class="label">{{ if site == "default" }}Kategorier{{ else }}Categories{{ /if }}</p>
                <div class="checkbox">
                    {{ taxonomy:category sort="title:asc" }}
                    <div class="checkbox__item">
                        <label for="{{ slug }}">
                            <input class="checkbox__item__input" type="checkbox" name="{{ slug }}" id="{{ slug }}" value="{{ slug }}" data-checklist-model="category.category" data-checklist-change="filterChange()">
                            <a class="checkbox__item__helper checkbox__item__helper--disable-on-mobile" href="?brand={{ get:brand }}&category={{ if get:category | contains:slug }}{{ get:category replace='{{slug}}|' }}{{ else }}{{ get:category }}|{{ slug }}{{ /if }}&functions={{ get:functions }}">{{ title }}</a>
                        </label>
                    </div>
                    {{ /taxonomy:category }}
                </div>

                <p class="label">{{ if site == "default" }}Funksjoner{{ else }}Functions{{ /if }}</p>
                <div class="checkbox">
                    {{ taxonomy:functions sort="title:asc" }}
                    <div class="checkbox__item">
                        <label for="{{ slug }}">
                            <input class="checkbox__item__input" type="checkbox" name="{{ slug }}" id="{{ slug }}" value="{{ slug }}" data-checklist-model="functions.functions" data-checklist-change="filterChange()">
                            <a class="checkbox__item__helper checkbox__item__helper--disable-on-mobile" href="?brand={{ get:brand }}&category={{ get:category }}&functions={{ if get:functions | contains:slug }}{{ get:functions replace='{{slug}}|' }}{{ else }}{{ get:functions }}|{{ slug }}{{ /if }}">{{ title }}</a>
                        </label>
                    </div>
                    {{ /taxonomy:functions }}
                </div>

                <div class="products__filter__content__update">

                    <a class="button" data-ng-class="{'button--solid': filterChanged}" data-ng-show="category.category != '' || brand.brand != '' || functions.functions != '' || filterChanged" data-ng-href="?category=[[category.category.toString() | replace:',':'|']]&brand=[[brand.brand.toString() | replace:',':'|']]&functions=[[functions.functions.toString() | replace:',':'|']]">
                        {{ if site == "default" }}Oppdater{{ else }}Update{{ /if }}
                    </a>

                </div>
            </div>

            <div class="products__filter__bar">

                <div class="products__filter__bar__info">
                    <div data-ng-show="showFilter">
                        <h4 class="o-no-margin">{{ if site == "default" }}Filter{{ else }}Filter{{ /if }}</h4>
                        <small>
                            {{ if site == "default" }}Velg kategorier, merker og funksjoner.{{ else }}Choose category, brand and functions.{{ /if }}
                        </small>
                    </div>
                    <div data-ng-hide="showFilter">
                        <span class="products__filter__bar__info__count">
                        {{ collection from="{ mount }" taxonomy:brand="{ get:brand }" taxonomy:category="{ get:category }" taxonomy:functions="{ get:functions }" taxonomy:movement="{ get:movement }" }}
                        {{ if last }}
                        {{ total_results }}
                        {{ /if }}
                        {{ /collection }}
                        {{ if site == "default" }}sveitsiske klokker{{ else }}Swiss Watches{{ /if }}
                        </span>
                    </div>
                </div>

                <div class="products__filter__bar__toggle">
                    <a class="button o-no-margin" data-ng-class="{'button--solid': filterChanged}" data-ng-show="showFilter" data-ng-href="?brand=[[brand.brand.toString() | replace:',':'|']]&category=[[category.category.toString() | replace:',':'|']]&functions=[[functions.functions.toString() | replace:',':'|']]">{{ if site == "default" }}Lagre{{ else }}Save{{ /if }}</a>
                    <button class="button o-no-margin" type="button" data-ng-click="toggleFilter();" data-ng-hide="showFilter">
                        Filter
                    </button>
                </div>

            </div>
        </div>
        <div class="products__results">

            <div class="products__results__header">
                {{ taxonomy:brand }}
                {{ if get:brand | contains:slug }}
                <div class="tag">{{ title }}<a class="tag__remove" href="?brand={{ get:brand replace='{{slug}}|' }}&category={{ get:category }}&functions={{ get:functions }}" title="Fjern {{ title }}"><span class="tag__remove__title">Fjern {{ title }}</span></a></div>
                {{ /if }}
                {{ /taxonomy:brand }}
                {{ taxonomy:category }}
                {{ if get:category | contains:slug }}
                <div class="tag">{{ title }}<a class="tag__remove" href="?brand={{ get:brand }}&category={{ get:category replace='{{slug}}|' }}&functions={{ get:functions }}" title="Fjern {{ title }}"><span class="tag__remove__title">Fjern {{ title }}</span></a></div>
                {{ /if }}
                {{ /taxonomy:category }}
                {{ taxonomy:functions }}
                {{ if get:functions | contains:slug }}
                <div class="tag">{{ title }}<a class="tag__remove" href="?brand={{ get:brand }}&category={{ get:category }}&functions={{ get:functions replace='{{slug}}|' }}" title="Fjern {{ title }}"><span class="tag__remove__title">Fjern {{ title }}</span></a></div>
                {{ /if }}
                {{ /taxonomy:functions }}

                <h1 class="products__results__header__count">
                    {{ collection from="{ mount }" taxonomy:brand="{ get:brand }" taxonomy:category="{ get:category }" taxonomy:functions="{ get:functions }" taxonomy:movement="{ get:movement }" }}
                    {{ if last }}
                    {{ total_results }} {{ if site == "default" }}sveitsiske klokker{{ else }}Swiss Watches{{ /if }}
                    {{ /if }}
                    {{ /collection }}
                </h1>
            </div>

            {{ collection from="{ mount }" limit="15" paginate="true" as="posts" taxonomy:brand="{ get:brand }" taxonomy:category="{ get:category }" taxonomy:functions="{ get:functions }" sort="title:asc" }}

            {{ if no_results }}
            <div class="products__results__message">
                <p class="lead">{{ if site == "default" }}Ingen produkter matcher ditt søk. Prøv å <a href="{{ current_url }}">nullstille søket</a>.{{ else }}No products match your search. Try to <a href="{{ current_url }}">reset the search</a>{{ /if }}</p>
            </div>
            {{ /if }}

            <div class="products__results__cards">
                <div class="card card--products-page">
                {{ posts }}
                {{ partial:product-item }}
                {{ /posts }}
                </div>
            </div>

            {{ paginate }}
            {{ if next_page or prev_page }}
            <div class="products__results__pagination">
                <nav class="pagination">
                    {{ if prev_page }}
                    <a class="pagination__item pagination__item--prev" href="{{ prev_page }}">← {{ if site == "default" }}Forrige{{ else }}Previous{{ /if }}</a>
                    {{ /if }}
                    <em class="pagination__item pagination__item--counter">
                        {{ if site == "default" }}Side{{ else }}Page{{ /if }} {{ current_page }} {{ if site == "default" }}av{{ else }}of{{ /if }} {{ total_pages }}
                    </em>
                    {{ if next_page }}
                    <a class="pagination__item pagination__item--next" href="{{ next_page }}">{{ if site == "default" }}Neste{{ else }}Next{{ /if }} →</a>
                    {{ /if }}
                </nav>
            </div>
            {{ /if }}
            {{ /paginate }}


            {{ /collection }}

        </div>
    </div>
</div>

{{ partial:brands }}
{{ partial:footer }}
`

@axelferdinand
Copy link

Here's my markup (above)

I'm using a technique where the user can toggle on and off filters, and toggling off a filter returns a pipe (didn't find a clever way to get rid of it)

Test it here:
https://swisstime.no/en/swiss-watches

@duncanmcclean
Copy link
Member Author

Thanks! I've changed the way I'm checking so it should no work for alone pipes.

@jasonvarga
Copy link
Member

Beautiful, thank you!

@jasonvarga jasonvarga merged commit bce678d into statamic:master Oct 20, 2020
@duncanmcclean duncanmcclean deleted the fix/2293 branch October 20, 2020 14:54
@axelferdinand
Copy link

Thank you so much @damcclean Now I'm ready to launch the v3 version of my online store!!!

@duncanmcclean
Copy link
Member Author

Thank you so much @damcclean Now I'm ready to launch the v3 version of my online store!!!

Glad I could help 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty taxonomy parameter causes zero results
3 participants