Reading list Switch to dark mode

    Upgrade Opencart 3 Extension to Opencart 4

    Updated 5 June 2023

    Upgrading from OpenCart version 3 extension to OpenCart version 4 is an important step to take advantage of the latest features, improvements, and security enhancements offered by the newer version of the e-commerce platform.

    This upgrade process requires careful planning and execution to ensure a smooth transition while preserving your store’s data and functionality.

    These are the major upgrade in opencart version4

    • User Interface and Design
    • Performance Enhancements
    • Extension Compatibility
    • Security Enhancements
    • Improved SEO Features
    • Upgraded Technology Stack
    • Directory structure
    • Event-Based

    The major upgrade in the Opencart 4 is the directory structure. Version 4 now follows a different structure compared to previous versions.

    What is the difference between version 3 & version 4?

    Searching for an experienced
    Opencart Company ?
    Find out More

    Before starting, it is important to understand the basic functionality of Version 4. In this version, Opencart has removed “ocmod,” so any modifications or changes should be made using the “event” system.

    Opencart Version 4 utilizes namespacing for controller definition and relies on Ajax for handling all requests.

    upgrade version4 namespace

    In this version 4 methods have “void” non-return-value.

    method void

    What upgrades are required to convert the version 3 extension to version 4?

    To begin, it is important to take a backup of your files and database. Afterward, download the latest Opencart Version 4 files and configure them in a similar manner to how you had previously configured Version 3.

    Opencart version 4 introduces new changes, including a modification in calling class methods. To invoke a class method, you must define it in the following format: “extension/module/example.save”. Note that there is a dot (.) separating the class and the method.

    Let’s start the changes in the extension to make it compatible with the latest version 4.

    Ensure that you define the “namespace” in all of your extension files with the .php extension. Next, you should include an “install.json” file in the root folder of your extension.

    {
      "name": "Example Extension",
      "version": "1.0",
      "author": "Webkul Software Pvt. Ltd.",
      "link": "https://www.webkul.com",
      "instruction": ""
    }

    Now let’s check the version 4 module directory structure.

    directory structure

    Now we are taking an opencart version 3 extension example to convert into version 4.

    Let’s upgrade the controller to version 4

    <?php
    /**
     * Webkul Software.
     *
     * @category Webkul
     * @package Opencart Module Tutorial
     * @author Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    namespace Opencart\Admin\Controller\Extension\Webkul\Module;
    /**
     * The controller class must extend the parent class i.e. Controller
     * The controller name must be like Controller + directory path (with first character of each folder in capital) + file name (with first character in capital)
     */
    class FirstModule extends \Opencart\System\Engine\Controller  {
        /**
         * property named $error is defined to put errors
         * @var array
         */
        private $error = array();
        /**
         * Basic function of the controller. This can be called using route=module/first_module
         */
        public function index():void {
            /**
             * Loads the language file. Path of the file along with file name must be given
             */
            $this->load->language('extension/module/first_module');
            /**
             * Sets the title to the html page
             */
            $this->document->setTitle($this->language->get('heading_title'));
            
            /**
             * Putting the language into the '$data' array
             * This is the way how you get the language from the language file
             */
            $data['heading_title'] = $this->language->get('heading_title');
    
            $data['text_edit'] = $this->language->get('text_edit');
            $data['text_enabled'] = $this->language->get('text_enabled');
            $data['text_disabled'] = $this->language->get('text_disabled');
    
            $data['entry_status'] = $this->language->get('entry_status');
    
            $data['button_save'] = $this->language->get('button_save');
            $data['button_cancel'] = $this->language->get('button_cancel');
            /**
             * If there is any warning in the private property '$error', then it will be put into '$data' array
             */
            if (isset($this->error['warning'])) {
                $data['error_warning'] = $this->error['warning'];
            } else {
                $data['error_warning'] = '';
            }
            /**
             * Breadcrumbs are declared as array
             */
            $data['breadcrumbs'] = array();
            /**
             * Breadcrumbs are defined
             */
            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
            );
    
            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_module'),
                'href' => $this->url->link('extension/module', 'user_token=' . $this->session->data['user_token'], true)
            );
    
            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('extenison/module/first_module', 'user_token=' . $this->session->data['user_token'], true)
            );
            /**
             * Form action url is created and defined to $data['action']
             */
            $data['action'] = $this->url->link('extension/module/first_module.save', 'user_token=' . $this->session->data['user_token'], true);
            /**
             * Cancel/back button url which will lead you to module list
             */
            $data['cancel'] = $this->url->link('extension/module', 'user_token=' . $this->session->data['user_token'], true);
            /**
             * checks whether the value exists in the post request
             */
            if (isset($this->request->post['module_first_module_status'])) {
                $data['module_first_module_status'] = $this->request->post['module_first_module_status'];
            } else {
            /**
             * if the value do not exists in the post request then value is taken from the config i.e. setting table
             */
                $data['module_first_module_status'] = $this->config->get('module_first_module_status');
            }
            /**
             * Header data is loaded
             */
            $data['header'] = $this->load->controller('common/header');
            /**
             * Column left part is loaded
             */
            $data['column_left'] = $this->load->controller('common/column_left');
            /**
             * Footer data is loaded
             */
            $data['footer'] = $this->load->controller('common/footer');
            /**
             * Using this function tpl file is called and all the data of controller is passed through '$data' array
             * This is for Opencart 2.2.0.0 version. There will be minor changes as per the version.
             */
            $this->response->setOutput($this->load->view('extension/module/first_module', $data));
        }
    
        /**
    	 * save method
    	 *
    	 * @return void
    	 */
    	public function save(): void {
            /**
             * Load language file
             */
    		$this->load->language('extension/module/first_module');
    
    		$json = [];
    
    		if (!$this->user->hasPermission('modify', 'extension/webkul/module/first_module')) {
    			$json['error']['warning'] = $this->language->get('error_permission');
    		}
    
    		if (!$json) {
    			/**
                 * Loads the model file. Path of the file to be given
                 */
                $this->load->model('setting/setting');
    
    			$this->model_setting_setting->editSetting('module_first_module', $this->request->post);
    
    			$json['success'] = $this->language->get('text_success');
    		}
    
    		$this->response->addHeader('Content-Type: application/json');
    		$this->response->setOutput(json_encode($json));
    	}
    }

    Note: There are no changes in the view & language files.

    After completing all steps you can zip it and name it that ends with .ocmod.zip, then upload it from the Opencart extension installer.

    ocv4-addon-install
    Extensions
    extension-configuration-1

    The previous Opencart versions had the ocmod feature to make changes in core files but now in version 4 Opencart removed the ocmod and made the extension event-based.

    Once you have completed that, if you wish to include a menu and other additional features, please refer to the documentation of the event for registration information.

    If you need custom Opencart Development services then feel free to reach us and also explore our exclusive range of Opencart Addons.

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home