Reading list Switch to dark mode

    How to set the module on layout programmatically in opencart

    Updated 11 August 2023

    Today we will learn about how to set the Opencart module on the layouts such as the product page or account page programmatically during editing the module or installing the module.

    Why We Need it: 

    OpenCart layouts are one of the best ways to extend the OpenCart front end. In Opencart some modules are created to enhance the functionality and feature of particular pages such as the Account page, Product Page, Home Page, etc.

    Every page you open in your OpenCart store has a link. Each configuration defines 4 possible locations where you can place the modules.

    For example, you can place the “category” module in the Left Column, Right Column, Content Top, and Content Bottom positions in the category layout.

    Searching for an experienced
    Opencart Company ?
    Find out More

    we need to install the module and after its configuration, most of the time the customer does not find it at the front end because they forgot to set the module on that particular page.

    How to set the module in the layout:

    First of all, we configure the module and if its status will be enabled then we will proceed to set it to layout.

    Second, we check that the module isn’t already set to layout. Select layout or create one if not set in the layout.

    Third, If the module is enabled and not already set on the layout then we add it in layout_modules of that layout with the module’s code, position(eg: column left, column right, content top, and content bottom), and sort order.

    How to resolve it programmatically:

    First, we get all the installed models. There are two types of modules in which some modules have many settings like ‘Banner’ as shown in the image:

    blog-img-1

    /**
     * Webkul Software.
     * @category Webkul
     * @package api
     * @author Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    
    // Get a list of installed modules
    $extensions = $this->model_setting_extension->getInstalled('module');
    
    
    // Add all the modules which have multiple settings for each module
    foreach($extensions as $code) {
       $this->load->language('extension/module/'. $code, 'extension');
       
       $module_data = array();
       $modules = $this->model_setting_module->getModulesByCode($code);
    
       foreach($modules as $module) {
          $module_data[] = array(
          'name' => strip_tags($module['name']),
          'code' => $code .'.'. $module['module_id']
          );
       }
    
       if ($this->config->has('module_'. $code .'_status') || $module_data) {
          $data['extensions'][] = array(
          'name' => strip_tags($this->language->get('extension')
            ->get('heading_title')),
          'code' => $code,
          'module' => $module_data
          );
       }
    
    }

    Suppose you want to add your module to the Account layout then go to design->layout->Account layout where you see your module in the drop-down menu.

    Here, you can add the module to the layout in four position column left, column right, content top, and content bottom as shown in the image below:

    blog-img-2

    foreach ($layout_modules as $layout_module) {
        $part = explode('.', $layout_module['code']);
    
        if (!isset($part[1])) {
            $data['layout_modules'][] = array(
            'code'       => $layout_module['code'],
            'edit'       => $this->url->link('extension/module/' . $part[0], 'user_token=' . $this->session->data['user_token'], true),
            'position'   => $layout_module['position'],
            'sort_order' => $layout_module['sort_order']
            );
        } else {
            $module_info = $this->model_setting_module->getModule($part[1]);
    
            if ($module_info) {
                $data['layout_modules'][] = array(
                    'code'       => $layout_module['code'],
                    'edit'       => $this->url->link('extension/module/' . $part[0], 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $part[1], true),
                    'position'   => $layout_module['position'],
                    'sort_order' => $layout_module['sort_order']
                    );
            }
        }
    }

    Here you will find some models already set like in the “Account’ module. If you already have some module set in any layout then you can see it here as shown in the image below:blog-img-3

    So, when we install or edit the module and after configuring it as enabled then the module will set on the particular layout at the desired position with the desired sort order.

    We have created many blogs to make Open-cart easy, you can also visit them.
    Click here to view.

    . . .

    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