Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    hyperlocal-marketplace-guide

    Create your first basic module in Opencart

    The Opencart is one of the great examples of MVC framework as it provides separate folders for controller, model, view and language. In order to start creating your first Opencart module, you must know the basic fundamentals of Opencart. You can learn the basic fundamentals of Opencart by visiting Opencart documentation. For creating a module in Opencart, you have to follow a directory structure. You can learn about the directory structure here.

    So, in order to start module development in Opencart, you have to create a file in module folder of each controller, language and view. You don’t need to create a model file in module development as your values of the module will be stored in a table named setting. The queries for saving the values in setting table is written in admin->model->setting->setting.php file.

    We will first learn the module development in Opencart 2.2.0.0. The development details and change in the code according to other versions will be provided in the comments part of the code provided by us.

    First, we will create the module’s language file named first_module.php in admin->language->en-gb->module folder. This path will be for the Opencart version 2.2.0.0. For Opencart version 2.3.0.0 and above, we have to create the file in admin->language->en-gb->extension->module. For Opencart version under 2.2.0.0, we have to create the file in admin->language->english->module.

    In this file, we will assign the languages to the indexes of an array. Please mind that $_ is the name of the array. Here, is the language, we shall need in the module creation.


    After creating the language, we will head towards the controller. So, we will create a file named first_module.php in admin->controller->module folder. For 2.3.0.0 and upper versions, you have to create the file in admin->controller->extension->module folder.

    Here, I’m providing the controller code along with the comments.


    With the above code, you would learn many other things about the Opencart as well like how to load language, model etc and how to make their use.

    After the controller, we have to go for the view file. So, we will create a file named first_module.tpl in admin->view->template->module folder. For 2.3.0.0 and upper versions, you have to create the file in admin->view->template->extension->module folder.

    Here’s the view file code.


    So, here we complete with the module coding part. Now, you have to visit Extensions->Modules, where you can see the “First Module” in the modules listing. From there you can install the module and then configure it.

    You can also optimise your code by following this.

    Hope, you learn something from this blog. Keep following our blog, to know more about the Opencart.

    . . .

    Comments (14)

    Add Your Comment

  • Natalie
    Hi – how can I check this has worked? The module has installed but I’m not sure what page to access to view the new module?
    • Webkul Support
      Hello,
      You can check the module at admin side by heading to Extensions->modules(in version 2.2.0.0 or lower) and to Extensions->Extensions and then selecting Modules from the dropdown list(in version 2.3.0.0 or upper).
      Thanks
  • repetitor
    problem) i clicked “install” and saw “Fatal error: Class ‘Controllerextensionmodulefirstmodule’ not found in D:repetitor202bitbucketopencartpackagiststoragemodificationsystemengineaction.php on line 71”. what can i do not so?)
    • repetitor
      i found solution. title of controller should be ControllerExtensionModuleFirstModule (for opencart 3.*)
      • ahmed
        Module installed but in disable status when click edit with this error message :

        Fatal error: Uncaught Twig_Error_Loader: Unable to find template “module/first_module.twig” (looked into: C:/xampp/htdocs/storage/modification/admin/view/template, C:/xampp/htdocs/journal/admin/view/template). in C:xampphtdocsjournalsystemlibrarytemplateTwigLoaderFilesystem.php:215 Stack trace: #0 C:xampphtdocsjournalsystemlibrarytemplateTwigLoaderFilesystem.php(139): Twig_Loader_Filesystem->findTemplate(‘module/first_mo…’) #1 C:xampphtdocsjournalsystemlibrarytemplateTwigEnvironment.php(312): Twig_Loader_Filesystem->getCacheKey(‘module/first_mo…’) #2 C:xampphtdocsjournalsystemlibrarytemplateTwigEnvironment.php(378): Twig_Environment->getTemplateClass(‘module/first_mo…’, NULL) #3 C:xampphtdocsstoragemodificationsystemlibrarytemplatetwig.php(51): Twig_Environment->loadTemplate(‘module/first_mo…’) #4 C:xampphtdocsstoragemodificationsystemlibrarytemplate.php(63): TemplateTwig->render(‘module/first_mo…’, ‘1’) #5 C:xampphtdocsstoragemodificationsystemengineloade in C:xampphtdocsjournalsystemlibrarytemplateTwigLoaderFilesystem.php on line 215

        Module is searching about template files

        • Vikhyat Sharma
          Hello,
          This blog is about creating the module in Opencart version 2.x. The error is occurring because there is no twig file as Opencart version 3 uses twig. You can find a twig file in extension/module folder and by performing some modifications you will be able to use it for version 3.
          Thanks
    • repetitor
      but new problem: Undefined index: token in D:repetitor202bitbucketopencartpackagistpublic_htmladmincontrollerextensionmodulefirst_module.php on line 92Notice: Undefined index: token in
      may be somebody could suggest good tutorial for inslalling simplest module for opencart 3.*?)
      • Webkul Support
        Thank you for the contact. We will try to update it in our next blog
        • Kevin
          Hi, I’ve tried to create this module with the latest version (3.0.2.0) but I keep getting an error when I try to edit the module:

          Undefined index: token in C:xampphtdocstestadmincontrollerextensionmodulefirst_module.php on line 90

          I’ve been fiddling for a while, but not sure what the problem is?

          • Atendra Singh Jadon
            sir if you are create this module with the latest version (3.0.2.0) than please replace token to user_token (in controllerextensionmodulefirst_module.php )
          • Kevin
            Thank you for such a quick reply. I actually did do that after I posted my comment, but then it just returns a blank screen when I try to edit the module. No header, footer, or sidebar at all. I’m completely unfamiliar with Opencart 3, the last iteration I coded for was 1.5, and that was 3 years ago. I think there is a problem with my template file? I see Opencart is using Twig now, which I’ve never used before as well.
          • Vikhyat Sharma
            Hello,
            Twig is very easy to use. You can edit any of the pre-existing file in the extension/module folder. And by performing some modifications you will be able to make your first module.
            Thanks
  • Lesna Vevericka
    hello, thanks for the article, could you present also the communication of model – controller? I mean calling $this->ModelX->abcfuncDB(); in Controller class? Thanks if possible. I only get blank screen if I call this way. I am on opencart 2.3.0.2
    • Vikhyat Sharma
      Hello,
      We have mentioned that in 37th and 47th line of the module controller. First you have to load the model this way:
      $this->load->model(‘folder_name/file_name’);
      This works just like you include/require a file. Then you can have an access to your model’s function by:
      $this->model_folder_name_file_name->functionName();
      Thanks
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close