Page 1 of 2

Creating a Custom Page in OpenCart 2

Posted: Sat Jan 10, 2015 10:13 am
by MarketInSG
Other tutorials:
Creating a Custom Page in OpenCart 1.5 (viewtopic.php?f=23&t=59542)
Creating a Custom Page in OpenCart 3 (viewtopic.php?f=23&t=186404)

What you need:
- Basic PHP knowledge
- Basic HTML knowledge
- Text Editor

Required files to create a custom page:
- Controller file
- Template file

Optional files:
- Model file
- Language file (I will be using it in this example here)


Controller file

Create a file name 'static.php' in /catalog/controller/information/

Code: Select all

<?php
class ControllerInformationStatic extends Controller {
  private $error = array();
    
  public function index() {
    $this->language->load('information/static'); //Optional. This calls for your language file

    $this->document->setTitle($this->language->get('heading_title')); //Optional. Set the title of your web page.

    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
      'text'      => $this->language->get('text_home'),
      'href'      => $this->url->link('common/home')
    );

    $data['breadcrumbs'][] = array(
      'text'      => $this->language->get('heading_title'),
      'href'      => $this->url->link('information/static')
    );   
       
    $data['heading_title'] = $this->language->get('heading_title'); //Get "heading title" from language file.

    $data['column_left'] = $this->load->controller('common/column_left');
    $data['column_right'] = $this->load->controller('common/column_right');
    $data['content_top'] = $this->load->controller('common/content_top');
    $data['content_bottom'] = $this->load->controller('common/content_bottom');
    $data['footer'] = $this->load->controller('common/footer');
    $data['header'] = $this->load->controller('common/header');

    // OpenCart 2.1 and below CHOOSE ACCORDINGLY
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/static.tpl')) { //if file exists in your current template folder
      $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/information/static.tpl', $data)); //get it
    } else {
      $this->response->setOutput($this->load->view('default/template/information/static.tpl', $data)); //or get the file from the default folder
    }
    
    // OpenCart 2.2 and above CHOOSE ACCORDINGLY
    $this->response->setOutput($this->load->view('information/information', $data)); 
  }
}
Template file

Create a file name 'static.tpl' in /catalog/view/theme/default/template/information/

Code: Select all

<?php echo $header; ?>
<div class="container">
  <ul class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
    <?php } ?>
  </ul>
  <div class="row"><?php echo $column_left; ?>
    <?php if ($column_left && $column_right) { ?>
    <?php $class = 'col-sm-6'; ?>
    <?php } elseif ($column_left || $column_right) { ?>
    <?php $class = 'col-sm-9'; ?>
    <?php } else { ?>
    <?php $class = 'col-sm-12'; ?>
    <?php } ?>
    <div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>
      <h1><?php echo $heading_title; ?></h1>
      YOUR OWN CONTENTS
      <?php echo $content_bottom; ?></div>
    <?php echo $column_right; ?></div>
</div>
<?php echo $footer; ?> 
Language file

Create a file name 'static.php' in /catalog/language/english/information/

Code: Select all

<?php
// Heading
$_['heading_title']  = 'Static Page'; //Add as many as you want, but remember to call for it in the controller file before you can use it in the template
Feel free to leave any comments behind ;D

Re: Creating a Custom Page in OpenCart 2.0

Posted: Mon Jan 19, 2015 1:21 am
by muhamedauda
Hello
I did as you say on oc 2.0.1.1 and when call view give me blank page !!!!

Re: Creating a Custom Page in OpenCart 2.0

Posted: Mon Jan 19, 2015 1:28 am
by muhamedauda
I recreate and found i forget php letters , but whe call :

Code: Select all

http://localhost/testoc2cart/index.php?route=information/static
it display:

Code: Select all

Notice: Indirect modification of overloaded property ControllerInformationStatic::$data has no effect in /var/www/testoc2cart/catalog/controller/information/static.php on line 10
Notice: Indirect modification of overloaded property ControllerInformationStatic::$data has no effect in /var/www/testoc2cart/catalog/controller/information/static.php on line 12
Notice: Indirect modification of overloaded property ControllerInformationStatic::$data has no effect in /var/www/testoc2cart/catalog/controller/information/static.php on line 18
Notice: Indirect modification of overloaded property ControllerInformationStatic::$data has no effect in /var/www/testoc2cart/catalog/controller/information/static.php on line 24
I did this before on other examples and give me same error, and it !!!!!!!!!

Re: Creating a Custom Page in OpenCart 2.0

Posted: Mon Jan 19, 2015 2:06 am
by muhamedauda
I changed the:

Code: Select all

$this->data
to

Code: Select all

$data
Now view display blank page !!!!

Re: Creating a Custom Page in OpenCart 2.0

Posted: Mon Jan 19, 2015 10:07 pm
by MarketInSG
as long as you follow correctly, that should do.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Wed Mar 04, 2015 10:47 pm
by skip
I got error show on image in attachement for 2.0.1.1 Where to search error ? Thanx

Re: Creating a Custom Page in OpenCart 2.0

Posted: Thu Mar 05, 2015 8:50 am
by MarketInSG
my bad. Missed some codes.

Try copying again. Was missing

Code: Select all

$this->load->view(

Re: Creating a Custom Page in OpenCart 2.0

Posted: Thu Mar 05, 2015 6:54 pm
by skip
Still error, but work without:

Code: Select all

$this->response->setOutput($this->render());  
Thank you!

Re: Creating a Custom Page in OpenCart 2.0

Posted: Sun Apr 12, 2015 2:49 pm
by pta
Hi,

I think, the custom page is successfully created named static, and it is showing blank or displays home page.
Now, i want to put some texts or links, descriptions in that page.
Another inquiry is how to link this page from customer service section,

pls assist.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Wed Apr 15, 2015 8:12 pm
by MarketInSG
the rest is up to your basic HTML knowledge to add links to your template on OpenCart.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Fri Apr 17, 2015 5:51 am
by aljawaid
THIS is amazing... thank you I can now create custom pages in php without using the html editor

The best thing besides the header/footer staying intact is that you can apply a layout to information/static too

Thanks a lot for making it easier. :)

Re: Creating a Custom Page in OpenCart 2.0

Posted: Fri Apr 17, 2015 5:59 am
by aljawaid
Is there a way to add it to the sitemap and add SEO keyword to it too or is that a complicated task?

Just asking :) OCv2.0.2.0 by the way

Re: Creating a Custom Page in OpenCart 2.0

Posted: Mon Oct 19, 2015 12:05 pm
by jewelrana
MarketInSG wrote:This is a follow up to the previous topic http://forum.opencart.com/viewtopic.php?f=23&t=59542

What you need:
- Basic PHP knowledge
- Basic HTML knowledge
- Text Editor

Required files to create a custom page:
- Controller file
- Template file

Optional files:
- Model file
- Language file (I will be using it in this example here)


Controller file

Create a file name 'static.php' in /catalog/controller/information/

Code: Select all

<?php
class ControllerInformationStatic extends Controller {
  private $error = array();
    
  public function index() {
    $this->language->load('information/static'); //Optional. This calls for your language file

    $this->document->setTitle($this->language->get('heading_title')); //Optional. Set the title of your web page.

    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
      'text'      => $this->language->get('text_home'),
      'href'      => $this->url->link('common/home')
    );

    $data['breadcrumbs'][] = array(
      'text'      => $this->language->get('heading_title'),
      'href'      => $this->url->link('information/static')
    );   
       
    $data['heading_title'] = $this->language->get('heading_title'); //Get "heading title" from language file.

    $data['column_left'] = $this->load->controller('common/column_left');
    $data['column_right'] = $this->load->controller('common/column_right');
    $data['content_top'] = $this->load->controller('common/content_top');
    $data['content_bottom'] = $this->load->controller('common/content_bottom');
    $data['footer'] = $this->load->controller('common/footer');
    $data['header'] = $this->load->controller('common/header');

    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/static.tpl')) { //if file exists in your current template folder
      $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/information/static.tpl', $data)); //get it
    } else {
      $this->response->setOutput($this->load->view('default/template/information/static.tpl', $data)); //or get the file from the default folder
    }     
  }
}
Template file

Create a file name 'static.tpl' in /catalog/view/theme/default/template/information/

Code: Select all

<?php echo $header; ?>
<div class="container">
  <ul class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
    <?php } ?>
  </ul>
  <div class="row"><?php echo $column_left; ?>
    <?php if ($column_left && $column_right) { ?>
    <?php $class = 'col-sm-6'; ?>
    <?php } elseif ($column_left || $column_right) { ?>
    <?php $class = 'col-sm-9'; ?>
    <?php } else { ?>
    <?php $class = 'col-sm-12'; ?>
    <?php } ?>
    <div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>
      <h1><?php echo $heading_title; ?></h1>
      YOUR OWN CONTENTS
      <?php echo $content_bottom; ?></div>
    <?php echo $column_right; ?></div>
</div>
<?php echo $footer; ?> 
Language file

Create a file name 'static.php' in /catalog/language/english/information/

Code: Select all

<?php
// Heading
$_['heading_title']  = 'Static Page'; //Add as many as you want, but remember to call for it in the controller file before you can use it in the template
Thanks a lot bro.....

Re: Creating a Custom Page in OpenCart 2.0

Posted: Sun Aug 21, 2016 11:03 pm
by armand1610
Got the following:

Code: Select all

Notice: Error: Could not load template C:/xampp/htdocs/snipers-eye/catalog/view/theme/default/template/default/template/information/static.tpl! in C:\xampp\htdocs\snipers-eye\system\library\template\basic.php on line 26
changed the following:

Code: Select all

$this->response->setOutput($this->load->view('default/template/information/static.tpl', $data));
to this:

Code: Select all

$this->response->setOutput($this->load->view('information/static.tpl', $data));
Now all ok.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Mon Aug 22, 2016 10:52 pm
by MarketInSG
OpenCart 2.2 onwards changed, will need to update the topic again soon

Re: Creating a Custom Page in OpenCart 2.0

Posted: Thu Oct 13, 2016 4:27 am
by paloc
Is it possible to create the custom page and the necessary files and call it something other than 'static'? I have tried to do this unsuccessfully. I get a fatal error saying that the class ControllerInformationxxx does not exist, even if I do not change the name of the class, only the php file.

Thanks

Re: Creating a Custom Page in OpenCart 2.0

Posted: Fri Oct 14, 2016 10:33 pm
by MarketInSG
You need to rename + replace all references of 'static' to the name you wish to use. Do note, no spaces or hyphens allowed.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Sat Oct 15, 2016 7:38 am
by paloc
Thanks - I did get this to work. I failed to create a php file in my CUSTOM template folder.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Fri Jan 06, 2017 4:27 am
by tpalella
MarketInSG wrote:OpenCart 2.2 onwards changed, will need to update the topic again soon
Can you please give me a hint of what has changed. The route no longer works :(

Thank you!

t.

Re: Creating a Custom Page in OpenCart 2.0

Posted: Sat Jan 07, 2017 11:26 pm
by tpalella
Anyone knows what needs to be changed for 2.2+?

thank you!