Page 1 of 1

[SOLVED] different template for category page OpenCart 2.2.0.0

Posted: Wed Nov 28, 2018 6:12 pm
by adam.skrzynka
I would ask for a little help

I would like to create another template for my three categories (category ID 119 / ID 125 / ID 140).

I found one solution for version 2.2.0.0.

find $this->response->setOutput($this->load->view('product/category', $data));
in catalog/controller/product/category.php

and replace it with above codes, here is:

$config_theme = $this->config->get('config_theme') == 'theme_default' ? 'default' : $this->config->get('config_theme'); if (file_exists(DIR_TEMPLATE . $config_theme . '/template/product/category_' . $category_id . '.tpl')) {
$view = 'product/category_' . $category_id;
} else {
$view = 'product/category';
}
$this->response->setOutput($this->load->view($view, $data));

Link to the solution:
<https://stackoverflow.com/questions/407 ... 9#53498989>


I created category_119 however, it does not work. I tried to complete this code but nothing works

perhaps I have to complete code by:

* entering the name of my new template (category_119) = but I do not know exactly where to sign

* enter the category ID (119/125/140) = = but I do not know exactly where to sign

* probably enter the name of my template - although I have default - (Default (Default Store Theme)




How to enter correctly, because I try but it fails.

Thank you for your help

Best Regards. Adam.

Re: different template for category page OpenCart 2.2.0.0

Posted: Thu Nov 29, 2018 8:26 am
by muldercs
I changed the template in the category.tpl by just saying


<?php if ($heading_title =='name of your category you want to change') { ?>

Your new template code goes here
<?php } ?>

<?php if ($heading_title <>'name of your category you want to change') { ?>
Your regular category code
<?php } ?>

Re: different template for category page OpenCart 2.2.0.0

Posted: Thu Nov 29, 2018 5:03 pm
by adam.skrzynka
Thank you very much, it works and a much easier solution.

Best Regards.
Adam.

Re: different template for category page OpenCart 2.2.0.0

Posted: Thu Nov 29, 2018 6:58 pm
by OSWorX
Really can't see any benefit in editing the template!
Why?
This 'solution' is limited to one language only.
If you are using more languages, you have to edit always the code inside the template - or adding a if .. then .. else or switch.

The easiest (and most correct, because change is done inside the controller - you should avoid to mix html and php inside any template), is to change this in the controller:

from:

Code: Select all

$this->response->setOutput($this->load->view('product/category', $data));
to:

Code: Select all

$this->response->setOutput($this->load->view('product/category' . $category_info['category_id'], $data));
But this code is limited, because what if you have a category without a custom template?
Then, you should do this instead (check before if exist, otherwise use standard):

Code: Select all

$tpl = 'category_' . $category_info['category_id'];
if( !file_exists( DIR_TEMPLATE . $tpl . '.tpl' ) ) {
	$tpl = 'category';
}

$this->response->setOutput($this->load->view('product/' . $tpl, $data));
Done once, always useable.

Re: different template for category page OpenCart 2.2.0.0

Posted: Fri Nov 30, 2018 5:14 pm
by adam.skrzynka
Hello, thank you for your help - you're right I actually use two languages and it would be less work for your solution

I have a standard template then just use it:
$this->response->setOutput($this->load->view('product/category' . $category_info['category_id'], $data));

however, now I would have to create a file for each category - if I do not have one - it's not loading standard ( category.tpl )

there is no note that if it does not find the category_xxx = it loads the standard category

how to add it correctly??

Thank you very much for your help

Re: different template for category page OpenCart 2.2.0.0

Posted: Fri Nov 30, 2018 6:32 pm
by OSWorX
As already written:

Code: Select all

$tpl = 'category_' . $category_info['category_id'];
if( !file_exists( DIR_TEMPLATE . $tpl . '.tpl' ) ) {
	$tpl = 'category';
}

$this->response->setOutput($this->load->view('product/' . $tpl, $data));

Re: different template for category page OpenCart 2.2.0.0

Posted: Fri Nov 30, 2018 7:04 pm
by adam.skrzynka
still a problem

**** when I exchange for:
$this->response->setOutput($this->load->view('product/category' . $category_info['category_id'], $data));
it works but then I must have an individual template for each category

***** when I exchange for (or insert)
$tpl = 'category_' . $category_info['category_id'];
if( !file_exists( DIR_TEMPLATE . $tpl . '.tpl' ) ) {
$tpl = 'category';
}

$this->response->setOutput($this->load->view('product/' . $tpl, $data));
do not find new categories

***** when I exchange for:

$this->response->setOutput($this->load->view('product/category' . $category_info['category_id'], $data));
$tpl = 'category_' . $category_info['category_id'];
if( !file_exists( DIR_TEMPLATE . $tpl . '.tpl' ) ) {
$tpl = 'category';
}

error.
Parse error: syntax error,

What I'm doing wrong is not working?

Re: different template for category page OpenCart 2.2.0.0

Posted: Fri Nov 30, 2018 7:47 pm
by OSWorX
Sorry. 2.2.x is a totally crap!
Here the tested and working code:

Code: Select all

$tpl = 'category_' . $category_info['category_id'];
if( !file_exists( DIR_TEMPLATE . $this->config->get('theme_default_directory') . '/template/product/' . $tpl . '.tpl' ) ) {
	$tpl = 'category';
}

Re: different template for category page OpenCart 2.2.0.0

Posted: Fri Nov 30, 2018 8:10 pm
by adam.skrzynka
works - it's great

thank you very much for the moment of your time and help me.

Best Regards.
Adam.

Re: different template for category page OpenCart 2.2.0.0

Posted: Mon Dec 03, 2018 4:26 pm
by OSWorX
adam.skrzynka wrote:
Fri Nov 30, 2018 8:10 pm
works - it's great

thank you very much for the moment of your time and help me.

Best Regards.
Adam.
Pleasure to help.