Forum
Can I access category from a shop product template?
Hi there,
Is it possible to access the category a product is entered into via a produst listing template? I have my shop running on vanilla Perch.
My main shop page shows my product categories with an image. You can click this to go into the category and list out the products for the category you've clicked. What I am trying to do it format the route so that I can do shop/category/produttitle.
So for my list.html
I was using <a href="/shop/<perch:shop id="slug" type="slug" />">
but can I dynamically get the category into this url for the template? I see in <perch:showall />
there is a category array but I've no idea if I can access the actual category being veiwed.
UPDATE
I have used a Perch variable to pass the category title into the template. Although I am still stuck how I can set the right option for the product URL, in the settings. I tried something like shop/{category}/{slug}
.
Help or guidance appreciated.
Yes, you need to use the
<perch:categories>
tag pair to output categories.I believe this is included in the default product template. You can copy it from there.
Hi Drew,
Brilliant, thanks for that. Seems to work a treat in terms of grabbing the category name. I think somewhere along the line I am massively confused with my templates now. My main shop page displays the category listing. A user can click that and enter that category to see the products within - works fine. The bit that's still not working is clicking any of those products to see the detail view. At the moment the URL is correct but no product is appearing. My link on the products listing now looks like:
and my settings product URL is set to (I assume I can use the perch var I made... :
And my shop index.php looks like:
Like I said, the main shop page works fine. The next listing page also works fine, but the product code is never actively triggered and I'm having brain melt after looking at it all day.
Update...
Ok so I've stripped out my htaccess rewrites to avoid confusion and it works, kind of.
Category listing links
Product listing links
So it works in terms of displaying the right parts, though now I am wondering how I clean those urls and get the product listing links to sit with the category slug before it.
Ok, great.
Thank you for your help Drew. I managed to work it out, it was my routing via the htacess causing the main issues once you had helped point me towards accessing the category slug!