Forum

Thread tagged as: Question, Problem, Shop

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?

Tony Astley

Tony Astley 0 points

  • 3 years ago

I have achieved this modifying PHP method I found on Stack Overflow:

$pageSlug = $_SERVER["REQUEST_URI"];
if(substr($pageSlug, -1) == '/') {
$pageSlug = substr($pageSlug, 0, -1);
}
$pageSlug_array = explode('/',$pageSlug);
$brand = strtolower(end($pageSlug_array));

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:

'filter' => 'brand.slug',
'match' => 'eq',
'value' => perch_get('brand')

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

shop/[slug:brand]

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):

brands/[slug]
brands/[slug:brand]

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:

brands/[slug:brand]

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:

products/product-slug

Could we format it as:

[products:parent]/[slug:product]

This labels the first part as the 'parent', and the last part as the 'product' giving us an array that looks like:

Array
(
    [0] => /products/product-slug
    [parent] => products
    [1] => products
    [product] => product-slug
    [2] => product-slug
)

Can we then use the 'parent' and the 'product' by name in the perch_get.