Forum

Thread tagged as: Question, Suggestions, Shop

Dynamically displaying categories > products in Perch Shop

I would like to be able to display Shop/Product Categories on a page that are dynamically updated if my client adds more categories/products.

I know that: perch_shop_products(); displays a list of all products

And I know I can add the property e.g. 'category' => 'my-category' and that will then display a list of all the products within that category. But this isn't dynamic and if for example I currently have two categories that I'd like to display separately on a page then I can do it by having:

<?php perch_shop_products([
'category' => 'my-category-1'
]); ?>

<?php perch_shop_products([
'category' => 'my-category-2'
]); ?>

But what if my client adds another category to the back-end with some new products? Using the method above these products wouldn't display unless I hard-coded another perch_shop_products.

So my question: Is there a way that I can achieve what I'm after where categories/products can get added to a page automatically when generated by the client?

Richy Thomas

Richy Thomas 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

It sounds like you're asking if you can get a list of categories. Yes, you can do that with perch_categories()

You can then loop through the results and call perch_shop_products() however needed.

Thanks for the reply Drew,

So if I use the perch_categories() tag then it will display a list of all of the categories and products?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd get the categories with perch_categories() and you can then use those to query the products.

P K

P K 0 points

I was curious to see if I could achieve this, as I'm doing similar things on my site and also need more practice with nesting functions. The OP might already be sorted, but thought I'd contribute this and hope it helps someone.

Here's what I came up with, it works fine but I'm open to being advised of a better method, or any tips to fine tune what I have here.

perch_categories([
    'set' => 'products',
    'skip-template' => 'true',
    'each'  =>  function($item) {
    PerchSystem::set_var('catTitle-t', $item['catTitle']);
    PerchSystem::set_var('catPath-t', $item['catPath']);
        perch_shop_products([
            'category' => $item['catPath'],
            'template' => 'products_in_all_categories.html'
        ]);
    },
]);

and in the template we now have category title and path exposed....

<perch:before>
    <h2>Category Title: <perch:shop id="catTitle-t" /></h2>
    <h3>Category Path: <perch:shop id="catPath-t" /></h3>
<ul>
</perch:before>
    <li>Product Title: <perch:shop id="title" /></li>
<perch:after>
</ul>
</perch:after>

Hey PK. Thanks for your input - I've only just got around to implementing this.

So In your example. Do you advise me to make a new template under categories called: products_in_all_categories.html ?

I've done this but forever reason it doesn't seem to be picking up the template.

P K

P K 0 points

Hi Richy, the new template would go under perch/templates/shop, it should work once you put it there.

Do I need Perch runway in order to do this?

As I am trying to do this with just Perch and Perch Shop.. so that may be where my issues are.

I cant seem to be able to assign product to a category... and it seems to be giving me an input box rather than a dropdown list when trying to assign a category as you can see here..

how my category assign box looks

If this is the case then perhaps I could do this with brands instead?

I literally need to have 3 categories/brands and within those are 3 different products. But I need the products to be displayed under each heading like I've got in this Pen:

https://codepen.io/SomethingRich/pen/PKowom

P K

P K 0 points

That is actually a select box but just looks like an input box! It's a 'multiple' select so should show all your categories.

Looks like you don't have your categories set up properly if they don't appear in that box. Have you created a "set" for your products? I'm not sure what else to suggest, but what I posted earlier isn't a Runway thing. I don't use brands myself. Don't give up on categories, they are good to have working! Check the templates for Set and Category are there and selected in the categories area of control panel.

So do I need to create a 'Set' called e.g. 'Products' and then apply the 'Products In All Categories' Template to that set under Category Template?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The name of your set needs to match whatever you've used in the templates. Products would be an excellent place to start.

So I still haven't managed to get this to work.

Let me explain what I've done.

I've created a Set called 'products' and within that set I've got a category called 'ultrasound'. I'm unsure if I am supposed to select the template that you've advised me to create ('products_in_all_categories.html') here. But there is an option to set it as the set template or category template

I have 5 products and I've gone in and successfully managed to select ultrasound as the category for each of the products.

I then have a page called add-modules.php and on that page I have the following code:

<?php
perch_categories([
'set' => 'products',
'skip-template' => 'true',
'each' => function($item) {
PerchSystem::set_var('catTitle-t', $item['catTitle']);
PerchSystem::set_var('catPath-t', $item['catPath']);
perch_shop_products([
'category' => $item['catPath'],
'template' => 'products_in_all_categories.html'
]);
},
]);
?>

I have then created a new template file under perch/templates/shop called 'products_in_all_categories.html' and that file contains the following:

<perch:before>
<h2>Category Title: <perch:shop id="catTitle-t" /></h2>
<h3>Category Path: <perch:shop id="catPath-t" /></h3>
<ul>
</perch:before>
<li>Product Title: <perch:shop id="title" /></li>
<perch:after>
</ul>
</perch:after>

But when I visit the add-modules.php page it doesn't output anything.

Any idea what I'm doing wrong?

P K

P K 0 points

Your 'Set' template and 'Category' template are different to the one you've created above, and in my case I didn't change those from the ones that came with Perch. They are found in /perch/templates/categories. So if you have changed those, then change them back. So in the control panel under Set Options, in the drop down select boxes, choose 'Set' and 'Category'.

It should work from there, but if not, turn on debug mode - see the docs for how to do that: https://docs.grabaperch.com/perch/building/troubleshooting/how-do-i-debug-problems/

Even when a page seems to output nothing, it still outputs something in debug that may help you work out what's happening... such as "can't find template" etc.

Ok thanks PK. I'll try this later