Been scratching my head for the past day on how to implement search with multiple categories.
At the moment, I have product categories as a select dropdown which only allows to search in one category at a time. This is passing the value as a parameter. How do I go about searching with multiple categories (checkboxes) at once? Here's what I have so far.
Any ideas?
search.html
<form action="{{ url('food-suppliers/results') }}">
<input type="search" name="query" placeholder="What are you looking for?" style="width: 100%;">
<select name="supplier">
<option value="">All Markets</option>
{% set suppliers = craft.categories.group('suppliers') %}
{% for supplier in suppliers %}
<option value="{{ supplier.slug }}">{{ supplier.title }}</option>
{% endfor %}
</select>
<select name="product">
<option value="">All Products</option>
{% set products = craft.categories.group('product') %}
{% for product in products %}
<option value="{{ product.slug }}">{{ product.title }}</option>
{% endfor %}
</select>
<select name="delivery">
<option value="">All Delivery</option>
{% set deliveries = craft.categories.group('delivery') %}
{% for delivery in deliveries %}
<option value="{{ delivery.slug }}">{{ delivery.title }}</option>
{% endfor %}
</select>
<div class="">
<input type="checkbox" name="homeGrown" value="true">Home Grown<br/>
<input type="checkbox" name="preparedProduce" value="true">Prepared Produce<br/>
<input type="checkbox" name="redTractor" value="true">Red Tractor<br/>
</div>
<div class="">
<input type="submit" value="Search">
</div>
</form>
results.html
{% set query = craft.request.getParam('q') %}
{% set supplier = craft.request.getParam('supplier') %}
{% set product = craft.request.getParam('product') %}
{% set delivery = craft.request.getParam('delivery') %}
{% set homeGrownParam = craft.request.getParam('homeGrown') %}
{% set preparedProduceParam = craft.request.getParam('preparedProduce') %}
{% set redTractorParam = craft.request.getParam('redTractor') %}
{% set supplierCat = craft.categories.group('supplier').slug(supplier).first %}
{% set productCat = craft.categories.group('product').slug(product).first %}
{% set deliveryCat = craft.categories.group('delivery').slug(delivery).first %}
{% set supplierParam = supplier ? {targetElement: supplierCat} : '' %}
{% set productParam = product ? {targetElement: productCat} : '' %}
{% set deliveryParam = delivery ? {targetElement: deliveryCat} : '' %}
{% set entries = craft.entries.section('suppliers').relatedTo(productParam).relatedTo(deliveryParam).search(query).homeGrown(homeGrownParam).preparedProduce(preparedProduceParam).redTractor(redTractorParam) %}