Forum

Thread tagged as: Question, Addons

Product filtering by category

Hello, For the Perch Shop App I have a category set called "Products" and a few categories and sub categories within.

I'd like to display all of the products within a particular category, but I don't think I'm filtering correctly and I've tried a ton of combinations.

Up to now I've been filtering on sku and that works fine:

<?php perch_shop_products([
                    'filter' => 'sku',
                    'match' => 'eq',
                    'value' => $walletSku,

                ]); ?>

So, (and pls forgive the simplistic question) one of my product categories is 'wallets'' ; how can I display the 3 products within that category?

Thanks very much!!

Olympia Kyriakides

Olympia Kyriakides 1 points

  • 5 years ago

Add

'category' => 'wallets'

To your filter

EDITED: sorry, I missed the "Set" products, "Category" wallets

'category' => 'products/wallets'

Thanks Robert! I was hoping to get rid of the sku filtering and just filter on category. I've been trying something like this to no result:

<?php perch_shop_products([
            'filter'=> 'catSlug',
            'match' => 'eq',
            'value' => 'wallets',           
]); ?>

Apologies if this is unrelated but perhaps something is off with my categories on the whole...?

I set the URL pattern to:

shop/[slug:cat]/[slug]

with links:

<a href="/shop/<perch:category id="catSlug" type="slug" />/<perch:shop id="slug" type="slug" />/">

but the links aren't pulling in the category and look like shop//[slug]

When I key the catSlug into the browser url the page appears without issue

0.0777  0.0011  Matched route: shop/[slug:cat]/[slug]
0.0779  0.0001  Using master page: /templates/pages/products.php
0.0779  0.0001  Page arguments:
Array
(
    [0] => /shop/wallets/signature-card-wallet
    [cat] => wallets
    [1] => wallets
    [2] => signature-card-wallet
)

Incidentally, this line in product.html displays the category slug just fine when I use perch_shop_product('my-product');

<perch:categories id="category" set="products" label="Category">
    <perch:category id="catSlug" />
</perch:categories>
Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_shop_products([
            'category' => 'products/wallets',           
]);

Thank you!! Should we give Robert the mark for solution?! :)

The filtering works now. I guess I could use a little assistance with the routes.

I'd like to have a main shop page (/shop) that displays all products organized by category (got that!) When I select a product I want to be taken to /shop/[category]/[slug]

I can't get the template to pick up the category. I'm assuming this set up or similar should work..?

shop/[slug:cat]/[slug]

<a href="/shop/<perch:category id="catSlug" type="slug" />/<perch:shop id="slug" type="slug" />/">

At one point it was pointing to 'products' as the category, but I couldn't get to a lower lever.

UPDATED:

<perch:categories id="category" set="products" label="Category">
    <a href="/shop/<perch:category id="catSlug" type="slug" />/<perch:shop id="slug" type="slug" />/">
</perch:categories>

You have to get <perch:category> while inside <perch:categories> ... but I am a bit unsure if this works...

I tried the above and also the following, but they didn't work.

I'm still getting /shop//[slug]

<a href="/shop/<perch:category id="catSlug" type="slug" />/<perch:shop id="slug" type="slug" />/">

        <perch:categories id="category" set="products" label="Category"></perch:categories>
        <perch:shop id="title" type="text" />

</a>

Could you please post your product.html template

Drew McLellan

Drew McLellan 2638 points
Perch Support

Try:

<a href="/shop/<perch:categories id="category" set="products"><perch:category id="catSlug" type="slug" /></perch:categories><perch:shop id="slug" type="slug" />/">
        <perch:shop id="title" type="text" />
</a>

man, Drew nails it again...

I am guessing <perch:shop> cant be inside <perch:categories>??

Yay, success! Thanks to you both.

It was missing a slash though, so:

<a href="/shop/<perch:categories id="category" set="products"><perch:category id="catSlug" type="slug" />/</perch:categories><perch:shop id="slug" type="slug" />/">

<perch:shop id="title" type="text" />
</a>

Drew McLellan

Drew McLellan 2638 points
Perch Support

I was in two minds - looks like I chose wrong! I was thinking of the catPath, which would end in a slash. :)

Ah-ha, I think being reminded of that could prove helpful in the future though!!

..also that was a good guess, I feel like the queen of catPath!