Forum
Cannot get brand filtering working
I'm trying to display a list of products filtered based on brand.
The brand will be pulled from the url slug, however nothing I ask perch_get to retrieve returns any value.
For the purpose of the question the brand in question here is 'apple'
The brand page will show a list of products using the following perch_shop_products function... but only if I type the brand name in manually.
perch_shop_products([
'template' => 'products/product_list.html',
'variants' => true,
'filter' => 'brand.slug',
'match' => 'eq',
'value' => 'apple'
]);
This shows me all apple products correctly.
For the template to be useful I need the to set the brand based on the page slug and dynamically insert it into the value parameter.
I've tried:
$brandSlug = perch_get('slug');
$brandSlug = perch_get('url_1');
$brandSlug = perch_get('brand');
None of the above return any value. Any ideas?
I have achieved this modifying PHP method I found on Stack Overflow:
This returns the last part of the url, without the trailing slash.
Am I missing an obvious Perch way of achieving the same?
Hi Tony
Just use the perch_get as the value:
Didn't work. It was one of the first things I tried.
It's likely down to the fact I'm using SEO friendly urls and perch routes.
This works fine for me and I am using routes What’s your URL pattern for the brand page? I am using
If you turn on debug for the page you will find the brand slug ID you need to match on the query.
Hello Jonathan,
Thanks for the steer, I have it working the Perch way now - my error was in the way I had the routes set up.
I had the following routes in this order (I don't use the '/shop/' url scheme):
The first route was returning an array numbered [0][1], by deleting the first route, the correctly formatted second route took over and provide an array with the [brand] value I needed for the perch_get part of the filter.
So it works with:
Thanks Jonathan this will help me a great deal in the upcoming templates, as I now understand how to name the array returned by the route.
Just to check - If we wanted to label a route match the following structure:
Could we format it as:
This labels the first part as the 'parent', and the last part as the 'product' giving us an array that looks like:
Can we then use the 'parent' and the 'product' by name in the perch_get.