Jump to content

Creating conditional code based on product ID or Category of product?


Recommended Posts

Hey there,

I have this idea to create different template pages for different types of products. I am completely comfortable doing it on an individual ID basis or via categories.

 

The logic would be something like:

 

if product_id=15, 12, 2

display this page template

else

display default page template

 

OR

 

if p.parent_category= widgets

display this page template

else

display default page template

 

(where all products in the widgets category will use the new page template.)

Link to comment
Share on other sites

OK..

 

My goal was to be able to create a product page but remove the price, add to cart, and other items from the layout because the product was simply meant to be an alternate listing for other products, however, this code can really work for anyone.

so for those of you who want to change the layout of your product.tpl based on a product ID number, all you have to do is the following (p.s. this is messy and could be done cleanly using includes).

 

You simply need find this code:

{if isset($confirmation) && $confirmation}
<p class="confirmation">
 {$confirmation}
</p>
{/if}

 

and add this after it

{if $product->id|intval==27 }

{else}

 

And this at the very very last line of that page:

{/if} 

(this closes out the if statement)

 

That first chunk of code starts it all off saying that if the product id is 27, display the following layout.. Between the {if} and {else} paste all of your layout code for the new page. I created my layout by taking the chunks I wanted to keep from the product page and pasting them before the {else} and testing over and over again on a product until I got it to look right.

 

Because you are working directly in the product.tpl page you can use any and all of the functions already called/defined in that document, so it really is just copy/paste.

 

If you want to include multiple product ID's your if statement will look something like this:

{if $product->id|intval==27 ||$product->id|intval==28 || $product->id|intval==29 ||$product->id|intval==31 ||$product->id|intval==32 || $product->id|intval==33}

 

So thats it.

 

The goal ultimately is to use an include instead of coding the new layout directly in to that page. The other goal is to also simply create a check box so that when an admin creates a new product they can check a box called alt_layout and my if statement can simply by if alt_layout == true to call the new layout dynamically.

 

its all not too complex, so if anyone wants to run with this, feel free.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Wow,

 

exactly what i was looking for. And it works with PS 1.5.2.

But is there maybe already a more smoother solutione around?

 

I agree with the ultimate goal. Maybe a drop down list for all products and categories for multiple layouts and stored in the database?

Just Dreaming :-D

Link to comment
Share on other sites

×
×
  • Create New...