Htmyell

After spending the better part of a work day trying to figure out how to set it up so that modules could be positioned throughout my theme in more places than the default "content top," "content_bottom,"  "right," "left," etc. and after rubbing away the Z on my keyboard. I managed to get my module and theme to behave as intended. This article will explain how.

What you should know:

  • I should start by saying that I am by no means an OpenCart expert. In fact, This is on my third day every using OpenCart. As such, I have no idea if this works with 1.5.1 or older versions of the platform.
  • The changes in this tutorial are made to files that might be overwritten with upgrades or make future updates more difficult. You may be able to include all catalog files into your custom theme folder, however... I have not tested this theory.
  • This is a relatively exhaustive process that much be done to every module for every new position you wish to add. (though it gets easier once you get the hang of it).
  • This tutorial only adds positioning to the "Home" layout. I would assume to add it to other layouts you would just need to edit the corresponding template files (below) but there I go assuming again.

Getting Started:

I started by downloading and installing Hildebrando's Free Youtube Video module for OpenCart v1.5.1.1. I chose this mod mainly because it was highly rated and didn't involve too much back-end functionality. I assume you could modify any module similarly, however we all know that they say about assumption. If you're unsure I would get the feel of HildeBrando's version first so as not to waste your time.

Adding Positions in Admin:

First, you need to open the module's language file located in; /admin/language/english/module/ and add your new position. For this tutorial we're going to be adding a new position called: Content Middle.

$_['text_content_middle']       = 'Content Middle';

Second, you need to open your module's admin template file located in; /admin/view/template/module/ and add a new "if position is set" statement at around line 50.

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

and in the same file add the option to the javascript function at around line 140:

    html += '      <option value="content_middle"><?php echo $text_content_middle; ?></option>';

Third, you need to open the module's controller file located in; /admin/controller/module/ and add a new line anywhere around like 35.

$this->data['text_content_middle'] = $this->language->get('text_content_middle');

You should now be able to see the new position in your modules settings. Also make sure the module's layout is set to "Home."

Adding Positions to Your Template:

First, you must add the position to the array located in; /catalog/controller/common/home.php around line 20.

'common/content_middle',

Second, you'll need to create the corresponding PHP file in /catalog/controller/common/ (for example: "content_middle.php"). Add the following code, pay attention to lines 2, 50, 79, 80, and 82) as will need to reflect your position's name:

<?php
class ControllerCommonHomeOne 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 = $this->request->get['route'];
		} else {
			$route = 'common/home';
		}

		$layout_id = 0;

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

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

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

		if (substr($route, 0, 23) == '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'] == 'home_one' && $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/home_one.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home_one.tpl';
		} else {
			$this->template = 'default/template/common/home_one.tpl';
		}

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

Third, create the corresponding TPL file in /view/theme/your-theme/template/common/ (for example: "content_middle.tpl"). Add the following code:

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

Now you can call your insert your position anywhere in your theme's home.tpl file by calling

<?php echo $content_middle; ?>

Now, any modules you assign to Content Middle will appear here in your theme.

Well that's it, I hope it helped. It's definitely not the most straight forward thing in the world but after a while it does start to make sense. I do feel like there's plenty of opportunity to improve, but even still, OpenCart is the cleanest eCommerce solution I've worked with.

If this tutorial has helped you, if you have any questions, or know a better way, please don't hesitate to leave a comment.

About Jamie Taylor

Lead Developer and Interactive Manager at kenmontdesigngroup.com, Owner and operator of Htmyell.com.
Comments (14) Trackbacks (1)
  1. Hi! First I want to say you thanks. I found something like that about 8 hours.

    I have a question!

    Whit some modification I succeed to make this , work for OpenCart 1.5.0
    All works fine but if I want to put in header.tpl I see this ERROR:

    Notice: Undefined variable: content_middle in C:\wamp\www\MyShop\upload\catalog\view\theme\web7hztemplate\template\common\header.tpl on line 51

    What I want to do is to have 2 div display inline one will be logo and one will be slider .
    ________________________________________________
    | |
    | header |
    |________________________________________________|
    | | |
    | | |
    | Logo | Slider |
    | | |
    |______________________|_________________________|
    | |
    | currency cart language |
    |________________________________________________|

  2. Because I can’t found modify button I what do ask you how can I put in content_middle the slideshow module. Actually I can put only the information module.

    Excuse my bad English!

    P.S. Also the first paragraph shall be Hi! First I want to say you thanks.I looked for something like this for about 8 hours and than I found you.

  3. You should be able to do that in your theme’s header.tpl and home.tpl files in “catalog/view/theme/*your-theme*/template/common/”

    in header.tpl look for:
    the php bit about the logo.

    and play with it’s positioning. Sorry I can’t be of more help, still new myself.

  4. Hi Jamie,
    Being new to Opencart I’ve been trying to find a way to insert a new module position and your article looks the goods but will this work in OC 1.4.9.1 ?
    What I’m trying to achieve is a module position in the header for a login module to appear on all pages.
    Cheers

  5. Thank you! I like it really helped!

  6. hi there, very nice tutorial.
    following it, I have made it to make and put new position to a model.
    thank you.

    now, I can position the module to show before or after the header.
    but, what I really wanted to achieve is to position the module i-n-s-i-d-e the header.
    I have tried to echo the module in header.tpl file but Warning: Undefined variable… shows up.

    is there a solution for this?

    yes, I am using Opencart v.1.5.3.1 with custom theme.

  7. That’s really helped! thanks!

  8. If you want to put the new module position inside the header, then you need to modify the controllor for the header. You need to let it know that your new position is a child.

    open catalog/controller/common/header.php

    find:

    $this->children = array(
    ‘module/language’,
    ‘module/currency’,
    ‘module/cart’
    );

    change to:

    $this->children = array(
    ‘common/content_middle’,
    ‘module/language’,
    ‘module/currency’,
    ‘module/cart’
    );

    You can now put this inside your header.tpl:

  9. Aaaah, thanks for sharing. That’s really helpful!

  10. Congratulation … this save me … :D

  11. Hi! Аirst of all thank you for this fine manual.
    But, I can’t perform it on OC 1.5.4, despite the fact, that I did everything step by step.
    Firstly, you have error in example: “content_middle.php” in line 79,80,82. “home_one.tpl”, but and correctly “content_middle.tpl”
    Secondly, I heve erorr @Fatal error: Class ‘Controllercommonhomeone’ not found in Z:\home\oc\www\system\engine\controller.php on line 41″ I think this is because the class is not called as a controller.
    When, I rename class as controller – nothing happens. Error disappears, but the module not displayed.

    What do you think about this?

    Thanks in advance. Excuse my bad English!

  12. I’m not sure, actually… Are all the file names consistent? That seems like a file/class name is off.

  13. I should opencart 1.5.5.1.

    An error is @ Fatal error: Class ‘Controllercommonhomeone’ not found in Z: \ home \ oc \ www \ system \ engine \ controller.php on line 41 ”

    Replaced by class Controllercommonhomeone ControllerCommonHome.
    An error is Fatal error: Cannot redeclare class ControllerCommonHome in / opt / lampp / htdocs / qdent.ru / catalog / controller / common / content_middle.php on line 87.

    Adapt the article and please for version opencart 1.5.5.1
    Thanks in advance. Excuse my bad English!

  14. Thanks for the great tutorial, I can confirm that this works fine on 1.5.4 and I implemented it using VQMod so I didn’t have to change the core files directly.


Leave a comment

You must be logged in to post a comment.

Copyright © 2014 Htmyell.com