How do i get access to all front end pages on an opencart module?

665 Views Asked by At

Is there a way I can create an OpenCart module that has access to all the (front-end) pages of the store? What I want is for the user to not have to “bind” this module to all the layouts in order for it to exist in all pages. Basically I want to build a module that injects some js code to every page of the app and depending on some OpenCart hooks on the back-end to run some extra js code on front or some api requests on the back-end. Thanks

1

There are 1 best solutions below

7
On

I am not sure exactly what you are looking for by saying you want a module to have "access" to all the (front-end) pages of the store. Modules have to be placed in layouts, and layouts can be assigned to multiple pages.

Each layout can have elements that are assigned specifically to areas you can designate (and customize). For example, you might want to create a special place for your module in the common header. You would simply create the controller, language and view files in a folder of your own designation (in my example, I will use the common folder), and let's call these files "loadjs".

Pattern these after the common/header.php and .tpl files. Just be sure to change the class and file names in the controller file to match. So you would create a loadjs.php file in the common directory of the controller folder that contains the ControllerCommonLoadjs class, a loadjs.php file in the language/(your language)/common folder, and a loadjs.tpl file in the view/theme/(your theme name)/template/common folder.

Add a $data['loadjs'] = $this->load->controller('common/loadjs'); to your controller/common/header.php file and <?php echo $loadjs; ?> in your view/theme/(your theme name)/template/common/header.tpl file. You can now create your module.

However, to be able to add it to your layout you will need to make some changes to some admin files. Add $_['text_loadjs'] = 'Loadjs'; to your admin/language/(your language)/design/layout.php file and $data['text_loadjs'] = $this->language->get('text_loadjs'); to your admin/controller/design/layout.php file.

You will need to make a change to your admin/view/template/design/layout_form.tpl file as well. Look for the following code:

<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 } ?>
<?php if ($layout_module['position'] == 'content_bottom') { ?>
<option value="content_bottom" selected="selected"><?php echo $text_content_bottom; ?></option>
<?php } else { ?>
<option value="content_bottom"><?php echo $text_content_bottom; ?></option>
<?php } ?>

and add the following code just below that:

<?php if ($layout_module['position'] == 'loadjs') { ?>
<option value="loadjs" selected="selected"><?php echo $text_loadjs; ?></option>
<?php } else { ?>
<option value="loadjs"><?php echo $text_loadjs; ?></option>
<?php } ?>

Now, login to your OpenCart Dashboard and add your module to the loadjs position on your layout. And that should do it.