Post by MarketInSG » Tue Apr 17, 2012 7:59 pm

Other tutorials:
Creating a Custom Page in OpenCart 2 (viewtopic.php?f=23&t=136937)
Creating a Custom Page in OpenCart 3 (viewtopic.php?f=23&t=186404)

I noticed a lot of users are searching for help on this topic, so here's a short tutorial for all of you.

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.

         $this->data['breadcrumbs'] = array();

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

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

      if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/static.tpl')) { //if file exists in your current template folder
         $this->template = $this->config->get('config_template') . '/template/information/static.tpl'; //get it
      } else {
         $this->template = 'default/template/information/static.tpl'; //or get the file from the default folder
      }
      
      $this->children = array( //Required. The children files for the page.
         'common/column_left',
         'common/column_right',
         'common/content_top',
         'common/content_bottom',
         'common/footer',
         'common/header'
      );
            
      $this->response->setOutput($this->render());      
     }
}
?>
Template file

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

Code: Select all

<?php echo $header; ?><?php echo $column_left; ?><?php echo $column_right; ?>
<div id="content"><?php echo $content_top; ?>
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <h1><?php echo $heading_title; ?></h1>
 
  YOUR OWN CONTENT
 
   <?php echo $content_bottom; ?></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
Last edited by MarketInSG on Sat Jan 10, 2015 10:07 am, edited 1 time in total.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by littlebigbox » Wed Apr 25, 2012 5:48 pm

Hi I have followed your post, but can't seem to get the page to work, it just comes up blank?

Any ideas the link is:

http://lulabellsbaby.co.uk/index.php?ro ... /wholesale

Newbie

Posts

Joined
Wed Jul 27, 2011 10:43 pm

Post by MarketInSG » Thu Apr 26, 2012 11:39 am

Well, you must be missing something then. Try creating everything the same as mine, name it static etc.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by opcrat » Fri May 04, 2012 9:54 am

I think to create a static page, you don't have to add all these, you can easily create new page and add content through your admin (catalog -> Information -> Insert).

littlebigbox :- You are using external theme so you must have to include view file in your external theme folder and it looks like syntax error in your code that's why your server is not able to run.
Last edited by opcrat on Sat May 05, 2012 12:07 am, edited 2 times in total.

Most Useful Extensions || Most Selling Extensions
Opencart Development || Opencart Designing || eCommerce Project

✔ Join more than 5000 Happy Opencart Store Owners enjoying the extensions designed and developed by Opcrat, Opencart Partner.


User avatar
Active Member

Posts

Joined
Wed Dec 21, 2011 6:54 am

Post by MarketInSG » Fri May 04, 2012 8:03 pm

opcrat wrote:I think to create a static page, you don't have to add all these, you can easily create new page and add content through your admin (catalog -> Information -> Insert).

littlebigbox :- You are using external theme so you must have to include view file in your external theme folder and it looks like syntax error in your code that's why your server is not able to run.
No. You are wrong then. We are talking about adding a complex page which can do more than just static information. Eg. Adding your own news page and gets latest news off your database.

And no, you just have to include your template file in your default folder. 99% of controller files call for the template from the current theme folder. HOWEVER, if it's not found, it will retrieve from your default folder. So point is, default folder is needed.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by opcrat » Sat May 05, 2012 12:07 am

I thought you are talking about static pages. You took me wrong but you can include a point of external theme. Point is not default folder is needed or not, point is to solve littlebigbox's issue.

Most Useful Extensions || Most Selling Extensions
Opencart Development || Opencart Designing || eCommerce Project

✔ Join more than 5000 Happy Opencart Store Owners enjoying the extensions designed and developed by Opcrat, Opencart Partner.


User avatar
Active Member

Posts

Joined
Wed Dec 21, 2011 6:54 am

Post by tophat » Sat May 05, 2012 6:17 am

I just want to say a massive Thank you, thank you, thank you for this!!! I've been hunting down an answer for months amd months and this has simply summed it up so simply and easily. It's precisily what I was looking for as I'm not experienced in PHP or the MVC platform to be honest so couldn't get my head around which files I needed to make my own page. Amazing!!!

A bit of a novice mistake to start with but as our site is all built with SEO friendly urls, I kept getting 404'd when looking for /information/static and forgot this wasn't set like this. Wasn't till I saw opcrat's url I realised it should be with the URL:

/index.php?route=information/static

Doesn't matter for the record if the .tpl is in default or your theme folder from what I noticed.

Also, I've no idea if this applies to just me and my theme (I expect its just me) but I had an issue with the breadcrumbs not displaying. Had to add this to the .tpl to make it work:

Code: Select all

<?php echo $header; ?>
 <div class="breadcrumb">
 <div class="container">
 <h1><?php echo $heading_title; ?></h1>
 </div>
 <div id="breadcrumbs">
 <div class="container">
 <?php $zet = 100;  $counter = 0; ?>
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
   
    <div id="bread" <?php if($zet == 100) echo 'class="first"' ?>style="z-index: <?php echo $zet-- ; ?>; left: <?php echo $counter * -30;?>px;">
    <a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <span></span>
    </div>
    <?php $counter++; } ?>
  </div>
    </div>
   </div>
  </div> <!--  end top_container -->
  <div class="container">
Thanks again. Can finally pull in the glorious wordpress in with opencart for one ass kicking site =)

Webmaster for: http://www.getlaidbeds.co.uk + www.mattressmoose.co.uk + www.pixellounge.co.uk


User avatar
Active Member

Posts

Joined
Fri Nov 25, 2011 8:31 am


Post by tophat » Sat May 05, 2012 8:18 am

Just wondering how easy it would be to give this page an SEO friednly url? thanks.

Webmaster for: http://www.getlaidbeds.co.uk + www.mattressmoose.co.uk + www.pixellounge.co.uk


User avatar
Active Member

Posts

Joined
Fri Nov 25, 2011 8:31 am


Post by MarketInSG » Sat May 05, 2012 12:43 pm

Code: Select all

RewriteRule ^static$ index.php?route=information/static
Add that code into your htaccess file right after the rewrite on part. So to access your page, you can go to yoursite.com/static instead of the long url.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by MarketInSG » Sat May 05, 2012 12:50 pm

opcrat wrote:I thought you are talking about static pages. You took me wrong but you can include a point of external theme. Point is not default folder is needed or not, point is to solve littlebigbox's issue.
He didn't even bother replying which implies he don't care any more. For modules and everything, it is best to have the template file in the default folder so as there will always be a fallback. This is a practical way of doing stuffs and the recommended way for all mods developers to learn.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by MarketInSG » Sat May 05, 2012 12:51 pm

tophat wrote:I just want to say a massive Thank you, thank you, thank you for this!!! I've been hunting down an answer for months amd months and this has simply summed it up so simply and easily. It's precisily what I was looking for as I'm not experienced in PHP or the MVC platform to be honest so couldn't get my head around which files I needed to make my own page. Amazing!!!

A bit of a novice mistake to start with but as our site is all built with SEO friendly urls, I kept getting 404'd when looking for /information/static and forgot this wasn't set like this. Wasn't till I saw opcrat's url I realised it should be with the URL:

/index.php?route=information/static

Doesn't matter for the record if the .tpl is in default or your theme folder from what I noticed.

Also, I've no idea if this applies to just me and my theme (I expect its just me) but I had an issue with the breadcrumbs not displaying. Had to add this to the .tpl to make it work:

Code: Select all

<?php echo $header; ?>
 <div class="breadcrumb">
 <div class="container">
 <h1><?php echo $heading_title; ?></h1>
 </div>
 <div id="breadcrumbs">
 <div class="container">
 <?php $zet = 100;  $counter = 0; ?>
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
   
    <div id="bread" <?php if($zet == 100) echo 'class="first"' ?>style="z-index: <?php echo $zet-- ; ?>; left: <?php echo $counter * -30;?>px;">
    <a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <span></span>
    </div>
    <?php $counter++; } ?>
  </div>
    </div>
   </div>
  </div> <!--  end top_container -->
  <div class="container">
Thanks again. Can finally pull in the glorious wordpress in with opencart for one ass kicking site =)
Thanks for the praise :D


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by tophat » Sun May 06, 2012 1:19 am

MarketInSG wrote:

Code: Select all

RewriteRule ^static$ index.php?route=information/static
Add that code into your htaccess file right after the rewrite on part. So to access your page, you can go to yoursite.com/static instead of the long url.
Simply amazing. That was easier than I thought.

I had to put it directly below

Code: Select all

RewriteBase /
for it to work, anywhere else it didn't regardeless of other rewrite rules.

Thanks again.

Webmaster for: http://www.getlaidbeds.co.uk + www.mattressmoose.co.uk + www.pixellounge.co.uk


User avatar
Active Member

Posts

Joined
Fri Nov 25, 2011 8:31 am


Post by alex1 » Tue May 15, 2012 11:51 am

Very helpful, thanks!

I guess I'm just a bit confused about:

1) How do you add a database call here, and have the template be able to read the variables?
2) How can you pass this page variables?

Thanks again, very helpful!

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by MarketInSG » Tue May 15, 2012 12:23 pm

1. It's possible if you want. Depends on how you want it and what you want to call for though.
2. $this->data['variable'] = 'string';

Use that to pass variables.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by alex1 » Tue May 15, 2012 9:08 pm

thanks... how do you add a db call? I tried to do one before, but it didn't work, I needed to use a built in OC db call, but wasn't sure how

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by MarketInSG » Tue May 15, 2012 9:16 pm

What do you want to call? That's the point. For product, you can call for $this->load->model('catalog/product'); and call for which ever function you need in the model file. If you don't know PHP, all these are beyond your reach.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by alex1 » Tue May 15, 2012 10:34 pm

What I mean is, how can I run a sql query on this page? What would be the proper syntax to get it to work here?

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by MarketInSG » Wed May 16, 2012 6:53 am

You can just run it normally here if you would like. There's nothing special about it. Just the usual way can do


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by alex1 » Wed May 16, 2012 10:20 am

Can you show an example? I'm not too sure how you run a query here... when i try and set a new DB call, it never works, no error, just never runs... thanks!

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by MarketInSG » Wed May 16, 2012 2:55 pm

if the function you want is already in the model of any of the scripts, you can call for it as I said with $this->load->model('catalog/category');

else it's just normal if you want, you can call for it in your controller files too.

Code: Select all

$query = mysql_query("SELECT * FROM table WHERE value='value'");


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore
Who is online

Users browsing this forum: No registered users and 17 guests