Page 1 of 2

Custom Template For Category

Posted: Sat Mar 28, 2009 3:26 am
by harlomedia
Hi guys, just curious about how to customize the page for a particular category.

For instance, "category/path/27" "category/path/26" and "category/path/24" should all be controlled by category.tpl

How do I set up a file to control just "category/path/25" ?

Thanks for any help. Let me know if I need to clarify more.

Re: Custom Template For Category

Posted: Sat Mar 28, 2009 4:24 am
by harlomedia
I just noticed that someone asked a similar question below. Sorry to post it again but that one didn't make sense anyway :P .

Re: Custom Template For Category

Posted: Sat Mar 28, 2009 5:22 am
by Qphoria
I'll show you a very easy and cool dynamic way to do this:

Edit: catalog/controller/product/category.php

Find:

Code: Select all

$this->template = $this->config->get('config_template') . 'product/category.tpl';
Replace with:

Code: Select all

$this->template = $this->config->get('config_template') . 'product/category_' . $this->request->get['path'] . '.tpl';
if (!file_exists(DIR_TEMPLATE . $this->template)) {
	$this->template = $this->config->get('config_template') . 'product/category.tpl';
}
Now, anytime you want a custom category template. Create a clone of the category.tpl file using the category path as part of the name. For example:

category_25.tpl
category_25_38.tpl
category_25_38_72.tpl
etc.

The code will first check for the existence of an override file. If there is one it will use it. If there is none for that category, then it will default to the original file.

This can be done for virtually any file. Products, information, etc.

Re: Custom Template For Category

Posted: Sat Mar 28, 2009 5:47 am
by harlomedia
Uh oh... I do not have any folders inside of catalog/controller/

I checked it against the latest download of OpenCart. I'm assuming I have a slightly older version of OpenCart. Can't find the version listed anywhere, though.

Any chance there's a way to do this with the older versions of OpenCart?

Re: Custom Template For Category

Posted: Sat Mar 28, 2009 6:52 am
by harlomedia
And now I see the OpenCart 0.x part of the forum and it's locked. I'm scattered today.

Assuming it's now unsupported. Is there documentation on how to upgrade?

Re: Custom Template For Category

Posted: Sat Mar 28, 2009 7:52 am
by Qphoria
OpenCart 0.x is now its own cart system with its own forums:
OpenCart Zero
It is very much alive, and yes a similar feature can be accomplished with 0.x as well:
Posted here

Re: Custom Template For Category

Posted: Tue Mar 31, 2009 12:04 am
by harlomedia
Thank you! I really appreciate the help.

Re: Custom Template For Category

Posted: Tue Mar 31, 2009 11:59 pm
by paulcortez
Hi Q,
Sorry i'm confused. What is the difference between opencart and opencart zero?
Qphoria wrote:OpenCart 0.x is now its own cart system with its own forums:
OpenCart Zero
It is very much alive, and yes a similar feature can be accomplished with 0.x as well:
Posted here
I already installed opencart in my server ( i like it so far :) ) and maybe opencart zero has a little bit more advantage than this one.

Re: Custom Template For Category

Posted: Wed Apr 01, 2009 12:06 am
by Qphoria
OpenCart version 0.x was the original version of OpenCart. After the release of OpenCart 1.x, it was deemed that the 2 projects were so different that they needed to progress their own separate ways.

OpenCart Zero, just refers to the 0.x branch. It's been around a bit longer so it has a leg up on stability, but both carts have different features, so it comes down to what you are looking for in a cart. I am currently ramping up for a new v0.8 release of OpenCart Zero within a week (hopefully) that will have a whole new assortment of features, improvements (imo), and a new default look.

Re: Custom Template For Category

Posted: Wed Apr 01, 2009 12:30 am
by paulcortez
Thankz a lot Q. That's a relief. hehe :) I thought opencart v1.x will loosing its support. I'm still getting my self acquainted with this system and first time to accept this online shopping project. Thankz again Q. I'll post the URL of my project here when i'm done and open to public.

I made some changes on the template and i'm glad to share it with you.

Re: Custom Template For Category

Posted: Fri Jun 18, 2010 6:12 pm
by timkirtley
How would I go about using dynamic templates for products/categories in 1.4.8? The code currently in my category.php appears slightly different...
Qphoria wrote:I'll show you a very easy and cool dynamic way to do this:

Edit: catalog/controller/product/category.php

Find:

Code: Select all

$this->template = $this->config->get('config_template') . 'product/category.tpl';
Replace with:

Code: Select all

$this->template = $this->config->get('config_template') . 'product/category_' . $this->request->get['path'] . '.tpl';
if (!file_exists(DIR_TEMPLATE . $this->template)) {
	$this->template = $this->config->get('config_template') . 'product/category.tpl';
}
Now, anytime you want a custom category template. Create a clone of the category.tpl file using the category path as part of the name. For example:

category_25.tpl
category_25_38.tpl
category_25_38_72.tpl
etc.

The code will first check for the existence of an override file. If there is one it will use it. If there is none for that category, then it will default to the original file.

This can be done for virtually any file. Products, information, etc.

Re: Custom Template For Category

Posted: Mon Jun 28, 2010 6:45 pm
by timkirtley
Does anyone have any solutions for doing this in the current version?

I have two main categories of products but need a slightly different layout for the product pages for each and have so far been unable to achieve this.

Would really appreciate any assistance with this, thanks.

Re: Custom Template For Category

Posted: Mon Jun 28, 2010 10:03 pm
by Qphoria
In 1.4.0 and later you can do it this way.

1. FIND (All instances):

Code: Select all

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category.tpl')) {
    $this->template = $this->config->get('config_template') . '/template/product/category.tpl';
} else {
    $this->template = 'default/template/product/category.tpl';
} 
2. REPLACE WITH:

Code: Select all

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category_' . $this->request->get['path'] . '.tpl')) {
                    $this->template = $this->config->get('config_template') . 'product/category_' . $this->request->get['path'] . '.tpl';
                } elseif (file_exists(DIR_TEMPLATE . 'default/template/product/category_' . $this->request->get['path'] . '.tpl')) {
                    $this->template = 'defaul/template/product/category_' . $this->request->get['path'] . '.tpl';
                } elseif (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category.tpl')) {
                    $this->template = $this->config->get('config_template') . '/template/product/category.tpl';
                } else {
                    $this->template = 'default/template/product/category.tpl';
                } 

Re: Custom Template For Category

Posted: Wed Jun 30, 2010 4:45 pm
by timkirtley
Awesome.. that's a great help with my category pages.

In case anyone else just copies and pastes the above code, I had to make a couple of tiny corrections:

On the third line
$this->template = $this->config->get('config_template') . 'product/category_' . $this->request->get['path'] . '.tpl';

needed: /template/ in front of 'product/category_'

and default is missing a 't' on the end on the sixth line.

Works a treat though as I say...

How easy is it to apply this approach, or something similar to product templates?

Really I would want to have a different template according to the category the product resides in, as my products only have one category assigned to them.

Re: Custom Template For Category

Posted: Wed Jun 30, 2010 6:45 pm
by JAY6390
You mean based on product id's or the categories still? Either way it should be fairly straightforward

Re: Custom Template For Category

Posted: Mon Jul 19, 2010 8:14 pm
by timkirtley
Hello.. back on this project again (been juggling magento, wordpress e-commerce and opencart projects!)


I'd rather not have to do it via product id's to keep things flexible, but I'd like to have different templates for products in different categories.

Re: Custom Template For Category

Posted: Tue Jul 20, 2010 3:24 am
by spikeachu
timkirtley wrote:Awesome.. that's a great help with my category pages.

In case anyone else just copies and pastes the above code, I had to make a couple of tiny corrections:

On the third line
$this->template = $this->config->get('config_template') . 'product/category_' . $this->request->get['path'] . '.tpl';

needed: /template/ in front of 'product/category_'

and default is missing a 't' on the end on the sixth line.

Works a treat though as I say...

How easy is it to apply this approach, or something similar to product templates?

Really I would want to have a different template according to the category the product resides in, as my products only have one category assigned to them.
I made a mod for this very thing, You select the template from the category / product page in the admin area. So no need to hardcode every page.

http://forum.opencart.com/viewtopic.php?f=23&t=12935

Re: Custom Template For Category

Posted: Tue Jul 20, 2010 11:13 pm
by timkirtley
Sounds perfect.. downloaded it any got so far but ran in to a small problem as described in the other thread... But yeah, this sounds like it will be perfect.

Re: Custom Template For Category

Posted: Wed Sep 07, 2011 7:47 pm
by sanken
Hi Qphoria,
Does this mod works in 1.5.1? I have a site (www.bodybebe.ro) with 6 templates, one for each category, one intro&about. I made a clone of category.tpl (in view/theme/default/product ) as category_59_60.tpl; category_59_63.tpl;category_59_65.tpl;category_59_66.tpl;category_59_67.tpl
and the code was :
if (file_exists(DIR_TEMPLATE . $this->config->get
('config_template') . '/template/product/category_' . $this->request->get['path'] . '.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/category_' . $this->request->get['path'] . '.tpl';
} elseif (file_exists(DIR_TEMPLATE . 'default/template/product/category_' . $this->request->get['path'] . '.tpl')) {
$this->template = 'default/template/product/category_' . $this->request->get['path'] . '.tpl';
} elseif (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/category.tpl';
} else {
$this->template = 'default/template/product/category.tpl';
}

But nothing happened, seems code don't work. Any sugestions?

Re: Custom Template For Category

Posted: Fri Dec 13, 2013 11:51 pm
by maffu
Hi, i need to identify the category NOT by path or id, but by SKU or Model or something else..i can do this?

ps: sorry for my bad english ^^'
Qphoria wrote:I'll show you a very easy and cool dynamic way to do this:

Edit: catalog/controller/product/category.php

Find:

Code: Select all

$this->template = $this->config->get('config_template') . 'product/category.tpl';
Replace with:

Code: Select all

$this->template = $this->config->get('config_template') . 'product/category_' . $this->request->get['path'] . '.tpl';
if (!file_exists(DIR_TEMPLATE . $this->template)) {
	$this->template = $this->config->get('config_template') . 'product/category.tpl';
}
Now, anytime you want a custom category template. Create a clone of the category.tpl file using the category path as part of the name. For example:

category_25.tpl
category_25_38.tpl
category_25_38_72.tpl
etc.

The code will first check for the existence of an override file. If there is one it will use it. If there is none for that category, then it will default to the original file.

This can be done for virtually any file. Products, information, etc.