OpenCart – New Modules Positions Tutorial

Many of us look to adding more positions for us to display our modules. Let’s take my website as example. Scroll to the bottom of the page, do you see a Facebook like box at the bottom? Well, I can easily display modules on page footer now because I added new positions. So here is a simple tutorial to guide you through.

Getting Started

There’s a few things that you will need:

  1. Basic PHP Knowledge
  2. Basic HTML Knowledge
  3. Basic CSS Knowledge
  4. Text editor

Creating New Position (Controller File)

First, you will have to create a new position. I shall create a PHP file and name it ‘content_footer’. This file should be placed in ‘catalog/controller/common/’ folder.

Next, you will just have to simply paste the following codes in.

<?php
class ControllerCommonContentFooter extends Controller {
    public function index() {
        $this->load->model('design/layout');
        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->load->model('catalog/information');

        if (isset($this->request->get['route'])) {
            $route = (string)$this->request->get['route'];
        } else {
            $route = 'common/home';
        }

        $layout_id = 0;

        if ($route == 'product/category' && isset($this->request->get['path'])) {
            $path = explode('_', (string)$this->request->get['path']);

            $layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
        }

        if ($route == 'product/product' && isset($this->request->get['product_id'])) {
            $layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
        }

        if ($route == 'information/information' && isset($this->request->get['information_id'])) {
            $layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
        }

        if (!$layout_id) {
            $layout_id = $this->model_design_layout->getLayout($route);
        }

        if (!$layout_id) {
            $layout_id = $this->config->get('config_layout_id');
        }

        $module_data = array();

        $this->load->model('setting/extension');

        $extensions = $this->model_setting_extension->getExtensions('module');

        foreach ($extensions as $extension) {
            $modules = $this->config->get($extension['code'] . '_module');

            if ($modules) {
                foreach ($modules as $module) {
                    if ($module['layout_id'] == $layout_id && $module['position'] == 'content_footer' && $module['status']) {
                        $module_data[] = array(
                            'code'       => $extension['code'],
                            'setting'    => $module,
                            'sort_order' => $module['sort_order']
                        );
                    }
                }
            }
        }

        $sort_order = array();

        foreach ($module_data as $key => $value) {
            $sort_order[$key] = $value['sort_order'];
        }

        array_multisort($sort_order, SORT_ASC, $module_data);

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

        foreach ($module_data as $module) {
            $module = $this->getChild('module/' . $module['code'], $module['setting']);

            if ($module) {
                $this->data['modules'][] = $module;
            }
        }

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/content_footer.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/content_footer.tpl';
        } else {
            $this->template = 'default/template/common/content_footer.tpl';
        }

        $this->render();
    }
}
?>

Wondering what all those codes above do? It checks if this is the right layout AND if any extension is to be placed at ‘content_footer’. If there is, it will grab the extension and display it in the template file of content_footer.tpl. So, this means we will need to create a template file for this new module position.

Creating New Position (Template File)

Now, you will have to create a new template file named ‘content_footer.tpl’. This time, the file will be placed at ‘catalog/view/theme/default/template/common/’ folder. Simply paste the codes below into the new template file you had just created.

<?php foreach ($modules as $module) { ?>
   <?php echo $module; ?>
<?php } ?>

Nothing much is actually done here. It just display the modules that is called from the controller file. Simple isn’t it? So now you are almost done! You have set up a new position for your modules. Now, it’s time to link them to your pages.

Linking It Up

Open up and edit catalog/controller/common/footer.php and find

$this->render();
before it, add
$this->children = array(
   'common/content_footer'
);

With this extra line, you have linked up the new module position with your footer. Now, to display it at the footer, you will have to edit the footer’s template. Open up and edit catalog/view/theme/default/template/common/footer.tpl and find

<div id="powered"><?php echo $powered; ?></div>

before it, add

<?php echo $content_footer; ?>

If you have reached this part of the tutorial, great! You have managed to set up a new position for your modules. Now all you will have to do is to add any modules to that position. Some simple changes will be required for this to be done.

Assigning Positions To Modules

For this task, it will be quite simple. Open up the template files of any modules that you would like to display at the new position. Let’s say I would like to display the Featured Module at Content Footer, I will open up admin/view/template/module/featured.tpl and find

<?php if ($module['position'] == 'content_top') { ?>
   <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
<?php } else { ?>
   <option value="content_top"><?php echo $text_content_top; ?></option>
<?php } ?>

before it, add

<?php if ($module['position'] == 'content_footer') { ?>
   <option value="content_footer" selected="selected">Content Footer</option>
<?php } else { ?>
   <option value="content_footer">Content Footer</option>
<?php } ?>

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + = 8

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>