Forum
Filter products by category
Hi All im trying to filter products by category but cant get it working
i have this
// Create an empty array ready to add our filters to
$filters = array();
$min = filter_var(perch_get('min_price'), FILTER_SANITIZE_NUMBER_INT);
if (perch_get('type')) {
// if 'type' is on the URL, add a filter for bedrooms
$filters[] = array(
'filter' => 'type',
'match' => 'eq',
'value' => perch_get('type'),
);
}
if (perch_get('category')) {
// if 'beds' is on the URL, add a filter for bedrooms
$filters[] = array(
'filter' => 'category',
'match' => 'eq',
'category' => perch_get('category'),
);
}
if (perch_get('min_price')) {
// if 'location' is on the URL, add a filter for the location
$filters[] = array(
'filter' => 'price',
'match' => 'eqbetween',
'value' => $min . ', ' . perch_get('max_price'),
);
}
// Unset the filters if none are used:
if (!count($filters)) $filters=false;
// Then get the list
perch_shop_products([
'template' => 'products/list_homepage.html',
'sort' => 'price',
'sort-order' => 'DESC',
'filter' => $filters,
]);
<section id="product_filter">
<perch:form id="filter" method="get">
<div class="row">
<div class="large-12 columns">
<section class="rw-wrapper">
<div class="rw-sentence">
<span>Choose your next </span>
<div class="rw-words rw-words-1">
<span>Adventure</span>
<span>Activity</span>
<span>Day Out</span>
<span>Venture</span>
<span>Experience</span>
<span>Trip</span>
</div>
</div>
</section>
</div>
<div class="large-3 columns">
<perch:input type="select" id="category" placeholder="Category" options=",Kayaking|kayaking,Sailing|bushcraft,DofE|dofe,Archery|archery" />
</div>
<div class="large-3 columns">
<perch:input type="number" id="min_price" placeholder="Min Price"/>
</div>
<div class="large-3 columns">
<perch:input type="number" id="max_price" placeholder="Max Price" />
</div>
<div class="large-3 columns">
<perch:input type="submit" value="Filter" class="button"/>
</div>
</div>
</perch:form>
</section>
The
category
option is not part of the filter set:https://docs.grabaperch.com/docs/categories/filtering/
so the category needs to be added like products/archery
soo
No,
category
isn't a filter.Ahh i see
did filters also get fixed
i have managed to filter by min and max price at the same time just not min by its self
i now have this code
i can filter the category now so thats working
when i filter by min and max it works
when i filter using min or max it does not work
update
max_price works by its self but min price does not
Can you please post your diagnostics report?
sure
Debug for just filtering by min price
What are the options you're using to create that? I can see lots of blanks, e.g.
i just added 150 into the min price filter but left max price empty
i need the option to add a min price and/or max price
Please show me a test case with the options you're using. If I guess that won't necessarily reflect what you're doing and so might not reproduce the same issue.
Left of this page is the filter
https://harbourchallenge.myboxproject.co.uk/shop
Last time of asking - please show me the options you're using.
I just have this
The logic here is flawed - you have no defaults if one of the values isn't set.
I'd suggest adding some defaults.