Tuesday, April 8, 2014

Creating the language file for catalog (frontend) module in OpenCart [Getting Started with OpenCart Modules]

You can create a language file in a similar way as we did in the admin section. For the frontend, your language file will be located at catalog/language/english/ module/MODULENAME.php. The filename should be the same as the module name. As per the Hello World module, the language file name is helloworld.php, it is created at catalog/language/english/module/ and consists only of the following code:
<?php
  // Heading
  $_['heading_title']  = 'Hello World';
?>
The Hello World text is assigned to heading_title; with the same heading_title, it is accessible to the controller.

Creating the controller file for catalog (frontend) module in OpenCart

A controller file of a module for the frontend is found at catalog/controller/ module/MODULENAME.php; as per the Hello World module, we can see the helloworld.php files atcatalog/controller/module. Since we named the file helloworld.php and put it at module/folder, the controller classname will be ControllerModuleHelloworld.
class ControllerModuleHelloworld extends Controller {
Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.
protected function index() {
The index function is always loaded by default if the second segment of the URL is empty. We can load the module controller at http://example.com/index. php?route=module/helloworld/index orhttp://example.com/index. php?route=module/helloworld.
Here the second segment of the URI is index; if you have created other functions, we can call the function of the module by passing it into the second segment of the URL.
$this->language->load('module/helloworld');
Loading of language files is done with the preceding line of code. According to the previous line, the helloworld.php file at catalog/language/english/module/ is loaded if English language is active or it will load as per the language activated. For example, if Spanish language is active, it loads from catalog/language/spanish/ module/.
$this->data['heading_title'] = $this->language->get('heading_title');
The preceding line fetches the text "Hello World" with $this->language>get('heading_title'); and assigns it to the heading_title variable of the data array. The $heading_title file will show "Hello World" in the template files.
if (isset($this->request->server['HTTPS']) && (($this->request   ->server['HTTPS'] == 'on') || ($this->request->server['HTTPS']
    == '1'))) {
    $this->data['code'] = str_replace('http', 'https',       html_entity_decode($this->config->get('helloworld_code')));
    } else {
      $this->data['code'] = html_entity_decode($this->config
      ->get('helloworld_code'));
    }
The first line of code checks whether SSL is active. If SSL is active, the link's http of $this->config->get('helloworld_code') is replaced with https.
You will be able to get the value of the setting table in a database by passing the key. For example, consider the setting table of a database that consists of the following rows, as shown in the following screenshot:


If you want to show Dressing Shop, you can get it easily wherever you like in the controller, model, or template files. You just have to type the following line of code:
echo $this->config->get('config_name');
But if serialized is equal to 1, it means that the value is stored in an array.
if (file_exists(DIR_TEMPLATE . $this->config   ->get('config_template') . '/template/module/helloworld.tpl')) {
    $this->template = $this->config->get('config_template') .
      '/template/module/helloworld.tpl';
    } else {
    $this->template = 'default/template/module/helloworld.tpl';
    }
  $this->render();
You can get an active template name from $this->config->get('config_ template'); the preceding lines of code check whether the helloworld.tpl file is on the active template or not. If the file is found in the active template, it uses it, or it will use one from the default template. It will be better if we keep the files on the default theme.

Creating the template file for catalog (frontend) module in OpenCart

You can find the template file at catalog/view/theme/<template name>/module; as for the Hello World module, the file name is helloworld.tpl. OpenCart frontend template files have deeper folder structures than the admin ones because admin sections can have only one template. For the frontend, on the other hand, there can be any number of templates; among them, one is selected from the admin |system | setting | edit | the store and at the store tab choose the best template under the Template field.
A folder named <template name> is created at catalog/view/theme. One of the basic rules in OpenCart is never to edit the default theme template file because if OpenCart does not find certain template files on your theme <template name> folder, it will find them on the default theme. While upgrading, the changes made on your custom theme will also get overridden. If template files are not found on the default theme, it shows the following error:
Notice: Error: Could not load template catalog/view/theme/customtheme/template/ module/helloworld.tpl! in system\engine\controller.php
Here, the theme folder's name is customtheme.
If you see this kind of error, it means that helloworld.tpl is missing on the customtheme and default theme folders. So you need to create the helloworld. tpl file atcatalog/view/theme/customtheme/template/module/ or catalog/ view/theme/default/template/module/. Since the helloworld.tpl file is not the default file of OpenCart, we can place it either on customtheme or in default theme.
If you require any changes on the default theme template files, you have to copy the files and folders to the customtheme folder and make changes on the customtheme folder's files, so upgrading it will help in preserving your changes. The following are the code on catalog/view/theme/default/module/helloworld.tpl.
<div class="box">
  <div class="box-heading"><?php echo $heading_title; ?></div>
  <div class="box-content" style="text-align: center;"><?php echo 
    $code; ?></div>
</div>
The $heading_title file holds the text "Hello World" and $code holds the message or text that is inserted into the Hello World module at the backend.

Summary

In this chapter, we duplicated the Google_talk module to create the Hello World module. Hello World is created, installed, configured, and uninstalled. On configuration, we inserted some data and showed the same at the frontend.
We found out how code works in the Hello World module and its file and folder structure. We also described all the code that we used in the Hello World module's files. Taking reference of Hello World module, we should be able to go through other modules and become familiar with the modules of OpenCart.

No comments:

Post a Comment