Answers.Body: <p>Found the answer. When creating the where clauses for the query, there's a line that checks <code>empty($str)</code>, 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 <code>hideInEurope: 'false'</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-05T13:59:58.663Z
Answers.LastActivityDate: 2017-10-05T13:59:58.663Z
Answers.UserDisplayName: karinegomes
Body: <p>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.</p>
  
  <p>This is the block of code that creates the query params to filter the products.</p>
  
  <pre><code>&lt;!-- START QUERYPARAMS --&gt;
          &lt;!-- Initialize query paramaters array --&gt;
          {% set queryParams = {} %}
  
          &lt;!-- Add sale parameter to query paramaters array if searching for sale items --&gt;
          {% if craft.request.getSegment(2) == "sale" %}
            {% set queryParams = queryParams|merge({ hasSales: true }) %}
          {% endif %}
  
          &lt;!-- If on new arrivals page, add new arrivals to query parameters array --&gt;
          {% if craft.request.getSegment(2) == "new-arrivals" %}
            {% set queryParams = queryParams|merge({ newArrival: true }) %}
          {% endif %}
  
          &lt;!-- If order parameter passed in, merge it to the query parameters array --&gt;
          {% if craft.request.getParam('order') != "" and craft.request.getParam('order') != "default-sorting" %}
            {% set queryParams = queryParams|merge({ order: craft.request.getParam('order') }) %}
          {% endif %}
  
          &lt;!-- Initiating rate variable --&gt;
          {% set rate = 0 %}
  
          &lt;!-- Setting rate according to current currency --&gt;
          {% for currency in craft.commerce.paymentCurrencies %}
            {% if currency == cart.paymentCurrency %}
              {% set rate = currency.rate %}
            {% endif %}
          {% endfor %}
  
          &lt;!-- Set price variables to default values if none are passed in --&gt;
          {% 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 %}
  
          &lt;!-- Add price paramaters to queryParams array --&gt;
          {% set queryParams = queryParams|merge({
            defaultPrice: [
              'and',
            '&gt;= ' ~ lowPrice,
            '&lt;= ' ~ highPrice,
            ]
          })
          %}
  
          {% set queryParams = queryParams|merge({ hideInEurope: false }) %}
          &lt;!-- END QUERYPARAMS --&gt;
  
          &lt;!-- Perform query and store results in results array --&gt;
          {% paginate craft.commerce.products(queryParams).type(typesArray).search(queryString ~ (q is defined ? ' ~ ' ~ q : '')).limit(productLimit) as pageInfo, products %}
  </code></pre>
  
  <p>As you can see, we have:</p>
  
  <pre><code>{% set queryParams = queryParams|merge({ hideInEurope: false }) %}
  </code></pre>
  
  <p>But for some reason, the products that I set as TRUE are still showing on the list. On the other hand, if I set <code>hideInEurope: true</code>, 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?</p>
  
Tags: <custom-field><products>
Title: Filter products by custom boolean field
ThreadText: # Filter products by custom boolean field
  
  (Asked by: username_0 on 2017-10-04)
  
  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?
  
  **Tags:** custom-field, products
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-10-05. Score: 2 | Accepted answer
  
  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'`.
  
  
  ---
  
  