Forum

Thread tagged as: Question, Configuration, Shop

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.

Mathew Doidge

Mathew Doidge 2 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

Drew McLellan said:

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:

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

and my settings product URL is set to (I assume I can use the perch var I made... :

/shop/{catTitle}/{slug}

And my shop index.php looks like:

       if (perch_get('category')) {

        PerchSystem::set_var('catTitle', perch_get('category'));
        echo 'This is a indv category';
        perch_shop_products([
            'category' => 'products/'.perch_get('category'),
        ]);
    } elseif (perch_get('product')) {
        echo 'This is a product';
        perch_shop_product(perch_get('product'), [
            'template'=>'product.html'
        ]);
    } else {
        // List mode
        echo 'This is a category listing';
        perch_categories([
            'set' => 'products',
            'template' => 'category_list.html',
        ]);
    }

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

<a href="/shop/index.php?category=<perch:category id="catSlug" />"></a>

Product listing links

<a href="/shop/index.php?product=<perch:shop id="slug" type="slug" />"></a>

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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!