1

I'm working on a online store website and we need a filter to hide some products based on the country. I created a lightswitch field called "Hide in Europe" (default false) and added it to the categories, and set it to true on some products to test.

This is the block of code that creates the query params to filter the products.

<!-- START QUERYPARAMS -->
        <!-- Initialize query paramaters array -->
        {% set queryParams = {} %}

        <!-- Add sale parameter to query paramaters array if searching for sale items -->
        {% if craft.request.getSegment(2) == "sale" %}
          {% set queryParams = queryParams|merge({ hasSales: true }) %}
        {% endif %}

        <!-- If on new arrivals page, add new arrivals to query parameters array -->
        {% if craft.request.getSegment(2) == "new-arrivals" %}
          {% set queryParams = queryParams|merge({ newArrival: true }) %}
        {% endif %}

        <!-- If order parameter passed in, merge it to the query parameters array -->
        {% if craft.request.getParam('order') != "" and craft.request.getParam('order') != "default-sorting" %}
          {% set queryParams = queryParams|merge({ order: craft.request.getParam('order') }) %}
        {% endif %}

        <!-- Initiating rate variable -->
        {% set rate = 0 %}

        <!-- Setting rate according to current currency -->
        {% for currency in craft.commerce.paymentCurrencies %}
          {% if currency == cart.paymentCurrency %}
            {% set rate = currency.rate %}
          {% endif %}
        {% endfor %}

        <!-- Set price variables to default values if none are passed in -->
        {% if (craft.request.getParam('lowPrice') != "" and craft.request.getParam('highPrice')) != "" %}
          {% set lowPrice = (craft.request.getParam('lowPrice')) / rate %}
          {% set highPrice = (craft.request.getParam('highPrice')) / rate %}
        {% else %}
          {% set lowPrice = 0 %}
          {% set highPrice = 1500 * rate %}
        {% endif %}

        <!-- Add price paramaters to queryParams array -->
        {% set queryParams = queryParams|merge({
          defaultPrice: [
            'and',
          '>= ' ~ lowPrice,
          '<= ' ~ highPrice,
          ]
        })
        %}

        {% set queryParams = queryParams|merge({ hideInEurope: false }) %}
        <!-- END QUERYPARAMS -->

        <!-- Perform query and store results in results array -->
        {% paginate craft.commerce.products(queryParams).type(typesArray).search(queryString ~ (q is defined ? ' ~ ' ~ q : '')).limit(productLimit) as pageInfo, products %}

As you can see, we have:

{% set queryParams = queryParams|merge({ hideInEurope: false }) %}

But for some reason, the products that I set as TRUE are still showing on the list. On the other hand, if I set hideInEurope: true, only the products that I set the field as true show, so it seems to be working. Looks like when I set the param to false, it gets "ignored". Am I missing something?

2

Found the answer. When creating the where clauses for the query, there's a line that checks empty($str), so passing a false boolean value, PHP interprets it as an empty string, so the where clause doesn't get created. So I fixed it by changing the query param to hideInEurope: 'false'.

Your Answer

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

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