Forum

Thread tagged as: Problem, Shop

Perch Shop get category from URL

Just setting up Shop and so far I have a shop/index.php and a shop/category.php

My index.php has a simple list of categories which link to category.php where I want to display all products within each cat. My problem is that category.php is not getting the category from the slug passed from index.php. So the <title>, <h1> and list of products are just blank.

index.php (extract):

<?php perch_categories(array(
    'set'=>'products',
    'template'=>'category_shop.html',
)); ?>

category_shop.html template:

<perch:before><ul></perch:before>
<perch:if id="catParentID" value="0">
<li>
    <a href="category.php?cat=<perch:category id="catSlug" />">
        <perch:category id="catTitle" type="smarttext" label="Title" required="true" />
    </a>
</li>
</perch:if>
<perch:after></ul></perch:after>

category.php:

<?php 
    include($_SERVER['DOCUMENT_ROOT'].'/admin/runtime.php');

    perch_content_create('Page header', array(
        'template' => 'page_header.html',
        'multiple' => false,
    ));

    perch_layout('header', array(
        'title' => perch_category(perch_get('category'), true),
    )); 

?>

<div class="clear">
        <h1><?php perch_category(perch_get('category'), true) ?></h1>
        <?php perch_content('Page header'); ?>

        <?php perch_shop_products([
            'category' => 'products/'.perch_get('category'),
        ]);
        ?>
</div>

<?php perch_layout('footer'); ?>
Stephen Turvey

Stephen Turvey 0 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

You need to look at how you're creating your links in category_shop.html and the URL parameters you're setting.

Then look at your category.php page and look at the variables you're picking up from the URL using perch_get.

The two should match.

I'm still not sure what to do from that.

Duncan Revell

Duncan Revell 78 points
Registered Developer

<a href="category.php?cat=
perch_get('category')

cat= and 'category' need to match.

The products are now displaying as they should do but the code for the <h1> and <title> must still be wrong:

<h1><?php perch_category(perch_get('cat'), true) ?></h1>
Duncan Revell

Duncan Revell 78 points
Registered Developer

https://docs.grabaperch.com/functions/categories/perch-category/

If you study the above, perch_category is expecting 3 parameters.

The first needs to be the category path (the same as you are passing to perch_shop_products).

The second is expecting an array (and can be an empty array).

The third is true to stop the value echoing to the page.

I'm just guessing now I'm afraid...

<?php perch_category(perch_get('cat'), array(), true) ?>
Duncan Revell

Duncan Revell 78 points
Registered Developer

Looking pretty good - just replace perch_get('cat') with 'products/'.perch_get('cat') (which is actually the category path).

Still not working:

<h1><?php perch_category('products/'.perch_get('cat'), array(), true) ?></h1>
Duncan Revell

Duncan Revell 78 points
Registered Developer

Oops - sorry - for that one get rid of the , true. (And in fact, you can also get rid of the empty array).

I thought we were looking at

perch_layout('header', array( 
'title' => perch_category(perch_get('category'), true), 
)); 

(which you'll need to change too).

Let's do one thing at a time! First the <h1>:

<h1><?php perch_category('products/'.perch_get('cat')) ?></h1>

This is outputting the category title as well as a bunch of template html (ul, li, h4). Is there not a more simple tag to get the catTitle only without all the extra markup?

Duncan Revell

Duncan Revell 78 points
Registered Developer

That is because the perch_category calls the default template of category.html. You'll need to create yourself a new, simple template to pas into the function.

Your new template will just need

<perch:category id="catTitle" />

And you will call it like so

<h1><?php perch_category('products/'.perch_get('cat'), array('template' => 'pathto/yourtemplate.html')); ?></h1>

There's various bits of help in the documentation to help with the template thing if you haven't had to do that before...

OK thanks that's working. Now to the <title> which currently comes through blank:

perch_layout('header', array(
'title' => perch_category(perch_get('cat'), array(), true),
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

Just like above, use the full cat path.

'products/'.perch_get('cat')

Thanks Drew and Duncan.