Forum

Thread tagged as: Question, Shop

perch:if and categories

I'm trying but failing to display differing shop product template content dependent on product category. I'm wanting to display either a 'buy' or 'register' button depending on whether the product is physical or an event.

In semi-psuedo code, I'd envisaged something like this working:

<perch:if match="category" value="products/physical">
    <perch:input type="submit" value="Buy" />
</perch:if>

<perch:if match="category" value="products/event">
    <perch:input type="submit" value="Register" />
</perch:if>

… but think I'm mixing up id and category filtering and so getting nowhere.

Ta.

Pete Aylward

Pete Aylward 0 points

  • 4 years ago

Replying to myself, but thinking about it, my suggestion would be a bad way to achieve what I want (hardcoding a template against an externally set variable), so think it makes more sense to hardcode the pre-determined product types into the product template itself …

<perch:shop id="productType" type="radio" label="Product Type" options="Physical,Event" />

… and then filter on that ID.

Or even just check that an ID that only the events have (i.e. a date) exists, and run the <perch:if on that.

Duncan Revell

Duncan Revell 78 points
Registered Developer

If you're using <perch:if ..., the match tag should be eq or gt or lt etc - so that's why it's failing. It should be

<perch:if id="category" match="eq" value="products/physical">

The id probably won't be "category", it's probably catPath.

I considered that Duncan, but a category doesn't perform as a regular id does, does it? i.e., there's nothing in <perch:showall /> that gives the category value itself to match on, just the array of category IDs.

Categories have always confused me …

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to use categories tags to loop through the categories:

<perch:categories id="category" set="products">
    <perch:if id="catPath" value="products/bananas">
        ...
    </perch:if>
</perch:categories>

Thanks as ever, Drew.

For the record, I needed to include a trailing slash on the value to get that working:

<perch:categories id="category" set="products"> 
<perch:if id="catPath" value="products/bananas/"> 
Yes! We Have No Bananas
</perch:if> 
</perch:categories>