NewIncredible offer for our exclusive subscribers!Read More
November 14, 2024
Open Cart

How to create a module in Opencart 4

  • July 4, 2024
  • 6 min read
  • 296 Views
How to create a module in Opencart 4

Module in Opencart 4

We have our folder structure in place. Let’s start creating an extension.

First we have to crate language file.

Language

admin/language/en-gb/module/itmindslab_module.php

In this file, we assign the language of the indexes of an array. The array’s name is $_, the language that will be necessary for creating the module.

<?php

$_['heading_title']     = 'Itmindslab Module';
$_['entry_status']      = 'Status';

$_['text_extension']    = 'Extension';
$_['text_edit']         = 'Edit Module';
$_['text_success']		= 'Success: You have modified module!';

$_['help_status']		= 'Enable or disable the module';

$_['error_permission']	= 'Warning: You do not have permission to modify this module!';

Controller

After creating the language, we will head towards the controller. So, we will create a file named itmindslab_module.php in admin->controller->module folder.

admin/controller/module/itmindslab_module.php

<?php

namespace Opencart\Admin\Controller\Extension\Itmindslabmodule\Module;

class Itmindslabmodule extends \Opencart\System\Engine\Controller{
    public function index(){
        $this->load->language('extension/itmindslab_module/module/itmindslab_module');

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

        $data['breadcrumbs'] = [];

        $data['breadcrumbs'][] =[
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashbord', 'user_token=' .$this->session->data['user_token'])
        ];

        $data['breadcrumbs'][] = [
            'text' =>$this->language->get('text_extension'),
            'href' => $this->url->link('marketplace/extension', 'user_token=' .$this->session->data['user_token'] . '&type=module')
        ];

        $data['breadcrumbs'][] = [
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('extension/itmindslab_module/module/itmindslab_module', 'user_token=' .$this->session->data['user_token'])
        ];

        $data['save'] = $this->url->link('extension/itmindslab_module/module/itmindslab_module.save', 'user_token=' . $this->session->data['user_token']);

        $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module');

        $data['module_itmindslab_module_status'] = $this->config->get('module_itmindslab_module_status');

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->view('common/footer');

        $this->response->setOutput($this->load->view('extension/itmindslab_module/module/itmindslab_module', $data));
    }

    public function save(){
        $this->load->language('extension/itmindslab_module/module/itmindslab_module');

        $json = [];

        if(!$this->user->hasPermission('modify','extension/itmindslab_module/module/itmindslab_module')){
            $json['error'] = $this->language->get('error_permission');
        }

        if(!$json){
            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('module_itmindslab_module', $this->request->post);
            $json['success'] = $this->language->get('text_success');
        }
        $this->response->addHEader('content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
}

View

After the controller, we have to go for the view file. So, we will create a file named itmindslab_module.twig in admin->view->template->module folder.

admin/view/template/module/itmindslab_module.twig

{{ header }}{{ column_left }}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="float-end">
                <button type="submit" form="form-module" data-bs-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa-solid fa-save"></i></button>
                <a href="{{ back }}" data-bs-toggle="tooltip" title="{{ button_back }}" class="btn btn-light"><i class="fa-solid fa-reply"></i></a></div>
                <h1>{{ heading_title }}</h1>
                <ol class="breadcrumb">
                    {% for breadcrumb in breadcrumbs %}
			            <li class="breadcrumb-item">
                            <a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a>
                        </li>
		            {% endfor %}
                </ol>
            </div>
        </div>
    <div class="container-fluid">
        <div class="card">
            <div class="card-header"><i class="fa-solid fa-pencil"></i> {{ text_edit }}
            </div>
            <div class="card-body">
                <form id="form-module" action="{{ save }}" method="post" data-oc-toggle="ajax">
                    <div class="row mb-3">
                        <label class="col-sm-2 col-form-label">{{ entry_status }}</label>
                        <div class="col-sm-10">
                            <div class="form-check form-switch form-switch-lg">
                                <input type="hidden" name="module_itmindslab_module_status" value="0"/>
                                <input type="checkbox" name="module_itmindslab_module_status" value="1" id="input-status" class="form-check-input"{% if module_itmindslab_module_status %} checked{% endif %}/>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{{ footer }}

Once you have created the extension, you will need to include some information within the module’s “install.json” file. This file supports the following keys: name, code, version, author, and link.

Inastall.json file will create inside the module folder.

Install.json

{
    "name": "Itmindslab Module",
    "code": "itmindslab_module",
    "version": "1.0",
    "author": "itmindslab",
    "link":"https://www.itmindslab.com/"
}

Once install.json is inside the directory, you can zip it and name it that ends with .ocmod.zip, then upload it from the Opencart extension installer.

Steps to Install and Configure The Module

Upload & Install Zip

Go to Opencart’s Admin panel -> Extension -> Installer.

Configure

1:- Admin panel -> Extensions -> Extensions -> Modules.

2:- Install and edit the module.

3:- Enable the status of the module.

So, here we complete the module installation and configuration part.

Now Allow Permissions

1:- Admin panel -> System -> Users -> User Groups.

Select user group for allow permissions

 

2:- Select check box to allow permissions and save.

This is all about admin side module Code, its installation, configuration and permissions. I hope you learn something from this blog.

Keep following our blog, to know more about Opencart Version 4.

About Author

sagar

Leave a Reply

Your email address will not be published. Required fields are marked *