Opencart2 : Create custom module in admin

Here is the way add custom helloworld module in admin.

  1. create admin/controller/custom/helloworld.php
    <?php
    class ControllerCustomHelloworld extends Controller {
    
     public function index() {
    
     $this->load->language('custom/helloworld');
     $this->document->setTitle($this->language->get('heading_title'));
     $this->load->model('custom/helloworld');        
    
     $data['header'] = $this->load->controller('common/header');
     $data['column_left'] = $this->load->controller('common/column_left');
     $data['footer'] = $this->load->controller('common/footer');
     $this->response->setOutput($this->load->view('custom/helloworld.tpl', $data));
    
     }
    }
    ?>
  2. Create admin/model/custom/helloworld.php

    <?php
    class ModelCustomHelloworld extends Model {
    
    public function helloworldmodel(){
    
     }
    }
    ?>
  3. create admin/language/english/custom/helloworld.php

    <?php
    // Heading
    $_['heading_title']          = 'Hello world Admin module';
    
    ?>
  4. Create admin/view/template/custom/helloworld.tpl

    <?php echo $header; ?><?php echo $column_left; ?>
    <h1><?php echo "This is helloworld admin module in opencart 2.x.x.x "; ?></h1>         
    <?php echo $footer; ?>
  5. Go to system -> users -> user groups -> edit Administrator group. Select all to access permission and modify permission and save.custom-module

Open Cart Tutorial

 

Introduction to MVC-L

 

OpenCart is an excellent platform for developers looking to understand PHP web frameworks in general. It is one of the easiest to follow MVC structured applications available. OpenCart allows you to learn the MVC framework while giving you access to the familiar PHP, mySQL and HTML technologies on which it is built. This guide will assume a basic understanding of HTML, CSS, Javascript, PHP (including classes and inheritance), and mySQL, and will describe how these are used in the OpenCart system.

MVC(L)

OpenCart is designed to follow an MVC design pattern. The components of MVC (Model View Controller) can be broken down as follows.

M - Model
This is where you will interact directly with your database, pulling data out and restructuring it to a format that is suitable for your frontend. This will usually mainly consist of DB queries, and little more. If you are used to writing mySQL queries, you will enjoy the way OpenCart provides access to continue to do just that. OpenCart does not use an ORM, but allows you to write direct database queries.

V - View
This is the display side of the MVC pattern. The idea of the M and C is to pull as much logic out of the view as possible, meaning simpler templates. In order to redesign your whole store, you simply modify the View component, the M, C and L would remain the same. The view files in OpenCart have the .tpl suffix.

C - Controller
This is where you will pull together the data from the Model, any config settings saved with your install or modules, and then render it by choosing the appropriate View file(s).

L - Language
OpenCart extends MVC to MVCL, providing an easy way of separating language specific information for internationalization. You can use language files to store any text like headings, titles, button text, etc., so that you only need to adjust one file per language to provide translations of your store.

Directory structure
The OpenCart directory structure is based around two important parts of the OpenCart application.
The frontend and the admin interface are each represented by a folder in the top level of your OpenCart installation. The frontend folder is called catalog/ and the admin folder is called admin/. If you are making modifications only to the admin of an OpenCart store, you should not expect to modify any file within the catalog/ folder. If you are working on the frontend only, you should not be modifying the admin/ folder at all.
Within each of the admin/ and catalog/ folders, you will find a folder for each of the Model, View, Controller and Language components of the OpenCart application.
Several other folders exist in your OpenCart installation.
The system folder contains classes and functions used by both the admin and catalog areas of your store. Within this folder are email helpers, database helpers, the core definition of controllers, models and other parts of the OpenCart engine and library classes. When modifying OpenCart functionality, you will seldom need to edit any of the system files.
The image folder contains all the images that are uploaded via the Image manager. These are your product images, additional images, and the cached versions of images that OpenCart has resized.
The download folder contains any downloads associated with products. Downloads are given a hashed suffix to avoid malicious users guessing the filenames correctly and accessing your downloads directly. This is why you will see random strings on the end of your download filenames in this directory.

 

Developing modules

 

Writing OpenCart modules can be a very good way to learn how the fundamentals of OpenCart actually work. Just as the rest of OpenCart, modules follow the MVCL design pattern. This documentation guide will describe how you use each of the MVC-L components to create the admin and frontend parts of your module. The easiest way to create a module is to download the DIY Module Builder skeleton from HostJars. This module contains the directory structure, files, and instructions on how to understand and build your own modules. This page is a more theoretical guide.

Basic directory structure
The basic file structure for your module will be divided into two sections, the admin and the catalog folders. The contents of each folder will follow the MVC-L framework respectively, with the difference that the admin will deal solely with backend functionality, and the catalog with frontend functionality. Users of your module will interact and configure its settings in the administration side of the store. Therefore, the files in the admin folder will handle any changes to its settings, the way the module is displayed in the administration, install/uninstalling the module, etc. Likewise, the way the module is displayed and how it works in the front end of the store will be handled by the files in the catalog folder.

The image above displays a skeleton of the directory structure that your module should follow. A good way to get started with your module is to duplicate the folder structure and create the files above. What will go in those files is determined by what your module is trying to accomplish, but the basic functionality is detailed in the sections below.

Admin module functionality
When a store owner uses your module, they will want to edit the module’s configuration options in the admin in order to decide which layouts to display the module on, whether it is enabled or disabled, and any module specific options. As the module developer, you will need to create the admin page where the module may be edited and the configuration options added or adjusted.
All admin files are located in the admin/ folder. You will find four folders within the admin/ folder:

  1. controller
  2. view
  3. language
  4. model

All modules will require at least a single file in each of the view and controller folders. Most will require a file in each of the model and language folders. Usually the files have the same name, except the view file has a different suffix (.tpl). We will go through these files one by one.

Controller

The first file you make will be the controller for your module’s admin interface page. OpenCart identifies existing modules automatically, simply by reading the admin/controller/module folder of your store. Any modules existing in this folder will automatically be shown on the Modules page, and on the User Permissions, page. You may call your controller file my_module.php.
The controller file is the place where you can load the language files to convert text into variables to be utilized in the template file. In the diagram above, you can see the $_[‘text’] variable being handled by the controller, then sent as $text to the view. You will also utilize multiple model files and their class functions here, including your module’s model file if it has one. For more information on loading files see Loading files in the controller.
You may also have a function defined as public function install(). This function will be triggered when the install link is clicked on the Extensions > Modules page. Similarly, a function defined as public function uninstall() will be triggered when the uninstall link is clicked. You can use these functions to create and remove any structures (such as database tables or config settings) required by your module. It is good practice to create an uninstall function to clean up any changes your module has made. To see the specific code for an install(), uninstall(), visit Install/Uninstall a module.

Accessed via URL

The controller is the only file in the MVC-L framework to be accessed by URL in OpenCart. In the administration, the URL will look like /admin/index.php?route=module/my_module&token. The admin adds a token to the URL, whereas the link in the catalog will not have it. As a result, the controller file will have a function defined as public function index(). This is a publicly accessible ‘page’ that is loaded by the URL, which will be shown when the Edit button is clicked, and where the view form will submit to. The submitted data will be processed in this function and saved to the `settings` database table through the controller’s config object.

View
The second required file for your module’s admin interface is the view file. This will be created in the admin/view/template/module folder, and will have the suffix .tpl. This is standard for OpenCart view files. In this file you will create a form for the user to fill out and submit. It will be submitted to the module controller’s index function. The easiest way to create your view file is to copy and paste an existing, similar view file, and edit the form to contain the correct fields for your module’s configuration options. You may call your view file my_module.tpl.
In the view, you will be able to access the text from the language that the controller file stored as a PHP variable. See Loading Files in the Controller for the code on how to do so.

Model

It is uncommon for modules to require a model file on the admin side. However, if your module relies on its own database table, or a custom query to create data of a particular format, then you may find yourself writing a model file. An example of this may be if you are writing a visitor counter module, where each visitor is stored in a database table with their IP address and number of visits. You may then create a model file, with a function to define and create this extra table in the OpenCart database. The model file will live in the admin/model/module folder. You may call your model file my_module.php, the same as your controller and language files.

Frontend module functionality

The frontend of your module follows the same pattern as the admin interface just described. What you will include in each of your frontend files will largely depend on what your module is supposed to do. A module can access any model files that already exist in OpenCart, you do not need to write your own database queries if the same query already exists. For example, the catalog/product model contains many useful queries for fetching products. Using these model functions should be preferred over reinventing the wheel.
A key difference in the frontend of your module, is that your view file will be in the catalog/view/theme//template/module folder. This is a significantly deeper folder structure to the admin view file because of themes.
On the frontend part of your module you will have access to the configuration options saved by your module, through both the controller’s config object, and the $settings variable passed to the module controller’s index function. You can control aspects of the frontend display on the basis of these settings.

 

Install/Uninstall a module

 

It is recommended that developers include an install and uninstall function for their modules in the controller file. Within these functions we will need to load the necessary model files for a clean install/uninstall. To learn how to load a file in the controller, see Loading files in the controller.

Install:

The first thing we will include within the install function, are calls to any functions from the model where tables are created in the db. Secondly, we will want to enable the module for OpenCart with the install. Therefore, install function should loosely follow the code below:

public function install() {
       $this->load->model('module/my_module');
       $this->model_feed_my_module->createTable(); 
       $this->load->model('setting/setting');
       $this->model_setting_setting->editSetting('my_module', array('my_module_status'=>1));
   }

Uninstall:

The uninstall function should loosely follow the code below:

public function uninstall() {
        $this->load->model('module/my_module');
        $this->model_feed_my_module->deleteTable();
         
        $this->load->model('setting/setting');
        $this->model_setting_setting->editSetting('my_module', array('my_module_status'=>0));
    }

If we created a table in the db from a function in the model for the install, we will need to load another function from the model that removes the table when uninstalled. Also, we will want to disable the status of the module when uninstalled.

 

Developing new product feeds

 

OpenCart includes several useful Product Feeds in the core, but you may find you require a custom format and decide to create your own. Writing OpenCart product feeds is very similar to writing OpenCart modules, and can be a very good way to learn how the fundamentals of OpenCart actually work. Just as the rest of OpenCart, feeds follow the MVCL design pattern. This documentation guide will describe how you use each of the MVCL components to create the admin and frontend parts of your product feed.

Admin feed functionality

When a store owner uses your product feed, they will want to edit the product feed’s configuration options in the admin in order to select whether it is enabled or disabled and any specific options you make available. As the developer, you will need to create the admin page where the product feed may be edited.
All admin files are located in the admin/ folder. You will find four folders within the admin/ folder:

  1. controller
  2. view
  3. language
  4. model

All product feeds will require at least a single file in each of the view and controller folders. Some will require a file in each of the model and language folders. Usually the files have the same name, except the view file has a different suffix (.tpl). We will go through these files one by one.

Controller

The first file you make will be the controller for your product feed’s admin interface page. OpenCart identifies existing product feeds automatically, simply by reading the admin/controller/feeds folder of your store. Any product feed files existing in this folder will automatically be shown on the Product Feeds page, and on the User Permissions, page. You may call your controller file my_feed.php.
The controller file will have a function defined as public function index(). This is a publicly accessible ‘page’, which will be shown when the Edit button is clicked, and where the view form will submit to. The submitted data will be processed in this function and saved to the `settings` database table through the controller’s config object.
You may also have a function defined as public function install(). This function will be triggered when the install link is clicked on the Extensions > feeds page. Similarly, a function defined as public function uninstall() will be triggered when the uninstall link is clicked. You can use these functions to create and remove any structures (such as database tables or config settings) required by your feed. It is good practice to create an uninstall function to clean up any changes your feed has made.

View

The second required file for your feed’s admin interface is the view file. This will be created in the admin/view/template/feed folder, and will have the suffix .tpl. This is standard for OpenCart view files. In this file you will create a form for the user to fill out and submit. It will be submitted to the feed controller’s index function. The easiest way to create your view file is to copy and paste an existing, similar view file, and edit the form to contain the correct fields for your feed’s configuration options. You may call your view file my_feed.tpl.

Language

The third file you will usually create for any feed is the language file(s). You will need Cheap AC Milan football shirts one language file per language that your feed is compatible with. The language file will live in the admin/language//feed folder. It simply contains a PHP associative array called $_, which contains the internal name as the key and the translation as the value. You may call your language file my_feed.php, the same as your controller and model files.

Model

It is uncommon for feeds to require a model file on the admin side. However, if your feed relies on its own database table, or a custom query to create data of a particular format, then you may find yourself writing a model file. An example of this may be if you are writing a visitor counter feed, where each visitor is stored in a database table with their IP address and number of visits. You may then create a model file, with a function to define and create this extra table in the OpenCart database. The model file will live in the admin/model/feed folder. You may call your model file my_feed.php, the same as your controller and language files.

Frontend feed functionality

The frontend of your feed follows the same pattern as the admin interface just described. What you will include in each of your frontend files will largely depend on what your feed is supposed to do. A feed can access any model files that already exist in OpenCart, you do not need to write your own database queries if the same query already exists. For example, the catalog/product model contains many useful queries for fetching products. Using these model functions should be preferred over reinventing the wheel.
A key difference in the frontend of your feed, is that your view file will be in the catalog/view/theme//template/feed folder. This is a significantly deeper folder structure to the admin view file because of themes. An OpenCart store may have many different frontend themes available, but only one admin template.
On the frontend part of your feed you will have access to the configuration options saved by your feed, through both the controller’s config object, and the $settings variable passed to the feed controller’s index function. You can control aspects of the frontend display on the basis of these settings.

 

Loading files in the controller

 

In OpenCart’s MVC-L framework, your module’s controller is the glue connecting your the language, model, and template files to each other. The controller is responsible for grabbing the text contained in the language file and making them accessible as PHP variables in Cheap Barcelona football shirts the view’s template file. In addition to inheriting the functions available in the controller’s parent class, Controller, you can also load any of OpenCart’s default model files and their functions in the controller. In this documentation, we will show the PHP code needed to load language and model files and their functions.

Loading the language file

The controller brings the text stored in the language file, and turns them into variables that can be echoed in the template file to displayed text. This is especially useful for managing translations of your module. Instead of modifying your .tpl file every time you have a new translation to change each piece of text inside, you just need to modify the text in your language file, and the variables will remain the same in the controller and the template.
The piece of code below will load the language file inside in your module’s controller. Inside the parentheses you will need to include the path to the language file from inside the language folder.

$this->load->language('module/my_module');

It is important to remember that the admin controller will only load the admin language file, but not the catalog language file; and likewise the catalog controller will only load the catalog language file. Once the language file is loaded into the controller, you can store its text into a php variable with the use of the $data array. The $this->language->get('text') will grab the text from the $_['text'] variable inside of the language file.

// for opencart version below than 2.x.x.x
$this->data['text'] = $this->language->get('text');

//for opencart version 2.x.x.x + 
$data['text'] = $this->language->get('text');

The $this->language->get(‘text’) will grab the text from the $_[‘text’] variable inside of the language file we just loaded above. Every element of the data array will be converted into its own variable. The $data[‘text’] will become $text for the template file inside view. The $text variable can be echoed in the view’s .tpl file wherever needed:

<?php echo $text; ?>

Setting the heading title

The following code will set text from the language file as the heading title of the page:

$this->document->setTitle($this->language->get('heading_title'));

This will grab the text for the variable $_['heading_title'] stored in the module’s language file.
If you need the text to be stored as a php session variable, use $this->session->data['text'] instead of $this->data['text'] or $data['text'].

Loading model files

Loading model files into your controller file will allow your module to utilize OpenCart’s built-in functions. The functions inside the model files interact with the store’s database and to add/pull important information for your module. We recommended that you to take advantage of these functions, rather than making your own DB queries. Take some time to explore the model folders in both the admin and catalog files, to see which files may benefit cheap football shirts your module’s purpose. For example, if your module needs to pull product information from the store’s database, it will be useful to load the admin/model/catalog/products.php file, since it already has a multitude of helpful, built-in functions that interact with the store’s products in the database.
Your module can load any model file its controller file using the following code, granted that they are in the same admin or catalog folder as the controller.

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

You will need to specify the path to the file you want to load from the admin cheap football tops folder within the parentheses. The code above will load the settings class so we have access to the functions within the ModelSettingSetting class in our model’s controller file. Use the following format in your code to call a function from a loaded model file:

$this->model_setting_setting->editSetting('my_module', $this->request->post);

The underscores refer to the file designations for model/setting/setting.php. If you have a model file included for your module your code would follow the format mentioned above, since the model file is uploaded to model folder.

$this->load->model(module/my_module.php);
$this->model_module_my_module->myFunction();

The code above will load the my_module.php stored in admin/model/module/my_module.php.
Instead of using spaces in file names for your module, use underscores.

Loading template files
In the controller you will need to load your module’s template file in view. To do so, set $this->template or $data['template'] as so:

$this->template = 'module/my_module.tpl';
or
$data['template'] = 'module/my_module.tpl';

Loading library files

The OpenCart directory contains a collection of library files that can be cheap football kits accessed by both the admin and catalog controller files. These files are located under system/library in the root folder of the OpenCart store. In the code examples seen in loading the language file, both $this->load->language and $this->document give access to functions within the language.php and document.php files in the library folder. If you want to access a function in a library file you need to call it using $this->[insert library file name]->function() in the controller class.

cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap France football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |
cheap football shirts  |