How to set the module on layout programmatically in opencart

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

 Why We Need it: 

In the Opencart some modules are created to enhance the functionality and feature of the particular pages such as Account page, Product Page, Home Page etc. in various ways then we need to install the module and after it configuration, most of the time the customer does not find it at front end because they forgot to set the module on that particular page.

How to resolve it programmatically:

First, we configure the module and if it’s status will enable then we will proceed to set it on the layout.

Second, we check that the module is not already set on 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 module’s code, position and sort order.

For Example, We have to set the Booking and Reservation module at product page at the content bottom.

 * Webkul Software.
 * @category Webkul
 * @package api
 * @author Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (
 * @license
//After saving the module validate the module configuration
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
   // Get the layout id of that particular layout on which module is to be set
   $layout_id = $this->model_module_wk_booking_res->getLayoutId();
   //Check for layout id and check that our configured module's status is enable
   if($layout_id && isset($layout_id['layout_id']) && $this->request->post['wk_booking_res_status']) {
     $flag = 1;
     // Get the layout_modules and Check that module is already set or not
     $layout_modules = $this->model_design_layout->getLayoutModules($layout_id['layout_id']);
     foreach ($layout_modules as $key => $value) {
	   if(isset($value['code']) && $value['code'] =='wk_booking_res') {
	     $flag = 0;
     // Check if module is not already set
     if($flag) {
       //Insert in to the layout module for the particular layout id with our module code,position and sort order
	   $this->model_module_wk_booking_res->editLayout($layout_id['layout_id'], 'wk_booking_res','content_bottom',0);
   $this->model_setting_setting->editSetting('wk_booking_res', $this->request->post);			
   $this->session->data['success'] = $this->language->get('text_success');			
   $this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));

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

Output :

. . .

Ask a Feature